Get the interface and ip address used to connect to a specific host (ip)

444

Solution 1

You can try "route" to print you're routing table which will show you the destination networks and gateways for your various interfaces.

If the IP addresses don't help you determine the interface easily, run "traceroute," after running route, look at the first hop and compare it with the associated gateways from "route," and voila - that's your interface.

#route
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
145.67.4.0      switch47        255.255.255.0     U     0      0      eth0
192.168.1.0     localrouter     255.255.255.0     U     0      0      eth1

#traceroute <destination>
1    switch47.ba.foo.com   (145.67.4.1)    0.5 ms  .....
2    xxxxx.xxx.xxxx        (xxx.xxx.xxx.xxx)   0.Xms. ...

Solution 2

route -n get www.yahoo.com

I am running os x 10.6.8

Solution 3

You can use ip route get to find and output the route which would be used for a specified destination. The output should include the interface and source address.

% ip route get 8.8.8.8
8.8.8.8 via 172.16.4.1 dev eth1  src 172.16.4.36

Solution 4

I'm not sure if this is correct, but netstat might do it. It shows local address of established connections.

# netstat -nt
Proto Recv-Q Send-Q Local Address           Foreign Address         State      
...
tcp        0      0 192.168.1.61:44114      64.34.119.101:80        ESTABLISHED
tcp        0      0 192.168.1.61:36036      107.21.205.201:80       ESTABLISHED
Share:
444

Related videos on Youtube

brainkz
Author by

brainkz

Updated on September 18, 2022

Comments

  • brainkz
    brainkz almost 2 years

    I am currently trying to translate the following Verilog command into Simulink:

    assign A = {{14{B[15]}},B[15:0],2'b00};
    

    This line sign-extends the 16-bit vector and multiplies it by 4. For example, given the 16-bit input B = 0100110110101101 the output would be 32-bit A = 00000000000000010011011010110100:

    The Simulink this operation can be performed by the following blocks:

    enter image description here

    But I thought, there might be an easier way to do this. Any ideas?

  • Nicolò Fuccella
    Nicolò Fuccella about 12 years
    I did end up using route. Thank you. Here was my final command: ifconfig `route get <host> | grep "interface: " | sed "s/[^:]*: \(.*\)/\1/"` | grep "inet " | sed "s/.*inet \([0-9.]*\) .*/\1/". This works, but any suggestions / optimizations are welcome.
  • Univ426
    Univ426 about 12 years
    Ahh ok, I was just gonna say, I would really recommend mgorven's approach listed here, but if you don't have ip on Mac OS, I'm glad this helps.
  • bahamat
    bahamat about 12 years
    This is the correct definitive way to determine which local IP was used.
  • Amos Shapira
    Amos Shapira almost 8 years
    That's what I was looking for when I got here. Good job.