0

I have a workstation connected to several internal networks, with a different static IP address on each of them. Through one (?) of these networks, I can connect to a server over ssh. How can I tell which of my workstation's IP addresses is used to connect to the server? There's no NAT between the two computers as far as I can tell.

1
  • 2
    Run netstat and look for the foreign address of the server. When you find it look at the local address to determine which local address is connecting to the server.
    – joeqwerty
    Dec 9, 2015 at 17:33

4 Answers 4

5

On a Linux machine you can find the source IP that will be used for a connection with ip route get $destination_ip

This is the primary IP of the interface that is directly connected with the next hop.

1

Via ssh?

server$ echo $SSH_CLIENT
1234:5678:90a:bc::805f:c436 58642 22
server$ 
0

If you're on Windows, use tracert <destination IP> from your source computer to find the route it's traversing to the destination.

4
  • OSX and Linux, but this is still good to know.
    – Dan
    Dec 9, 2015 at 18:01
  • A network may be designed in such a way that you don't get correct information from this. For instance, in a network which uses MPLS, by default, the routers will not show up in the trace.
    – Ron Maupin
    Dec 9, 2015 at 18:28
  • Some firewalls (e.g. Cisco PIX/ASA) won't show up by default, either. Dec 9, 2015 at 18:31
  • @Dan, sorry, I missed your tag above. Thank you to the others as well-- it's good to know the possible gotchas involved in this one. Dec 10, 2015 at 20:09
0

You can use traceroute and see from which IP it goes. Or after ssh'ing to the server check:

who am i

the last field is your IP or fqdn if it has a reverse DNS entry.

Another option is, after ssh'ing to your server, do a :

tcpdump -nnpi any icmp

And from another terminal from your workstation ping that host. You'll see in the tcpdump shell icmp packets from your workstation's IP

Now that i think about it, you dont even need access to the server, you can launch tcpdump on your host checking for packets for a choosen unused port, let's say 121345, on one root shell in your workstation:

tcpdump -nnpi any port 12345 

Then from a normal terminal window on your workstation:

telnet server 12345

or:

nc server 12345

On the tcpdump shell you'll see the IP you're using to connect to the server.

2
  • I already know my fqdn; is there a way to get who to return my IP address?
    – Dan
    Dec 9, 2015 at 18:06
  • from who's manual page it seems not. But you can just resolve what who says (assuming your DNS is correct) whith host YOUR_FQDN. It will resolve in one IP. Alternatively you can grep for your username on /var/log/secure (in redhat at least). I updated my answer with yet another way to get your IP
    – Fredi
    Dec 9, 2015 at 18:18

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .