[英]How to run shell script in another server in a script?
No where online can i find a way to run a shell script on a remote server from another script.我无法在网上找到从另一个脚本在远程服务器上运行 shell 脚本的方法。 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
我的脚本将通过 ssh 连接到的服务器将显示密码提示或设置 RSA 密钥对
Thanks!谢谢!
只需将命令作为参数传递给 ssh。
ssh someserver /path/to/some/script.bsh
Let's say you want to execute a script on node2 but you have your script on node1 file name of script is sp over location /home/user/sp.假设您想在 node2 上执行脚本,但您在 node1 上有脚本,脚本的文件名是sp over location /home/user/sp。 Simply
简直
ssh node2 < /path-of-the-script-including-the-filename
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.如果您的服务器没有
expect
,您可以添加该包。 run the command.运行命令。 You can also put this command inside an
.sh
script.您也可以将此命令放在
.sh
脚本中。
expect -c 'spawn ssh user@10.11.12.13 "/path/to/my.sh"; expect "assword:"; send "Y0urp@ssw0rd\r"; interact'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.