bash 如何在脚本中在另一台服务器上运行shell脚本?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37666905/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 14:45:04  来源:igfitidea点击:

How to run shell script in another server in a script?

linuxbashshellsshscripting

提问by Shengqi Wang

No where online can i find a way to run a shell script on a remote server from another script. This is for automation, so the script on the host machine will automatically trigger another script on a different server. The server that my script will ssh to will either have a password prompt or have RSA key pair set up

我无法在网上找到从另一个脚本在远程服务器上运行 shell 脚本的方法。这是为了自动化,因此主机上的脚本将自动触发不同服务器上的另一个脚本。我的脚本将通过 ssh 连接到的服务器将显示密码提示或设置 RSA 密钥对

Thanks!

谢谢!

回答by Quentin

Just pass the command as an argument to ssh.

只需将命令作为参数传递给 ssh。

ssh someserver /path/to/some/script.bsh

回答by user12711146

Let's say you want to execute a script on node2 but you have your script on node1 file name of script is spover location /home/user/sp. Simply

假设您想在 node2 上执行脚本,但您在 node1 上有脚本,脚本的文件名是spover location /home/user/sp。简单地

ssh node2 < /path-of-the-script-including-the-filename

回答by Omari Victor Omosa

Another way, using expect package.

另一种方式,使用expect package.

Disclaimer: This you can use for testing environments since it has an open password. but depends on your usecase

免责声明:您可以将其用于测试环境,因为它有一个开放的密码。但取决于您的用例

If your server does not have expect, you may add the package then. run the command. You can also put this command inside an .shscript.

如果您的服务器没有expect,您可以添加该软件包。运行命令。您也可以将此命令放在.sh脚本中。

expect -c 'spawn ssh [email protected] "/path/to/my.sh"; expect "assword:"; send "Y0urp@ssw0rd\r"; interact'