Is it possible to run multiple command with remote command option in putty?

7.8k views Asked by At

I want to run multiple commands automatically like sudo bash, ssh server01, ls , cd /tmp etc at server login.. I am using Remote command option under SSH in putty.

I tried multiple commands with delimiter && but not working.

enter image description here

4

There are 4 answers

1
carnicer On BEST ANSWER

There is a some information lacking in your question.

You say you want to run sudo bash, then ssh server01.

Will sudo prompt for a password in your remote server?

Assuming there is no password in sudo, running bash will open another shell waiting for user input. The command ssh server01 will not be run until that bash shell is exited.

If you want to run 2 commands, try first simpler ones like:

ls -l /tmp ; echo "hi there"

or if you prefer:

ls -l /tmp && echo "hi there"

Does this work?

If what you want is to run ssh after running bash, you can try :

sudo bash -c "ssh server01"
0
Martin Prikryl On

You can execute two consecutive commands in PuTTY using a regular shell syntax. E.g. using ; or &&.


But you want to execute ssh server01 in sudo bash shell, right?

These are not two consecutive commands, it's ssh server01 command executed within sudo bash.

So you have to use a sudo command-line syntax to execute the ssh server01, like

sudo bash ssh server01
2
Aasmund Eldhuset On

That is probably because the command is expected to be a program name followed by parameters, which will be passed directly to the program. In order to get && and other functionality that is provided by a command line interpreter such as bash, try this:

/bin/bash -c "command1 && command2"
1
carnicer On

I tried what I suggested in my previous answer.

It is possible to run 2 simple commands in putty separated by a semicolon. As in my example I tried with ls and echo. The remote server runs them and then the session closes.

I also tried to ssh to a remote server that is configured for not asking for a password. In that case, it also works, I get connected to the 2nd server and I can run commands on it. Upon exit, the 2 connections are closed.

So please, let us know what you actually need / want.