Find the IP address of the client in an SSH session

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

    Improving on a prior answer. Gives ip address instead of hostname. --ips not available on OS X.

    who am i --ips|awk '{print $5}' #ubuntu 14
    

    more universal, change $5 to $6 for OS X 10.11:

    WORKSTATION=`who -m|awk '{print $5}'|sed 's/[()]//g'`
    WORKSTATION_IP=`dig +short $WORKSTATION`
    if [[ -z "$WORKSTATION_IP" ]]; then WORKSTATION_IP="$WORKSTATION"; fi
    echo $WORKSTATION_IP
    

提交回复
热议问题