通过ssh运行本地脚本

无声的SSH是从服务器A设置到服务器B(和其他约500台服务器)
我在服务器A(shell和perl)上写了一个脚本,我想通过服务器B上的ssh执行脚本(以及其他500台服务器)。 这可能吗 ? 我能够使用沉默的SSH运行命令,但不知道如何运行整个脚本。

如果服务器A是基于Unix / Linux的系统,则可以使用:

ssh root@MachineB 'bash -s' < local_script.sh 

您不必将脚本复制到远程服务器来运行它。

使用这个命令:

 ssh user@host <<'my.sh' #script to run on remote host my.sh 

由于无声SSH已经设置已经设置为你说,我会scp文件的文件,并在本地执行

即:

 while read line do echo Trying to configure server [IP]: $line >> error.log scp my-script.sh $line:/root/scripts/ &>> error.log ssh root@$line 'cd /root/scripts && ./my-script.sh' &> error.log echo Finished working with [IP]: $line >> error.log done <client-ips.txt 

在客户端网站上运行脚本比用<<<运算符parsing它更不容易出错。

类似于上面的脚本应该为你做大部分工作(希望全部)。 此外,它会跟踪任何错误消息(&>转发错误信息),所以你知道哪些IP地址你需要手动出席。

 #!/bin/bash # Source : http://backreference.org/2011/08/10/running-local-script-remotely-with-arguments/ # runremote.sh # usage: runremote.sh localscript interpreter remoteuser remotehost arg1 arg2 ... # example: runremote.sh MySQL_makeUser.sh bash pi coins.ml database user realscript=$1 interpreter=$2 user=$3 host=$4 shift 4 declare -a args count=0 for arg in "$@"; do args[count]=$(printf '%q' "$arg") count=$((count+1)) done ssh $user@$host "cat | ${interpreter} /dev/stdin" "${args[@]}" < "$realscript" # Note: you may need to add options or hardcode keys and such into the above command; example of this commented bellow # ssh -i <path/to/key> -p <port> $user@$host "cat | ${interpreter} /dev/stdin" "${args[@]}" < "$realscript" 

以上是一个脚本,我find了一点点search和修改了一点,以显示使用的例子和使用连接键的例子,因为OP声称它可以在许多其他服务器上运行。 这个脚本也被编码,你可以传递参数给本地脚本并指定服务器应该用来接收脚本命令的程序; 即你可以告诉你的服务器使用Perl或Python或Java …然后给它相关的脚本:-D我发现上面的脚本的源代码是硬编码到它的评论,使复制/过去仍然会允许你多年find原作者;-)

祝大家networking愉快。

你可以通过ssh执行远程命令

ssh user@serverA /path/to/your/script