SSH - 连接&执行:“echo $ SSH_CONNECTION” awk'{print $ 1}'“ [英] SSH - Connect & Execute : "echo $SSH_CONNECTION | awk '{print $1}'"

查看:429
本文介绍了SSH - 连接&执行:“echo $ SSH_CONNECTION” awk'{print $ 1}'“的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的bash(shell?)脚本。

  command =ssh root @ $ ip; 
command2 =\ls /; bash\;
xfce4-terminal -x sh -c$ command $ command2; bash

到服务器并执行

  ls / 

工作正常。



但不是 ls / ...

I想执行这个命令:

  echo $ SSH_CONNECTION | awk'{print $ 1}'

我替换了 ls / 与上面的代码,但是很快连接,
它只是打印一个空白行。



根据我的理解,代码在到达服务器之前正在本地执行,因为没有转义东西。



如果我手动将此代码粘贴到远程服务器上。

  echo $ SSH_CONNECTION | awk'{print $ 1}'

它的工作原理很好。打印出正确打印出来的内容。



所以问题是:我的代码中反斜杠在哪里?


$ b $我知道这听起来就像是尝试一堆反斜杠..
,直到有用。



我尝试了很多方法。我甚至尝试三重和六元反斜杠来逃避事情。



更新



是不够的。

它仍然只能在连接时打印出一个空行。

  command = ssh root @ $ ip; 
command2 =\echo \ $ SSH_CONNECTION | awk'{print \ $ 1}'; bash\;
xfce4-terminal -x sh -c$ command $ command2; bash

strong>更新2



从其中一个答案。



下面的代码可以,但是它看起来对我的眼睛不轻,也许只是我的想法,因为我不习惯执行和右边的管道?

  command =ssh -t root @ $ ip; 
command2 =\awk'{print \\\ $ 1}'<< \\\ $ SSH_CONNECTION; exec \\\ $ SHELL\
xfce4-terminal -x sh -c$ command $ command2; bash

更新3



从答案..

  command2 ='echo \\ $ SSH_CONNECTION\| awk''{print \ $ 1}''''; exec \ $ SHELL'

也似乎工作正常。



尽管信息被赋予为执行资源少。我仍然在寻找一个没有exec命令的解决方案,因为exec命令提醒我的php这不是轻的东西..所以也许只是感觉



更新4:



显示exec \ $ SHELL不是代码的一部分。这只是一个替代bash命令来保持登录的ssh。



尽管信息正在说它比bash
的资源消耗少命令..将来要研究。



现在这似乎是最后的结果。

  command2 ='echo \\ $ SSH_CONNECTION\| awk'''{print \ $ 1}''''; bash'

看起来非常合理,从左到右只是管道。



更新5



最终的代码是:

  command =ssh -p 2201 -t root @ $ ip; 
command2 ='echo \\ $ SSH_CONNECTION\| awk'''{print \ $ 1}''''; bash'
xfce4-terminal -x sh -c$ command $ command2; bash


解决方案

你必须逃脱两次:一次SSH,一次为shell命令你给 xfce4-terminal 。我已经使用 xterm 而不是 xfec4-terminal 进行测试,但它应该是一样的:

  $ cmd1 ='ssh -t root @ as'
$ cmd2 =\awk'{print \\ \ $ 1}'<<< \\\ \\\ $ SSH_CONNECTION\\\; exec \\\ $ SHELL\
$ xfce4-terminal -x sh -c$ cmd1 $ cmd2

我已经添加了 -t 来分配一个伪终端,我使用这个字符串而不是 echo 和一个管道。



而不是在子shell中生成Bash,我使用 exec $ SHELL



cmd2 中的三重反斜杠的替代方法是单引号,但是要获得单引号的单引号,您必须使用笨重的'''

  cmd2 ='awk'''{print \ $ 1}'''<< \\ $ SSH_CONNECTION\ exec \ $ SHELL'


Here's my bash (shell?) script.

command="ssh root@$ip";
command2="\"ls /;bash\"";
xfce4-terminal -x sh -c "$command $command2; bash"

connects to the server and executes the command of

 ls /

works just fine.

But instead of ls /..
I want to execute this command:

 echo $SSH_CONNECTION | awk '{print $1}'

I replaced " ls / " with the code above, but soon as it connects, it simply prints a blank line.

Based on my understanding, the code is being executed locally before it reaches the server because stuff is not escaped.

If I manually paste this code on my remote server..

   echo $SSH_CONNECTION | awk '{print $1}'

it works just fine. Prints out exactly what it should be printing out.

So the question is: where do the backslashes go in my code ?

I know it sounds like simply trying bunch of backslashes.. until something works.

I tried many ways. I even tried triple and sixtuple backslashes to escape things.

Update

This is not sufficient.
It still only prints out a blank line soon as it connects.

command="ssh root@$ip";
command2="\"echo \$SSH_CONNECTION | awk '{print \$1}';bash\"";
xfce4-terminal -x sh -c "$command $command2; bash"

Update 2

from one of the answers..

code below works okay but it looks "un-light" to my eyes or maybe just my mind because I am not used to exec and right to left piping ?

command="ssh -t root@$ip";
command2="\"awk '{ print \\\$1 }' <<< \\\$SSH_CONNECTION; exec \\\$SHELL\""
xfce4-terminal -x sh -c "$command $command2; bash"

Update 3

from the answers..

command2='"echo \"\$SSH_CONNECTION\" | awk '"'"'{ print \$1 }'"'"'; exec \$SHELL"'

also seems to be working okay.

although info is being given as "exec" being less resource consuming.. i am still looking for a solution without the "exec" command because "exec" command reminds me of "php" which is not light stuff.. so maybe it is just perception

Update 4:

Turns out "exec \$SHELL" was not part of the code. it was simply a replacement for the "bash" command to stay logged in in ssh.

Although info is being said it is less resource consuming than the bash command.. it is to be studied in the future.

for now this seems to be the final result.

command2='"echo \"\$SSH_CONNECTION\" | awk '"'"'{ print \$1 }'"'"';bash"'

it looks very reasonable simply piping from left to right..

Update 5

The final code is:

command="ssh -p 2201 -t root@$ip";
command2='"echo \"\$SSH_CONNECTION\" | awk '"'"'{ print \$1 }'"'"';bash"'
xfce4-terminal -x sh -c "$command $command2; bash"

解决方案

You have to escape twice: once for SSH, once for the shell command you give to xfce4-terminal. I've tested this with xterm instead of xfec4-terminal, but it should be the same:

$ cmd1='ssh -t root@as'
$ cmd2="\"awk '{ print \\\$1 }' <<< \\\"\\\$SSH_CONNECTION\\\"; exec \\\$SHELL\""
$ xfce4-terminal -x sh -c "$cmd1 $cmd2"

I've added -t to allocate a pseudo-terminal, and I use a here-string instead of echo and a pipe.

Instead of spawning Bash in a subshell, I'm using exec $SHELL.

An alternative to triple backslashes in cmd2 is to single-quote it, but to get a single quote into a single-quoted string, you have to use the unwieldy '"'"':

cmd2='"awk '"'"'{ print \$1 }'"'"' <<< \"\$SSH_CONNECTION\"; exec \$SHELL"'

这篇关于SSH - 连接&amp;执行:“echo $ SSH_CONNECTION” awk'{print $ 1}'“的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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