Coder Perfect

In an SSH session, find the client’s IP address.

Problem

I have a script that needs to be run by someone who uses SSH to connect to the server.

Is there a way to automatically determine the user’s IP address when they connect?

Of course, I could ask the user (it is a programming tool, after all), but it would be more interesting if I just found out.

Asked by flybywire

Solution #1

See if there’s a variable called: in the environment.

$SSH_CLIENT 

OR

$SSH_CONNECTION

(or any other environment variables) that are set by the user when they log in. Then use the user login script to handle it.

Extract the IP:

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

Answered by nolim1t

Solution #2

You can use the following command:

server:~# pinky

that will provide you with something similar to this:

Login      Name                 TTY    Idle   When                 Where 

root       root                 pts/0         2009-06-15 13:41     192.168.1.133

Answered by vncprado

Solution #3

To acquire simply the IP address, try the following:

who am i|awk '{ print $5}'

Answered by AlexP

Solution #4

On your Linux system, type the following command:

who

Answered by Bangar

Solution #5

 who | cut -d"(" -f2 |cut -d")" -f1

Answered by danilo

Post is based on https://stackoverflow.com/questions/996231/find-the-ip-address-of-the-client-in-an-ssh-session