Find the IP address of the client in an SSH session

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

提交回复
热议问题