Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the IP address from $SSH_CLIENT

Tags:

bash

$SSH_CLIENT has IP address with some port info, and echo $SSH_CLIENT gives me '10.0.40.177 52335 22', and Running

if [ -n "$SSH_CONNECTION" ] ; then for i in $SSH_CLIENT do echo $i done fi

gives me

  • 10.0.40.177
  • 52335
  • 22

And I see the first element is the IP address.

Q : How can I get the first element of $SSH_CLIENT? ${SSH_CLIENT[0]} doesn't work.

like image 863
prosseek Avatar asked Feb 09 '10 16:02

prosseek


2 Answers

sshvars=($SSH_CLIENT)
echo "${sshvars[0]}"

or:

echo "${SSH_CLIENT%% *}"
like image 91
Ignacio Vazquez-Abrams Avatar answered Oct 07 '22 03:10

Ignacio Vazquez-Abrams


For strings, as is the case here, the <<< operator may be used:

$ read ipaddress outport inport <<< $SSH_CLIENT

See e.g: Linux Bash: Multiple variable assignment. Don't do this with binary input though: Is there a binary safe <<< in bash?

like image 28
Emil Avatar answered Oct 07 '22 01:10

Emil