使用配置文件的ssh命令在远程机器中执行shell脚本

Jug*_*ugi 3 linux ssh bash shell scp

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

ssh user@remote_machine "bash -s" < /usr/test.sh
Run Code Online (Sandbox Code Playgroud)

shell脚本在远程计算机中正确执行.现在我在脚本中做了一些更改,以从配置文件中获取一些值.该脚本包含以下行,

#!bin/bash
source /usr/property.config
echo "testName"
Run Code Online (Sandbox Code Playgroud)

property.config:

testName=xxx
testPwd=yyy
Run Code Online (Sandbox Code Playgroud)

现在如果我在远程机器上运行shell脚本,我没有收到这样的文件错误,因为/usr/property.config在远程机器中不可用.

如何将配置文件与shell脚本一起传递到远程机器中执行?

ana*_*and 5

只有这样您才能引用config您创建的文件并仍然运行脚本,您需要将配置文件放在所需的路径上,有两种方法可以执行此操作.

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

  2. 如果每次要运行该脚本时都需要发送配置文件,那么scp在发送和调用脚本之前可能只是简单的文件.

    scp property.config user@remote_machine:/usr/property.config
    ssh user@remote_machine "bash -s" < /usr/test.sh
    
    Run Code Online (Sandbox Code Playgroud)

编辑

根据要求,如果你想在一行强行完成,这就是它的完成方式:

现在您可以像John建议的那样运行命令:

ssh user@remote_machine "bash -s" < <(cat /usr/property.config /usr/test.sh)
Run Code Online (Sandbox Code Playgroud)