当我在Linux服务器中运行脚本时,如下所示:
./myscript \"hello\"然后,脚本以"hello"的形式接收参数。现在,我希望能够通过ssh从另一个主机远程运行这个脚本。如果ssh连接是Linux到Linux,下面的工作如下:
ssh remote-host ./myscript \\\"hello\\\"但是,如果ssh连接是Windows 10到Linux的话。以上操作无效,远程脚本接收参数为\hello“--注意额外的反斜杠和缺少的双引号。
ssh remote-host ./myscript \\\"hello\\\"
ssh remote-host ./myscript ^"hello^"
ssh remote-host ./myscript ""hello""
ssh remote-host ./myscript '"hello"'我能想到的唯一解决办法是创建另一个shell脚本,它包含:
./myscript \"hello\"然后将脚本scp到远程Linux服务器,并在那里执行它。那么,我是否有办法恰当地引用我的论点呢?
发布于 2022-02-17 13:34:09
您可以使用以下方便的包装脚本ssh.sh
cat <<'EOF' > ssh.sh
#!/bin/bash
function escape() {
for arg in "$@"; do
printf "%q " "$arg"
done
}
ssh $(escape "$@")
EOF
chmod +x ssh.sh然后,您可以通过ssh.sh安全地调用ssh,而不必担心逃脱。
例如,
./ssh.sh host sh -c 'ls /'与标准的ssh相比,
ssh host sh -c 'ls /'不起作用,它需要转换为
ssh host sh -c \'ls /\'https://stackoverflow.com/questions/51501339
复制相似问题