在递归 ssh 命令中引用 bash 和 perl [英] Quoting in bash and perl in recursive ssh command
问题描述
我正在编写一个 perl 脚本来使用 ssh 登录到服务器并在服务器上执行一些 shell 命令.问题是该服务器只能通过首先登录到另一台服务器来访问.(我使用的是带 ssh 密钥的无密码登录).
I am writing a perl script to login in to a server with ssh and do some shell commands on the server. The problem is that the server is only accessible by first logging into another server. (I am using password-less login with ssh keys).
以下 bash 脚本运行正常,并说明了问题:
The following bash script is working correctly, and illustrates the problem:
#! /bin/bash
server1="login.uib.no"
server2="cipr-cluster01"
ssh "$server1" "ssh $server2 "echo \"\$HOSTNAME\"""
它将正确的主机名打印到我的屏幕上:cipr-cluster01
.但是,当尝试在 Perl 中做同样的事情时:
It prints the correct host name to my screen: cipr-cluster01
. However, when trying to do same thing in Perl:
my $server1="login.uib.no";
my $server2="cipr-cluster01";
print qx/ssh "$server1" "ssh $server2 "echo \"\$HOSTNAME\"""/;
我得到以下输出:login.uib.no
.所以我猜,perl 脚本的引用存在一些问题..
I get the following output: login.uib.no
. So I guess, there is some problems with the quoting for the perl script..
推荐答案
qx
与双引号类似.你必须多反斜杠:
qx
works like double quotes. You have to backslash some more:
print qx/ssh "$server1" "ssh $server2 "echo \\"\$HOSTNAME\\"""/;
使用单引号可能会大大简化命令:
Using single quotes might simplify the command a lot:
print qx/ssh "$server1" 'ssh $server2 "echo \$HOSTNAME"'/;
这篇关于在递归 ssh 命令中引用 bash 和 perl的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!