views:

1319

answers:

4

I have a script that is to be run by a person that logs in to the server with ssh.

Is there a way to find out automatically what ip is the user connecting from.

Of course, I could ask the user (it is a tool for programmers, so no problem with that), but it would be cooler if I just found out.

A: 

Usually there is a log entry in /var/log/messages (or similar, depending on your OS) which you could grep with the username.

Daniel Schneller
+7  A: 

Check if there is an environment variable called:

SSH_CLIENT

OR

SSH_CONNECTION

(or any other environment variables) which gets set when the user logs in. Then process it using the user login script.

nolim1t
A: 

Assuming he opens an interactive session (i. e. allocates a pseudo terminal) and you have access to stdin , you can call an ioctl on that device to get the device number (/dev/pts/4711) and try to find that one in /var/run/utmp (where there will also be the username and the ip the connection originated from).

mihi
+2  A: 

You could use the command:

server:~# pinky

that will give to you somehting like this:

Login      Name                 TTY    Idle   When                 Where 

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