Find the IP address of the client in an SSH session

后端 未结 19 1014
刺人心
刺人心 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:30

    an older thread with a lot of answers, but none are quite what i was looking for, so i'm contributing mine:

    sshpid=$$
    sshloop=0
    while [ "$sshloop" = "0" ]; do
            if [ "$(strings /proc/${sshpid}/environ | grep ^SSH_CLIENT)" ];
    then
                    read sshClientIP sshClientSport sshClientDport <<< $(strings /proc/${sshpid}/environ | grep ^SSH_CLIENT | cut -d= -f2)
                    sshloop=1
            else
                    sshpid=$(cat /proc/${sshpid}/status | grep PPid | awk '{print $2}')
                    [ "$sshpid" = "0" ] && sshClientIP="localhost" && sshloop=1
            fi
    done
    

    this method is compatible with direct ssh, sudoed users, and screen sessions. it will trail up through the process tree until it finds a pid with the SSH_CLIENT variable, then record its IP as $sshClientIP. if it gets too far up the tree, it will record the IP as 'localhost' and leave the loop.

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

    One thumb up for @Nikhil Katre's answer :

    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

    The one using who or pinky did what is basically asked. But But But they don't give historical sessions info.

    Which might also be interesting if you want to know someone who has just logged in and logged out already when you start this checking.

    if it is a multiuser system. I recommand add the user account you are looking for:

    last | grep $USER | head
    

    EDIT:

    In my case, both $SSH_CLIENT and $SSH_CONNECTION do not exist.

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

    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.

    Extract the IP:

    $ echo $SSH_CLIENT | awk '{ print $1}'
    1.2.3.4
    $ echo $SSH_CONNECTION | awk '{print $1}'
    1.2.3.4
    
    0 讨论(0)
  • 2020-12-12 11:31

    Try the following to get just the IP address:

    who am i|awk '{ print $5}'
    
    0 讨论(0)
  • 2020-12-12 11:34

    You can get it in a programmatic way via an SSH library (https://code.google.com/p/sshxcute)

    public static String getIpAddress() throws TaskExecFailException{
        ConnBean cb = new ConnBean(host, username, password);
        SSHExec ssh = SSHExec.getInstance(cb);
        ssh.connect();
        CustomTask sampleTask = new ExecCommand("echo \"${SSH_CLIENT%% *}\"");
        String Result = ssh.exec(sampleTask).sysout;
        ssh.disconnect();   
        return Result;
    }
    
    0 讨论(0)
  • 2020-12-12 11:34
    netstat -tapen | grep ssh | awk '{ print $10}'
    

    Output:

    two # in my experiment

    netstat -tapen | grep ssh | awk '{ print $4}' 
    

    gives the IP address.

    Output:

    127.0.0.1:22 # in my experiment
    

    But the results are mixed with other users and stuff. It needs more work.

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