My objective is to write a Python script that provides the user with a series of link-local IPv6 addresses of specific devices. My technique is to send out a multicast ping and then use neighbor discovery to get a list of addresses which I can then filter down.
The first step is this:
output = subprocess.check_output(['ping6', '-c', '2', 'ff02::1%en0'])
The problem is the term en0 at the end. It is not a constant. On MacOS it is en0 but on Linux it will be eth0 or possibly br0. Windows has a different command but the same problem. Without the interface qualifier the ping6 command does not work.
Also, some systems may have multiple interfaces.
So how would a Python script get a list of those interfaces?
Alternatively, can this be done by using the socket package and not subprocess? (I don't want my script to require privileged mode and using ping gets around that.)
Under Linux there's no guarantee that your first Ethernet interface will be named
eth0
. It might be namedp1s3
orem1
or eveninternal
. Orbob
.Under both Linux and OS X you can use the
netifaces
python module to get a list of available interfaces and addresses associated with those interfaces. For example, consider the following code:Which on my Linux system produces:
And on my OS X system produces:
I have no idea if this works under Windows or not.