Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/codeigniter/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Function 在脚本中通过SSH在远程服务器上运行函数_Function_Shell_Ssh - Fatal编程技术网

Function 在脚本中通过SSH在远程服务器上运行函数

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

我有几个函数的shell脚本。我需要在两台不同的机器上运行这个
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执行该文件