SSH - Connect & Execute : “echo $SSH_CONNECTION | awk '{print $1}'”

落爺英雄遲暮 提交于 2019-12-08 09:38:09

问题


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"

回答1:


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"'



回答2:


Instead of dealing with all the escaping problems, you could just access the variable in another way:

Just substitute printenv SSH_CONNECTION for echo $SSH_CONNECTION. Notice that now there is no dollar sign, so the local shell will not expand the variable



来源:https://stackoverflow.com/questions/41858678/ssh-connect-execute-echo-ssh-connection-awk-print-1

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!