# How to determine remote host IP for SSH session



## dmmiller2k

If I ssh into a NetBSD machine and get a [bash] shell prompt, how do I find out my remote IP address?

When I ssh in from work, I go through our proxy server, so I don't really know the external IP address I appear to be connecting from.

Some UNIX-like systems I've used displayed this in the output to the, 'finger username' command,

$ finger `whoami`
Login: XXX Name: Xavier X. Xylophone
Directory: /arpa/xz/x/xxx Shell: /usr/pkg/bin/bash
Last login Tue Apr 8 15:12 (UTC) from 1.2.3.4:5 on ttyrp
Mail last read Tue Apr 8 15:06 2008 (UTC)
Mail forwarded to: "|IFS=' '&&exec /usr/pkg/bin/procmail -f-||exit 75 #xxx"
No Plan.

but on one particular [NetBSD] system, it only shows me the tty I am using:

$ finger `whoami`
Login: XXX Name: Xavier X. Xylophone
Directory: /arpa/xz/x/xxx Shell: /usr/pkg/bin/bash
Last login Tue Apr 8 15:12 (UTC) on ttyrp
Mail last read Tue Apr 8 15:06 2008 (UTC)
Mail forwarded to: "|IFS=' '&&exec /usr/pkg/bin/procmail -f-||exit 75 #xxx"
No Plan.

How do I query the remote address, given an active tty, e.g. 'ttyrp'?

Thanks,
David


----------



## lotuseclat79

Hi David,

Try the command ifconfig to see what results are printed out on your terminal screen and look for inet address or where P-t-P (for dialup on ppp0 or some other identifier for eth0 if not on dialup connection) is followed by the remote ip address. 

Also, run ifconfig locally to see what you get. Use ipconfig for Windows if that is your local computer.

-- Tom


----------



## dmmiller2k

Thanks, but the ifconfig command only shows the IP addresses of the local machine's network adapters, not the remote IP address connected to my tty, say, 'ttyp0'.

If I run the finger command with no parameters and grep for my username, I get something like this:

$ finger | fgrep `whoami`
xxx Xavier X. Xylophone p0 - Thu 18:24

which shows that I am using the tty named, 'ttyp0'. Now, I would like to see the remote IP address of the host connected to 'ttyp0'. Isn't there a way to do this?


----------



## lotuseclat79

Can you issue the ifconfig command while logged into the remote machine - you should not be getting local ip addresses at a remote location.

-- Tom


----------



## Squashman

Which freeshell server are you logging in to directly and why aren't you just using the Round Robin tty address?

Do you want to know your Public IP addrress. You can go to whatismyip.com for that.

I thought he had lastlog installed. I can get the man page for it but when I try to execute it, it tells me it can't be found. So maybe he doesn't want anyone using that.


----------



## fenderfreek

I understand what you're going for, but the closest suggestion I can toss in is to check the output of the "last" command, or the "who" command. The output will show your hostname. Here's a little one-liner I threw together and tested real quick that may give you what you need, assuming your IP can be resolved to a hostname. If it doesn't behave as expected, you can just look at the output of one of those commands and grep it by hand.



Code:


% nslookup `who | grep *username* | sed 's/.*(\(.*\)).*/\1/g'`
Server:  ns4.ncsu.edu   <-Ignore these two lines. They are just about the NS
Address:  152.1.1.161

Name:    myhostname.ncsu.edu  <- This should be your hostname (FQDN)
Address:  152.7.7.123  <- This should be your IP

Just put your username in the appropriate spot, and try it out. I ran this on a Solaris box, so you *might* need to tweak it for a BSD system.


----------

