0

I need to create a Python script that queries the network interfaces and returns me the name of the host, the IP address, and the mac address.

#!/usr/bin/env python3
import netifaces

x = netifaces.interfaces()
i = x[0]

for i in x:
    if i != 'lo':

        print(i)
        face = netifaces.ifaddresses(i)

        print(face)
        i += i
    else:
        continue

This is one version of the program I am working with. This seems to grab all the data that I need but I cannot get it to print clean or correctly! I am looking for something like: "Nic: wlan0, ipaddr: 10.0.0.1, mac: 4651168584541"

I am new to programming and very new to python so please any help is appreciated!

Joe
  • 2,641
  • 5
  • 22
  • 43

2 Answers2

0

Please check the links:

  1. @camflan's response in Getting MAC Address

  2. How to get the physical interface IP address from an interface

    import netifaces
    
    x = netifaces.interfaces()
    
    
    for i in x:
        if i != 'lo':
            print(i)
            print("mac:" + netifaces.ifaddresses(i)[netifaces.AF_LINK][0]['addr'] + "     ipaddr:" + netifaces.ifaddresses(i)[netifaces.AF_INET][0]['addr'])
            i += i
        else:
            continue
    
Community
  • 1
  • 1
vvanarasi
  • 1
  • 4
  • For some reason this is not working for me. Error on line 9 where we declare face – Joe Feb 23 '17 at 04:00
  • Apologies Joe. Could you please try the updated code – vvanarasi Feb 23 '17 at 17:06
  • still getting that syntax error. face = "mac:" + netifaces.ifaddresses(i)[netifaces.AF_LINK][0]['addr'] + ";ipaddr:" netifaces.ifaddresses(i)[netifaces.AF_INET][0]['addr'] – Joe Feb 23 '17 at 17:11
  • It is telling me that netifaces is an unresolved reference which i do not understand because it is declared. – Joe Feb 23 '17 at 17:13
  • Removed the face variable assignment and printing directly. Please check now. – vvanarasi Feb 23 '17 at 17:14
  • "Key error 2" is what I am receiving now. Still on the same line the print(mac.... – Joe Feb 23 '17 at 17:17
  • I have tried running the code from the link "How to get.." that you posted and it is returning the same keyerror: 2 – Joe Feb 23 '17 at 17:21
  • So Broke your code up and was able to get the first half (netifaces.ifaddresses(i)[netifaces.AF_LINK][0]['addr']) to print the mac beautifully. The second half (netifaces.ifaddresses(i)[netifaces.AF_INET][0]['addr']) is where the key error is triggering – Joe Feb 23 '17 at 17:30
  • We get a keyerror when the key is not defined in the dictionary. http://www.endmemo.com/python/keyerror.php. – vvanarasi Feb 23 '17 at 17:38
  • Some of the network interfaces doesnot have the ipaddresses defined? – vvanarasi Feb 23 '17 at 17:40
  • Yes! just realized that is why it is breaking. I am on wifi so there is no IP for eth0 and that is why it is breaking. – Joe Feb 23 '17 at 19:05
  • So got it to work just about perfectly after accounting for the KeyError. Thank you so much @vvanarasi. I will post the answer soon – Joe Feb 23 '17 at 19:54
0

So this was a little tricky at first but this format should allow you to grab data from the results of netifaces.

import netifaces

x = netifaces.interfaces()


for i in x:
    if i != 'lo':
    print('\nInterface: ' + i)
    mac = netifaces.ifaddresses(i)[netifaces.AF_LINK][0]['addr']
    print('Mac addr: ' + mac)

    try:
        ip = netifaces.ifaddresses(i)[netifaces.AF_INET][0]['addr']

        print('IP addr: {0} '.format(ip))
    except KeyError:
        print('NO IP')
        continue

Output will look as follows:

Interface: eth0
Mac add: eo:ie:9:38:ri
No IP

Interface: wlan0
Mac addr: 34:po:iu:66
IP addr: 10.0.0.1
Joe
  • 2,641
  • 5
  • 22
  • 43
  • What happens if you are connecting with a protocol that doesn't use MAC addresses, e.g. PPP or HDLC? Does it work with protocols that use 64-bit MAC addresses? What about IPv6? – Ron Maupin Jul 21 '17 at 16:51
  • Not sure never tested it under those conditions. I would assume that IPv6 would work fine. This was a homework assignment so it was good enough. I would imagine that the `netifaces` docs may cover this. Even if the protocol does not use mac addrs it shouldnt matter bc the interface should have one right? – Joe Jul 21 '17 at 17:55
  • Only interfaces that use IEEE LAN protocols would have MAC addresses, and some will use 48-bit MAC addresses, and some will use 64-bit MAC addresses. Many libraries only assume 48-bit MAC addresses, but newer IEEE LAN protocols use 64-bit MAC addresses. If you connect with an interface that doesn't use IEEE LAN protocols, then there is no MAC address. Don't assume that everything is ethernet or Wi-Fi. I was just pointing out that making assumptions, like many programmers do with networks, can lead to problems, bugs, and security holes. I just see so much crap software released for networks. – Ron Maupin Jul 21 '17 at 18:01
  • haha thanks for the information. And don't worry my little script will not be released to anyone so it will not join the pile of crap – Joe Jul 21 '17 at 18:05