报价在递归ssh命令的bash和perl [英] Quoting in bash and perl in recursive ssh command

查看:102
本文介绍了报价在递归ssh命令的bash和perl的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写一个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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆