IP details for Unix/Linux login clients?

Hi there,

I am wondering if by logging in to a unix system, if it is possible to get the IP address of the machine I am connecting FROM.

I know how I can do this using the name server, but is this possible without a host lookup?:confused:

Thanks,

-ghoti

If you were connected via ssh/telnet running "netstat -n -e" from the server would show what user ids and ip addresses are connected to the server.

"netstat -e" would show user names and hostnames connecting to the server.

The ipaddress and user details will also go into one of the system's log files although I you might not have access.

It may sound silly but why do you need to know this?

Andy Hibbins :slight_smile:

Thanks for the help Andy, but I had got that far already.

The problem is this:

we are working using a unix box, accessed from several windows systems, usually running Hummingbird Exceed. As some of the technicians need to access the server from many locations worldwide, I need to set the $DISPLAY variable using an IP address rather than a hostname, as not all the IP's are in the DNS.

I had initially thought that I could use 'netstat |grep X' to isolate the connections using an Xserver, then do the same using netstat -n to get the ip number, but this fails with multiple connections, as it is very difficult to differentiate using a script.

Cheers for the help tho...

-ghoti

On my systems, <B>netstat -en </B> provides the kernel level user of the sockets (tcp, unix) , not the application level user (i.e john, harry, sally, root).

Working to automatically set the X display parameter is normally not performed with shell utilities such as netstat; but passed as environmental varialble during remote login. This is a function of the remote login protocol.

Some versions of telnet automatically set the DISPLAY variable, so that when you login remotely, DISPLAY is set and exported for X to use. However, your problem seems to be that this works for you already, but you have DNS issues.

I think it is possible to have the remote login session pass the IP| address in the DISPLAY environment variable. There is no reason to only pass the FQDN to $DISPLAY.

As a matter of fact, if you have your remote users set the IP address in their HOSTNAME environment variable before remote login (or do this is the login script); then you will not have to worry about the DNS issues.

HOSTNAME = 111.222.333.444

DISPLAY = $HOSTNAME:0.0

Thsi is a good option, but it requires special settings on all the clients, rather than just one script..

Thanks anyway, and I'll keep looking, if I find it, I'll post my solution,

-ghoti

have you tried #who -a ???

Thanks for the suggestion, but this seems to use the host lookup again, I am really looking for just the IP information.

Cheers anyway!

You might consider setting the local users HOSTNAME environmental variable to their IP address. When they log in remotely the DISPLAY variable willl be set using HOSTNAME (which will now be the IP address). When they execute an X process it will work because the IP address is in the DISPLAY variable and not the name. This eliminates the DNS problem.

This works, of course:

export DISPLAY=111.222.333.444:0.0

(without the name :slight_smile: )

I tried to use netstat -n -e , but it gives me illegal option thingy. It so happens that Digi Unix doesn't have -e option with netstat. What is -e for? I think in order to find the IP of the user logged in one can pipe the users from who to netstat... Maybe that will work!
-Nitin :smiley:

In my SCO Unixware system I have command like last. Try something like
last | grep <your_name>| awk ' {print $3}'

well, I have found one way to determine it by loking up host tables.
This is the script I am now using:

TTY=`who am i | awk '{ print $2 }'`
HOSTNAME=`w | grep $TTY | awk '{ print $3 }'`
IP=`host $HOSTNAME | awk '{ print $4 }'`
echo $IP

Hi,

I have the same issue, it seems that the second line of ur script gives the time. Can u tell me the exact working of this script?

Also, is it possible to find who is logged on in the remote machine?

Thanks,

This script works as follows:


TTY=`who am i | awk '{ print $2 }'`
# TTY is second field of my "who am i" command

HOSTNAME=`w | grep $TTY | awk '{ print $3 }'`
#HOSTNAME is then the third field of the "w" command
#Which is the Hostname of the machine logged in FROM.

IP=`host $HOSTNAME | awk '{ print $4 }'`
#IP is then the IP number of the machine that made the 
#connection, looked up from the host tables.
#for me this is the fourth field.


echo $IP
#Print IP number to screen.

You may need to change some of the awk commands to different fields, as these are only what works on my system, Redhat 7.0.
try running it manually, line by line to find out what changes need to be made.

regards,
-gHoTi

Hi,
Thanks for the explanation.

But these things dont work for a Solaris/HP/AIX box.

The idea of passing the DISPLAY variable is good, we can know the IP Address.

Is it possible to pass the LOGNAME variable? I also need to find the user logged in the other machine. I tried exporting LOGNAME variable, as I do with DISPLAY, but it doesnt work. Unix gurus can u tell me what i should do? :confused:

Thanks

In case it is of any use to you, I have had a go on a solaris machine at work, and I came up with a rough version of an equivelant that should work for you on Solaris. I have run this on a Solaris box with success, so hopefully this will work.

NAME=`who am i | awk '{ print $1 }'`
#get NAME of person logged in

HOSTS=`finger -sfw $NAME | awk '{ print $5 }'`
#finger NAME to get name of users machine

IP=`/usr/sbin/ping -sn $HOSTS 1 1 | grep from | awk '{ print $4 }'`
#ping users machine once with 1 byte to get IP address back
#As I do not have control of what is installed on this machine
#I was limited to the commands I could use....

echo $IP

I hope this is of some help?
-gHoTi

thanks yaar.

Ill try it out and let u know if i come across any problem.