简体   繁体   English

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

[英]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.

相关问题 如何在不同服务器上的另一个 shell 脚本中运行 shell 脚本? - How to run a shell script within another shell script on a different server? 如何通过Shell脚本在远程服务器上连接和运行Shell脚本 - How to connect and run shell script on remote server, via shell script 如何从另一个Shell脚本运行一个Shell脚本,该脚本从CSV文件中读取第一个Shell脚本名称 - How to run a shell script from another shell script reading first shell script name from a CSV file 如何在shell脚本中运行shell / python脚本? - How to run shell/python script in shell script? 在 shell 脚本中运行服务器后,如何运行另一个命令? - How can I run another command after running a server in a shell script? 编写运行其他用户拥有的另一个Shell脚本的Shell脚本 - Writing a shell script that run another shell script owned by other user 使用ssh和shell从另一个PHP脚本在linux服务器中运行php脚本 - Run php script in linux server from another php script using ssh and shell 如何在启动时运行 shell 脚本 - How to run a shell script at startup 如何在TFS服务器的Linux代理中运行Shell脚本 - How to run shell script in linux agent of a TFS server 如何在AIX服务器中运行在shell脚本中创建的功能? - How to run a function created in the shell script in an AIX server?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM