ssh - echo $SSH_CLIENT 在初始连接时执行时返回空 [英] ssh - echo $SSH_CLIENT returns empty when executed on initial connect

查看:78
本文介绍了ssh - echo $SSH_CLIENT 在初始连接时执行时返回空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当命令附加到 ssh 连接时..

when the command is attached to the ssh connection..

ssh -p $port -t root@$ip "echo $SSH_CLIENT; bash"

一旦它连接起来......似乎有一个空行的回声.
换句话说,只是在服务器上回显了一个新行.

once it connects... there seems to be an echoing of a empty line.
in other words just a new line is being echo-ed on the server.

但是如果我手动粘贴

 echo $SSH_CLIENT

效果很好.

这是为什么?有什么解决办法?

why is this ? what can be the solution ?

推荐答案

ssh 命令运行之前,变量正在本地 shell 中扩展.由于它在本地 shell 上未设置,因此您什么也没有得到,只是 echo 在远程 shell 上执行时默认生成的换行符.

The variable is being expanded in the local shell, before the ssh command runs. As it is unset on the local shell, you are not getting anything, just a newline that echo produces by default, while being executed on remote shell.

您需要以任何通常的方式对 SSH_CLIENT 前面的 $ 进行转义:

You need to escape the $ in front of SSH_CLIENT in any of the usual manners:

ssh ... 'echo $SSH_CLIENT; bash'
ssh ... "echo \$SSH_CLIENT; bash"

请注意,虽然在这种情况下不是绝对必要的,但您应该引用变量扩展以防止分词和路径名扩展.

Note that, although it is not strictly necessary in this case, you should quote your variable expansions to prevent word splitting and pathname expansion.

这篇关于ssh - echo $SSH_CLIENT 在初始连接时执行时返回空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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