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

安稳与你 提交于 2020-06-13 09:18:33

问题


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


回答1:


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"



回答2:


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"



回答3:


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.




回答4:


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


来源:https://stackoverflow.com/questions/30858114/is-it-possible-to-run-multiple-command-with-remote-command-option-in-putty

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!