Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/28.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
Linux 使用ssh命令和config文件在远程计算机中执行shell脚本_Linux_Bash_Shell_Ssh_Scp - Fatal编程技术网

Linux 使用ssh命令和config文件在远程计算机中执行shell脚本

Linux 使用ssh命令和config文件在远程计算机中执行shell脚本,linux,bash,shell,ssh,scp,Linux,Bash,Shell,Ssh,Scp,我想在远程机器上执行一个shell脚本,我使用下面的命令实现了这一点 ssh user@remote_machine "bash -s" < /usr/test.sh property.config: testName=xxx testPwd=yyy 现在,如果我在远程机器上运行shell脚本,我不会得到这样的文件错误,因为/usr/property.config在远程机器上不可用 如何将配置文件与要在远程计算机中执行的shell脚本一起传递?尝试以下操作: ssh user@remo

我想在远程机器上执行一个shell脚本,我使用下面的命令实现了这一点

ssh user@remote_machine "bash -s" < /usr/test.sh
property.config:

testName=xxx
testPwd=yyy
现在,如果我在远程机器上运行shell脚本,我不会得到这样的文件错误,因为/usr/property.config在远程机器上不可用

如何将配置文件与要在远程计算机中执行的shell脚本一起传递?

尝试以下操作:

ssh user@remote_machine "bash -s" < <(cat /usr/property.config /usr/test.sh)

sshuser@remote_machine“bash-s”<您可以引用您创建的
config
文件并仍然运行脚本的唯一方法是您需要将配置文件放在所需的路径上。有两种方法可以做到这一点

  • 如果
    config
    几乎总是固定的,您不需要更改它,请在需要运行脚本的主机上本地设置
    config
    ,然后将
    config
    文件的绝对路径放入脚本中,并确保运行脚本的用户有权访问它

  • 如果每次需要运行脚本时都需要发送配置文件,那么在发送和调用脚本之前,只需
    scp
    文件即可

    scp property.config user@remote_machine:/usr/property.config
    ssh user@remote_machine "bash -s" < /usr/test.sh
    
  • test.sh

    #!bin/bash
    #do not use this line source /usr/property.config
    echo "$testName"
    
  • 现在,您可以按照John的建议运行命令:

    ssh user@remote_machine "bash -s" < <(cat /usr/property.config /usr/test.sh)
    

    sshuser@remote_machine“bash-s”scp
    ?scp是将文件发送到目标的最可靠的方法。还有其他选择,比如通过
    stdin
    ssh)发送user@remote_host“cat>/path/to/config.file;/remote/command”
    ),但scp是最可靠的方法。谢谢。。配置文件包含更多属性值集。它就像一个包装器配置文件,定义配置文件中的所有值并在此基础上执行脚本。将所有这些值作为参数传递似乎有点困难bash:意外标记附近的语法错误
    (')。在运行ssh时获取此错误user@remote_machine“bash-s”<(cat source/usr/property.config/usr/test.sh)`@anishsane第二行应该是
    sshuser@remote_machine“bash-s”scp property.configuser@remote_machine:/usr/property.config;sshuser@remote_machine“bash-s”
    但是如果你真的想把它们组合成一个命令,那么你必须将
    cat
    配置
    bash
    脚本的输出放在一个文件中(正如@John所提到的)这需要对
    test.sh
    的源代码进行一些小的更改,如果您愿意的话,我也可以分享这个方法,如果您对更改它感兴趣的话。谢谢anand。我让它按预期工作,但我仍然很好奇能得到更多的选项。请分享您的方法。@Jugi我已经更新了答案来描述其他技术。谢谢..M最后一个问题,如何传递论点:(?
    testName=xxx
    testPwd=yyy
    
    #!bin/bash
    #do not use this line source /usr/property.config
    echo "$testName"
    
    ssh user@remote_machine "bash -s" < <(cat /usr/property.config /usr/test.sh)