network icon indicating copy to clipboard operation
network copied to clipboard

Wrong NetMask shown for IP

Open nsgundy opened this issue 7 years ago • 4 comments

Node: 6.9.1 network: 0.4.0

I have this situation where eth0 has two IP addresses (one assigned by DHCP and the other is a link-local address assigned by systemd-networkd), but get_interfaces_list() returns the IP of the DHCP address with the netmask of the link-local address.

{
  "name": "eth0",
  "ip_address": "192.168.2.116",
  "mac_address": "5c:f8:21:0f:40:d8",
  "gateway_ip": "192.168.2.1",
  "netmask": "255.255.0.0",
  "type": "Wired"
}

Output from os.networkInterfaces()

> os.networkInterfaces()
{ lo:
   [ { address: '127.0.0.1',
       netmask: '255.0.0.0',
       family: 'IPv4',
       mac: '00:00:00:00:00:00',
       internal: true } ],
  eth0:
   [ { address: '169.254.123.48',
       netmask: '255.255.0.0',
       family: 'IPv4',
       mac: '5c:f8:21:0f:40:d8',
       internal: false },
     { address: '192.168.2.116',
       netmask: '255.255.255.0',
       family: 'IPv4',
       mac: '5c:f8:21:0f:40:d8',
       internal: false } ] }

Output from ip -o -4 addr show eth0:

2: eth0    inet 169.254.123.48/16 brd 169.254.255.255 scope link eth0\       valid_lft forever preferred_lft forever
2: eth0    inet 192.168.2.116/24 brd 192.168.2.255 scope global dynamic eth0\       valid_lft 1803687sec preferred_lft 1803687sec

This also poses the question: How can I grab a specific IP address for an interface using network? In my application for example, I am interested in the scope global address when available, otherwise in the scope local address.

nsgundy avatar Jan 11 '18 15:01 nsgundy

Right... I just took a look at the code and the command that is executed to grab the netmask for eth0 is the following, followed by the output

ifconfig "eth0" 2> /dev/null | egrep 'netmask|Mask:' | awk '{print $4}' | sed 's/Mask://'
255.255.0.0

Well, in this case ifconfig isn't the right thing to use as it only lists one of the two IPs and in this case its the link-local address:

ifconfig eth0
eth0      Link encap:Ethernet  HWaddr 5C:F8:21:0F:40:D8
          inet addr:169.254.123.48  Bcast:169.254.255.255  Mask:255.255.0.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:7177 errors:0 dropped:122 overruns:0 frame:0
          TX packets:8245 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:1095587 (1.0 MiB)  TX bytes:4655128 (4.4 MiB)
          Interrupt:33

Using ip -o -4 addr show eth0 might be better suited, as it outputs both IPs (see post above for output). I don't know how good / bad this is for backwards compatibility.

nsgundy avatar Jan 11 '18 16:01 nsgundy

@nsgundy What's the actual output you're getting from interfaces_list(), then? Are you getting a single eth0 interface or two?

tomas avatar Jan 11 '18 21:01 tomas

The array returned by interfaces_list() contains a single entry:

[ { name: 'eth0',
    ip_address: '192.168.2.116',
    mac_address: '5c:f8:21:0f:40:d8',
    gateway_ip: '192.168.2.1',
    netmask: '255.255.0.0',
    type: 'Wired' } ]

So far, from what I have observed, ifconfig seems to list the address that was assigned last, where as ip addr and the built in os.networkInterfaces() lists them in the order they have been added.

nsgundy avatar Jan 12 '18 05:01 nsgundy

Ok, I'll take a look at this later. Thanks for the heads up!

tomas avatar Jan 12 '18 13:01 tomas