0

I am running macOS High Sierra and Mojave.

I'd like to be able to find out (for a python script) if some IP address is from the local machine or another machine. The local machine may have multiple IP addresses (such as one for ethernet and one for wifi) which both are active on the local machine (even if only one is used to connect to the LAN).

I've found a way, route get <ip-address> will show interface: lo0 in the output for any IP address that is from an interface of the local host.

Default route (ethernet):

hermione:~ gerben$ route get 192.168.1.10
   route to: hermione
destination: hermione
  interface: lo0
      flags: <UP,HOST,DONE,LLINFO,WASCLONED,LOCAL,IFSCOPE,IFREF>
 recvpipe  sendpipe  ssthresh  rtt,msec    rttvar  hopcount      mtu     expire
       0         0         0         0         0         0     16384         0 

The other ip address (Wifi, not the default route when ethernet is connected) on this host:

hermione:~ gerben$ route get 192.168.1.11
   route to: 192.168.1.11
destination: 192.168.1.11
  interface: lo0
      flags: <UP,HOST,DONE,LLINFO,WASCLONED,LOCAL,IFSCOPE,IFREF>
 recvpipe  sendpipe  ssthresh  rtt,msec    rttvar  hopcount      mtu     expire
       0         0         0         0         0         0     16384         0 

I don't know how reliable that is, but I could parse that. Update: this only works on Mojave, High Sierra will not report that it is local.

Is there a better way by which I can determine if some random IP address is in fact on a local interface? One that works on more versions of macOS?

Preferably directly in python 3, so I don't have to run external commands and parse their output in python.

At the request of @Allan, the output of ifconfig (Mojave)

lo0: flags=8049<UP,LOOPBACK,RUNNING,MULTICAST> mtu 16384
    options=1203<RXCSUM,TXCSUM,TXSTATUS,SW_TIMESTAMP>
    inet 127.0.0.1 netmask 0xff000000 
    inet6 ::1 prefixlen 128 
    inet6 fe80::1%lo0 prefixlen 64 scopeid 0x1 
    nd6 options=201<PERFORMNUD,DAD>

en0: flags=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 1500
    options=10b<RXCSUM,TXCSUM,VLAN_HWTAGGING,AV>
    ether f0:18:98:ed:f6:45 
    inet6 fe80::1040:443f:fe24:97e8%en0 prefixlen 64 secured scopeid 0x8 
    inet 192.168.1.10 netmask 0xffffff00 broadcast 192.168.1.255
    nd6 options=201<PERFORMNUD,DAD>
    media: autoselect (1000baseT <full-duplex,flow-control>)
    status: active
en9: flags=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 1500
    ether ac:de:48:00:11:22 
    inet6 fe80::aede:48ff:fe00:1122%en9 prefixlen 64 scopeid 0x9 
    nd6 options=201<PERFORMNUD,DAD>
    media: autoselect (100baseTX <full-duplex>)
    status: active

en1: flags=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 1500
    ether f0:18:98:b6:d3:d0 
    inet 192.168.1.11 netmask 0xffffff00 broadcast 192.168.1.255
    media: autoselect
    status: active

awdl0: flags=8943<UP,BROADCAST,RUNNING,PROMISC,SIMPLEX,MULTICAST> mtu 1484
    ether 12:69:24:84:fd:9a 
    inet6 fe80::1069:24ff:fe84:fd9a%awdl0 prefixlen 64 scopeid 0xd 
    nd6 options=201<PERFORMNUD,DAD>
    media: autoselect
    status: active

vboxnet0: flags=8842<BROADCAST,RUNNING,SIMPLEX,MULTICAST> mtu 1500
    ether 0a:00:27:00:00:00 
vboxnet1: flags=8842<BROADCAST,RUNNING,SIMPLEX,MULTICAST> mtu 1500
    ether 0a:00:27:00:00:01 
vboxnet2: flags=8943<UP,BROADCAST,RUNNING,PROMISC,SIMPLEX,MULTICAST> mtu 1500
    ether 0a:00:27:00:00:02 
    inet 192.168.97.1 netmask 0xffffff00 broadcast 192.168.97.255

I can egrep 'init.*192\.168\.1\.11' on that to get the ip-addresses of the interfaces on this host.

  • You could also parse the output of ifconfig, but using route seems more appropriate. – nohillside Jan 30 at 14:48
  • Maybe I am interested in an ifconfig method as I just found out my route route ;-) doesn't work on High Sierra – gctwnl Jan 30 at 15:03
  • 1
    lo0 is your loopback interface. It's a pseudo interface created by the system so it will be local no matter what. I've read this question several times and it seems that you're asking to determine if any given IP address is on the local subnet or not. Is that correct? – Allan Jan 30 at 15:09
  • I've adapted the question to make it more clear – gctwnl Jan 30 at 15:19
  • I've removed the inactive/irrelevant interfaces and only left the active ones. The first thing I can see is that you have a multi-homed setup with WiFi and Ethernet both connected to the same network - this is not ideal. If you're connected via Ethernet, turn off WiFi. There's no reason to have both on simultaneously. – Allan Jan 30 at 15:58
2

I've had to figure this out for some customers in the past. Easiest and most reliable method across OS versions I've found is string matching inet lines in ifconfig results.

0

netstat is the tool meant for that.

netstat -i will display your interfaces and IPs associated with them

here is the link with more examples and man netstat of course the next thing to read to get more details.

You must log in to answer this question.

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