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

后端 未结 4 1836
别跟我提以往
别跟我提以往 2021-01-15 23:46

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

相关标签:
4条回答
  • 2021-01-16 00:15

    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
    
    0 讨论(0)
  • 2021-01-16 00:19

    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 讨论(0)
  • 2021-01-16 00:22

    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"
    
    0 讨论(0)
  • 2021-01-16 00:28

    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.

    0 讨论(0)
提交回复
热议问题