bash: Get IP address of SSH remote user

Today I needed to find out the IP address of the currently logged on user (e.g. via SSH) on a Linux system to use it in a bash script. After searching the web for a simple tool for this task for quite some time, I finally came up with the following bash script to get me the needed information. Is there any way to obtain the IP address in a simpler way by using built in Linux tools?

  1. HOST=`who am i | sed -r "s/.*\((.*)\).*/\\1/"`
  2. IP=`host $HOST | sed -r "s/.* has address (.*)/\\1/"`

Über uns Stefan

Polyglot Clean Code Developer

7 Kommentare

  1. I think you can use the command „last“ to that.
    With the parameter -i, you can always print the IPs instead of the hostnames.

    i.e.
    last -i | grep „still logged in“ | awk ‚{print $3}‘

    Note that this gives you a list of ALL currently logged in users.

  2. Just type:

    PGNvZGU+d2hvIC0taXBzPC9jb2RlPg==
  3. who –ips

  4. echo $SSH_CLIENT | cut -d ‚ ‚ -f 1
    echo $SSH_CONNECTION | cut -d ‚ ‚ -f 1

    (c/o http://stackoverflow.com/questions/996231)

  5. I think you can use the command „last“ to that.
    With the parameter -i, you can always print the IPs instead of the hostnames.

    i.e.
    last -i | grep „still logged in“ | awk ‚{print $3}‘

    Note that this gives you a list of ALL currently logged in users.

  6. Even if your post is old, just wanted to say that your method:

    1. HOST=`who am i | sed -r „s/.*\((.*)\).*/\\1/“`
    2.IP=`host $HOST | sed -r „s/.* has address (.*)/\\1/“`

    should give you the best results along with the advices from Amy.

  7. Pingback:networkfailure.info

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert.

To create code blocks or other preformatted text, indent by four spaces:

    This will be displayed in a monospaced font. The first four 
    spaces will be stripped off, but all other whitespace
    will be preserved.
    
    Markdown is turned off in code blocks:
     [This is not a link](http://example.com)

To create not a block, but an inline code span, use backticks:

Here is some inline `code`.

For more help see http://daringfireball.net/projects/markdown/syntax