简体   繁体   English

ssh remote命令作为不同的用户

[英]ssh remote command as different user

Why does the awk portion of this command not get processed. 为什么此命令的awk部分未得到处理。 It works when I run this directly as user1 . 当我直接以user1身份运行它时,它可以工作。

[zimmerman@SERVER1 check]# su  user1 -c "ssh -i /home/user1/.ssh/id_rsa -q user1@SERVER2 df -h /dir1/dir2 | awk '{print $5}'"

Filesystem            Size  Used Avail Use% Mounted on
/dev/sdf1            1008G  204M  957G   1% /dir1/dir2

When you combine all these stuff together, you need to make sure you escape all the important characters that you don't want to evaluate now, but later one. 将所有这些内容组合在一起时,您需要确保逃避了所有您现在不想评估的重要角色,但稍后再评估。 The $ is a good example of that. $是一个很好的例子。

$ su user1 -c "ssh -i [...] -q user1@SERVER2 df -h /dir1/dir2 | awk '{print $5}'"

evaluates the $5 in the current bash, which is what you don't want. 计算当前bash中的$5 ,这是您不想要的。 You can see it, if you run simply 如果您简单运行,就可以看到它

$ echo "ssh -i [...] -q user1@SERVER2 df -h /dir1/dir2 | awk '{print $5}'"
ssh -i [...] -q user1@SERVER2 df -h /dir1/dir2 | awk '{print }'

So to fix it, you need to escape the $ sign, such as 因此,要解决此问题,您需要转义$符号,例如

$ echo "ssh -i [...] -q user1@SERVER2 df -h /dir1/dir2 | awk '{print \$5}'"
ssh -i [...] -q user1@SERVER2 df -h /dir1/dir2 | awk '{print $5}'

TL;DR : so the final commandline you should use is TL; DR :因此,您应该使用的最后一个命令行是

su  user1 -c "ssh -i /home/user1/.ssh/id_rsa -q user1@SERVER2 df -h /dir1/dir2 | awk '{print \$5}'"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM