Operating System - Linux
1752810 Members
5901 Online
108789 Solutions
New Discussion юеВ

Re: who can show my one simple script to get my current login IP?

 
SOLVED
Go to solution

who can show my one simple script to get my current login IP?

I tried many times to got my current remote login ip, (I want it(xx.xx.xxx.xx) to configure one script so as to configure the fw to open service port for me automatialy, other wise I will have to type in complicate checkpoint command over and over again).

I tried like that `who |awk xxx |sed xxx`,but got fail everytime, for I am not the profession to shell script. Who can do me favor for one example?

thanks in advance
Frederick van targero
14 REPLIES 14
Stuart Browne
Honored Contributor
Solution

Re: who can show my one simple script to get my current login IP?

There's probably an easier way, but:

awk 'BEGIN { "/usr/bin/tty" | getline; tty = gensub( /^\/dev\/(.*)$/, "\\1", "g", $0 );print tty;while ( "/usr/bin/who" | getline ) { if ($2 == tty) { hostname = gensub( /\((.*)\)/, "\\1", "g", $NF ); print hostname } } }'

But that does everything. It returns the full-hostname of the current connected tty.

Don't know how it'll behave if it's a console login :P

If you want it to perform some command, you could probably change the 'print hostname' to something like 'system( "your command with argument " hostname )' instead.
One long-haired git at your service...
Stuart Browne
Honored Contributor

Re: who can show my one simple script to get my current login IP?

Blech... Don't have to do the 'tty' checking:


awk 'BEGIN { "/usr/bin/who -m" | getline; hostname = gensub( /\((.*)\)/, "\\1", "g", $NF ); print hostname }'
One long-haired git at your service...
Muthukumar_5
Honored Contributor

Re: who can show my one simple script to get my current login IP?

You can get the IP-Address of currently logged user's as,

who | awk '{ print $6 }' | tr -d '()' | uniq

Where uniq --> It is used to get only uniq ip-address if they did login two or more

Note: IF you are having remote IP-Address which is easily resolvable to hostname then only hostname will be there on who command

Easy to suggest when don't know about the problem!
Stuart Browne
Honored Contributor

Re: who can show my one simple script to get my current login IP?

Didn't think of using 'tr'.. Have had 'awk' on the brain for a few weeks now.

Change Muthukumar's, to start with 'who -m', and you've got your current tty's details.

Tis much shorter than my awk ;P
One long-haired git at your service...
Muthukumar_5
Honored Contributor

Re: who can show my one simple script to get my current login IP?

hai ,

We have more problem on who command as,

when ever our IP-Address is resolved by hosts: entry on /etc/nsswitch.conf then,

who command will return hostname not the IP-Address.

If you see who --help for

m --> -m only hostname and user associated with stdin

It will be problem. So we have to change the method as,

IP=$( who -m | awk '{ print $6 }' | tr -d '()')

if [[ $(echo $IP | grep -q '[0-9]*\.[[0-9]*\.[0-9]*\.[0-9]*') -ne 0 ]]
then

let IP_ADDR=$(host $IP | awk '{ print $4 }')
else

let IP_ADDR=$IP

fi


It is done because of getting hostname on who command.

Regards
Muthu
Easy to suggest when don't know about the problem!

Re: who can show my one simple script to get my current login IP?

Try using this
/sbin/ifconfig eth0|grep inet|awk {'print $2'}|cut -d":" -f2

regards
SK
Your imagination is the preview of your life's coming attractions
Muthukumar_5
Honored Contributor

Re: who can show my one simple script to get my current login IP?

sunil,

It is not the server's IP-Address.

It is IP-Address of logged user's one.

who -m will give the user shell's login information with the ip-address they started to login to this server.

- Muthu
Easy to suggest when don't know about the problem!
Claudio Cilloni
Honored Contributor

Re: who can show my one simple script to get my current login IP?

My Redhat 9 gives me the REMOTEHOST environment variable.

$ echo $REMOTEHOST
192.168.1.7

... the easiest way!

hope your linux distribution sets the same variable. try the 'env' command to look all your environment variables.

ciao
Claudio

Stuart Browne
Honored Contributor

Re: who can show my one simple script to get my current login IP?

Claudio, how are you connecting to your server? I can't see that environment variable when using either SSH, Telnet or X-Client's.
One long-haired git at your service...