How do I find out (command line or python) if an IP address is from the local host? 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 &lt;ip-address&gt; 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: &lt;UP,HOST,DONE,LLINFO,WASCLONED,LOCAL,IFSCOPE,IFREF&gt;
 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: &lt;UP,HOST,DONE,LLINFO,WASCLONED,LOCAL,IFSCOPE,IFREF&gt;
 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&lt;UP,LOOPBACK,RUNNING,MULTICAST&gt; mtu 16384
    options=1203&lt;RXCSUM,TXCSUM,TXSTATUS,SW_TIMESTAMP&gt;
    inet 127.0.0.1 netmask 0xff000000 
    inet6 ::1 prefixlen 128 
    inet6 fe80::1%lo0 prefixlen 64 scopeid 0x1 
    nd6 options=201&lt;PERFORMNUD,DAD&gt;

en0: flags=8863&lt;UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST&gt; mtu 1500
    options=10b&lt;RXCSUM,TXCSUM,VLAN_HWTAGGING,AV&gt;
    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&lt;PERFORMNUD,DAD&gt;
    media: autoselect (1000baseT &lt;full-duplex,flow-control&gt;)
    status: active
en9: flags=8863&lt;UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST&gt; mtu 1500
    ether ac:de:48:00:11:22 
    inet6 fe80::aede:48ff:fe00:1122%en9 prefixlen 64 scopeid 0x9 
    nd6 options=201&lt;PERFORMNUD,DAD&gt;
    media: autoselect (100baseTX &lt;full-duplex&gt;)
    status: active

en1: flags=8863&lt;UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST&gt; 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&lt;UP,BROADCAST,RUNNING,PROMISC,SIMPLEX,MULTICAST&gt; mtu 1484
    ether 12:69:24:84:fd:9a 
    inet6 fe80::1069:24ff:fe84:fd9a%awdl0 prefixlen 64 scopeid 0xd 
    nd6 options=201&lt;PERFORMNUD,DAD&gt;
    media: autoselect
    status: active

vboxnet0: flags=8842&lt;BROADCAST,RUNNING,SIMPLEX,MULTICAST&gt; mtu 1500
    ether 0a:00:27:00:00:00 
vboxnet1: flags=8842&lt;BROADCAST,RUNNING,SIMPLEX,MULTICAST&gt; mtu 1500
    ether 0a:00:27:00:00:01 
vboxnet2: flags=8943&lt;UP,BROADCAST,RUNNING,PROMISC,SIMPLEX,MULTICAST&gt; 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.
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.