Find the IP address of the client in an SSH session

后端 未结 19 1015
刺人心
刺人心 2020-12-12 10:48

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 address the user is connecting fr

相关标签:
19条回答
  • 2020-12-12 11:20

    Assuming he opens an interactive session (that is, 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 address the connection originated from).

    0 讨论(0)
  • 2020-12-12 11:21

    Simplest command to get the last 10 users logged in to the machine is last|head. To get all the users, simply use last command

    0 讨论(0)
  • 2020-12-12 11:25

    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
    
    0 讨论(0)
  • 2020-12-12 11:26
    netstat -tapen | grep ssh | awk '{ print $4}'
    
    0 讨论(0)
  • 2020-12-12 11:27
    who am i | awk '{print $5}' | sed 's/[()]//g' | cut -f1 -d "." | sed 's/-/./g'
    
    
    export DISPLAY=`who am i | awk '{print $5}' | sed 's/[()]//g' | cut -f1 -d "." | sed 's/-/./g'`:0.0
    

    I use this to determine my DISPLAY variable for the session when logging in via ssh and need to display remote X.

    0 讨论(0)
  • 2020-12-12 11:30

    Just type the following command on your Linux machine:

    who
    
    0 讨论(0)
提交回复
热议问题