2

I have a code snippet which print interface information if any new interface is getting up, but it prints same interface information multiple times

import os
import socket
import struct

# These constants map to constants in the Linux kernel. This is a crappy
# way to get at them, but it'll do for now.
RTMGRP_LINK = 1

NLMSG_NOOP = 1
NLMSG_ERROR = 2

RTM_NEWLINK = 16
RTM_DELLINK = 17

IFLA_IFNAME = 3

# Create the netlink socket and bind to RTMGRP_LINK,
s = socket.socket(socket.AF_NETLINK, socket.SOCK_RAW, socket.NETLINK_ROUTE)
s.bind((os.getpid(), RTMGRP_LINK))

while True:
    data = s.recv(65535)
    msg_len, msg_type, flags, seq, pid = struct.unpack("=LHHLL", data[:16])

    if msg_type == NLMSG_NOOP:
        print "no-op"
        continue
    elif msg_type == NLMSG_ERROR:
        print "error"
        break

    # We fundamentally only care about NEWLINK messages in this version.
    if msg_type != RTM_NEWLINK:
        continue

    data = data[16:]

    family, _, if_type, index, flags, change = struct.unpack("=BBHiII", data[:16])

    remaining = msg_len - 32
    data = data[16:]

    while remaining:
        rta_len, rta_type = struct.unpack("=HH", data[:4])

        # This check comes from RTA_OK, and terminates a string of routing
        # attributes.
        if rta_len < 4:
            break

        rta_data = data[4:rta_len]

        increment = (rta_len + 4 - 1) & ~(4 - 1)
        data = data[increment:]
        remaining -= increment

        # Hoorah, a link is up!
        if rta_type == IFLA_IFNAME:
            print "New link %s" % rta_data

run the above program and restart network-manager

sudo service network-manager restart

output :

New link enp0s25
New link lo
New link wlp4s0
New link wlp4s0
New link wlp4s0
New link wlp4s0
New link wlp4s0
New link wlp4s0
New link enp0s25
New link wlp4s0
New link wlp4s0
New link wlp4s0
New link wlp4s0
New link wlp4s0
New link wlp4s0
New link wlp4s0
New link wlp4s0
New link wlp4s0
New link wlp4s0

I am excepting to get following output only

New link enp0s25
New link lo
New link wlp4s0

Also I got a c program which return me the right output but I want this in python

4
  • Store the interfaces and don't print if an interface is known already. Commented Jun 1, 2017 at 5:48
  • In that case if I restart the network again it wont display anything because the result is already stored. @KlausD. Commented Jun 1, 2017 at 5:51
  • Then you have to find out when an interface goes down and remove it. BTW I would listen to the dbus to get notified about the network changes in realtime. Commented Jun 1, 2017 at 5:54
  • Someone already did this in C without having an extra patches and I didn't understand the dbus. @KlausD. Commented Jun 1, 2017 at 6:13

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.