2

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)

  • 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 Feb 3 '16 at 13:22
  • How do I change CRLF line terminator to unix line terminator ? Can you please help ? @Hastur – learner1 Feb 4 '16 at 6:38
  • 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 Feb 4 '16 at 6:43
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
  • 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 Feb 4 '16 at 4:56
  • Thank you so much @Martin. It's working damn fine. Lots of appreciation from my side. It is very helpful. – learner1 Feb 5 '16 at 4:40
  • 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 Feb 8 '16 at 12:07
  • 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 Feb 8 '16 at 16:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.