简体   繁体   English

SSH来自shell脚本的另一个shell脚本

[英]SSH another shell script from shell script

I'm in host1 and I want to shell script shell1.sh to execute another shell script shell2.sh in another host machine host2 . 我在host1 ,我想对脚本shell1.sh进行shell shell2.sh以在另一个主机host2执行另一个shell脚本shell2.sh Need to ssh for same. 需要相同的SSH。 Sorry, I'm little new to this scripting 抱歉,我对该脚本并不陌生

So: 所以:

host1$ sh shell1.sh ----ssh---->  host2$ shell2.sh

On your shell1.sh : 在您的shell1.sh

#!/bin/sh

ssh host2 '/bin/sh /path/to/shell2.sh'

And, if the shell2.sh file not on host2 : 而且,如果shell2.sh文件不在host2

#!/bin/sh

ssh host2 < /path/to/shell2.sh

On the first alternative, you are passing to ssh a second argument, that it interprets as being a command to execute on the remote machine. 在第一种选择上,您要传递给ssh第二个参数,它将其解释为要在远程计算机上执行的命令。

From man ssh : man ssh

If command is specified, it is executed on the remote host instead of a login shell. 如果指定了命令 ,那么它将在远程主机而不是登录shell上执行。

On the second script, the commands on shell2.sh are passed to the ssh command via stdin, which then forwards them to host2 's shell, finally executing them. 在第二个脚本中, shell2.sh上的命令通过stdin传递到ssh命令,然后将它们转发到host2的shell,最后执行它们。

You can doing this : 您可以这样做:

local.sh : local.sh

#!/bin/bash
hostname
whoami
ifconfig

Then, with your local script, you can run it remotely : 然后,使用本地脚本,可以远程运行它:

ssh host < local.sh

The STDIN shell of the remote host will be connected with the local shell script. 远程主机的STDIN Shell将与本地Shell脚本连接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM