Today I needed to find out the IP address of the currently logged on user (e.g. via SSH) on a Linux system to use it in a bash script. After searching the web for a simple tool for this task for quite some time, I finally came up with the following bash script to get me the needed information. Is there any way to obtain the IP address in a simpler way by using built in Linux tools?
HOST=`who am i | sed -r "s/.*\((.*)\).*/\\1/"`
IP=`host $HOST | sed -r "s/.* has address (.*)/\\1/"`
I think you can use the command “last” to that.
With the parameter -i, you can always print the IPs instead of the hostnames.
i.e.
last -i | grep “still logged in” | awk ‘{print $3}’
Note that this gives you a list of ALL currently logged in users.
Just type:
who --ips
who –ips
echo $SSH_CLIENT | cut -d ‘ ‘ -f 1
echo $SSH_CONNECTION | cut -d ‘ ‘ -f 1
(c/o http://stackoverflow.com/questions/996231)
I think you can use the command “last” to that.
With the parameter -i, you can always print the IPs instead of the hostnames.
i.e.
last -i | grep “still logged in” | awk ‘{print $3}’
Note that this gives you a list of ALL currently logged in users.
Even if your post is old, just wanted to say that your method:
1. HOST=`who am i | sed -r “s/.*\((.*)\).*/\\1/”`
2.IP=`host $HOST | sed -r “s/.* has address (.*)/\\1/”`
should give you the best results along with the advices from Amy.
Pingback:networkfailure.info