Execute commands using sudo in remote server after logging into PuTTY through batch file

2

3

I need to login into remote server daily and perform same set of tasks. I am using Windows platform and the remote server is Unix. I use PuTTY to login to remote server. To reduce the manual work, I need to automate this task.

I tried this code:

start putty.exe -ssh -load session -l username -pw password -m command.txt -t

The command.txt contains:

sudo su - user1 
ls 
cd directory

Only the first command in command.txt is working and then PuTTY is expecting input from user.

I need all the commands to be run without user input.

Things that matters :

  • I am not root user

  • Access granted to switch to the user1 without password

  • I have a constraint of using PuTTY only (Plink installation is not permitted)

learner1

Posted 2016-02-03T10:48:54.573

Reputation: 155

Try to verify which initialization scripts you run (automatically) when you connect "normally" if they differs from the ones you go to execute with your command,txt. Remember that in Linux and in Windows the end of line are different. Did you check if the command.txt is with the CRLF line terminator (windows) or with the unix-like one? – Hastur – 2016-02-03T13:22:59.940

How do I change CRLF line terminator to unix line terminator ? Can you please help ? @Hastur – learner1 – 2016-02-04T06:38:26.670

The most easy way in Windows is to have a word processor that saves it with the right terminator (I suppose even word can do it). My suggestion is to use notepad++. See here or do tr -d '\r' < infile > outfile in Linux, or look here or ... write it in a Linux computer and import in the windows machine..:) Did you think about to write a script in the Linux machine and execute with a single command line?

– Hastur – 2016-02-04T06:43:35.433

Answers

2

Because the commands that are contained in the command.txt are executed by a master shell one-by-one.

So the master shell executes the sudo, waits for it to exit, before it proceeds with the other commands (ls and cd). And the sudo does not exit (at least not on its own).

While you want the ls and cd to execute within a child shell executed by the su.

You have to tell the su to execute the commands.

This should work:

sudo su - -c "ls ; cd directory" user1

or this

echo "ls; cd directory" | sudo su - user1

Though I expect that you actually want to continue working in the shell. While the above will exit once the commands are executed.

So you want to add a shell to the list of commands:

sudo su - -c "ls ; cd directory ; /bin/bash" user1

or

echo "ls ; cd directory ; /bin/bash" | sudo su - user1

Martin Prikryl

Posted 2016-02-03T10:48:54.573

Reputation: 13 764

I tried the command: sudo su - -c "ls; cd directory" user1
But I got this error:
Sorry, user user1 is not allowed to execute '/usr/bin/su - -c ls and cd directory as a root on that server'. And I need not continue to work on the child shell, it can exit once the commands are executed. @Martin
– learner1 – 2016-02-04T04:56:10.850

Thank you so much @Martin. It's working damn fine. Lots of appreciation from my side. It is very helpful. – learner1 – 2016-02-05T04:40:45.397

Why the shell is being closed automatically though I use -t switch while connecting to remote server using the command start putty.exe -ssh -load session -l username -pw password -m command.txt -t ? – learner1 – 2016-02-08T12:07:59.150

Because the -m makes the server run the commands instead of the interactive shell. So once the commands complete, the session is closed. – Martin Prikryl – 2016-02-08T16:28:32.480