Function 在脚本中通过SSH在远程服务器上运行函数
我有几个函数的shell脚本。我需要在两台不同的机器上运行这个Function 在脚本中通过SSH在远程服务器上运行函数,function,shell,ssh,Function,Shell,Ssh,我有几个函数的shell脚本。我需要在两台不同的机器上运行这个n多次。我可以调用下面提到的函数并执行它吗?或者有没有其他办法 #!/usr/bash execCommand () { #few statements here } getStatus() { #few statements here } main () { execCommand getStatus } $machine1="machine1" $machine2="machine2" $user="use
n
多次。我可以调用下面提到的函数并执行它吗?或者有没有其他办法
#!/usr/bash
execCommand () {
#few statements here
}
getStatus() {
#few statements here
}
main () {
execCommand
getStatus
}
$machine1="machine1"
$machine2="machine2"
$user="username"
$n=2
while [$n -le 2]
do
ssh $user@$machine1 'main'
sleep 100
ssh $user@$machine2 'main'
n=$n+1
done
没有直接的方法可以做到这一点。但您可以提取所有函数定义,并在远程命令开头将其作为命令插入:
ssh "$user@$machine1" "$(declare -f); main"
请注意,全局变量和其他资源可能仍然存在问题。远程计算机将不知道函数
main
。您必须通过ssh
连接发送所有内容。您还可以将文件复制到远程计算机,然后使用ssh执行该文件