Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrong NetMask shown for IP #26

Open
nsgundy opened this issue Jan 11, 2018 · 4 comments
Open

Wrong NetMask shown for IP #26

nsgundy opened this issue Jan 11, 2018 · 4 comments

Comments

@nsgundy
Copy link

nsgundy commented Jan 11, 2018

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
Copy link
Author

nsgundy commented Jan 11, 2018

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.

@tomas tomas mentioned this issue Jan 11, 2018
@tomas
Copy link
Owner

tomas commented Jan 11, 2018

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

@nsgundy
Copy link
Author

nsgundy commented Jan 12, 2018

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.

@tomas
Copy link
Owner

tomas commented Jan 12, 2018

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants