Obtain IP address (Windows / Linux sys.) from Python – plus simple practice

Table of Contents

  • Windows System
  • Linux
  • A Simple Demo

Programs operating on different systems make no unique IP address. This can cause problems whenever we specify an exact IP address in services while executing the service programs on other machines/internet.

This simple instruction introduces netifaces in Python to flexibly locate IP address depending on Internet and systems of the device/machine.

 

Windows System

Open command line tool (whatever you’d like to run python) and go into Python environment. And then

 

import netifaces

 

Run


ni_interface = netifaces.interfaces()


This will list several interface identifiers of the machine (list of identifiers). One can see different configurations according to machines. The identifier is unique within the subnet and can identify interface of particular node. Well, just a brief note about interface and not knowing details will not prevent us from going for the next few steps.


print(ni_interface)


From this we see detailed identifiers and there are nine interface identifiers that can identify IP configuration, like

['{E61F3CCF-B4EB-4341-9024-784D5E87B4AB}', '{5B170AE3-374D-4DE2-A491-3D5DA51FB496}', '{C528B219-A992-4C09-9F18-3DE367BDAD38}', '{90C0A1C0-419A-429B-A7C9-FE600615B218}', '{05E56D60-ECAE-4D65-B95E-0CA85DA727F9}', '{9C741744-2EFF-44EF-850E-9C86EA90709F}', '{49457F3C-A4C6-42BD-8F07-CF182427E4CC}', '{2A09F8A5-E498-4FF1-8B46-08332E3251F8}', '{79761758-0053-11EB-8875-806E6F6E6963}']

 

We are able to ask for the IP address from a particular interface, by running

 

ifaddress = netifaces.ifaddresses(ni_interface[6])

 

In this case the interface of index 6 associates with a configuration of Wireless Local Area Network (Wireless LAN) Adapter, for which we aim to retrieve IP. The IP address obtained via the interface allows us to permit access to a network for any device.

 

The ifaddress looks like

{-1000: [{'addr': '34:c9:3d:23:a7:21'}], 23: [{'addr': 'fe80::6d3b:fd6b:a28:52ac%15', 'netmask': 'ffff:ffff:ffff:ffff::/64', 'broadcast': 'fe80::ffff:ffff:ffff:ffff%15'}], 2: [{'addr': '172.20.10.3', 'netmask': '255.255.255.240', 'broadcast': '172.20.10.15'}]}

 

The exact IP can be retrieved by


ip = ifaddress[2][0]['addr']

 

Get IP


'172.20.10.3'

 

ifaddress is an outer dictionary containing several key-value pairs. We dived into key which is named 2, accessing its value – the list containing a dictionary with a single dictionary in it (0). In that, we specify ‘addr’ to get the IP value kept paired with it.

 

But, how did we know to assign the 6?

By checking


ipconfig


in terminal (suggest running on another command line tools). It’s found that the Wireless LAN Adapter interface is right at the 7th position, which matches index of 6 (Python is indexed starting from 0).

 

Of course it’s not necessarily to be 6. we can specify other index numbers to locate other interfaces and their IP as we practically require.

 

Linux

In Linux system we can operate the same thing as well, and it’s demonstrated in Ubuntu 18.04 as follows

 

import netifaces

 

ni_interface = netifaces.interfaces()

 

print(ni_interface)

And we obtain interface identifiers like

['lo', 'bond0', 'dummy0', 'sit0', 'eth0']

 

According to netifaces documentation, IP address is bound to ‘eth0’ interfaces from which we can retrieve (also notice that ‘eth0’ is unavailable in Mac OS.

 

ifaddress = netifaces.ifaddresses(‘eth0’)

 

The results of this case look like

{17: [{'addr': '00:15:5d:5c:39:dd', 'broadcast': 'ff:ff:ff:ff:ff:ff'}], 2: [{'addr': '172.26.173.53', 'netmask': '255.255.240.0', 'broadcast': '172.26.175.255'}], 10: [{'addr': 'fe80::215:5dff:fe5c:39dd%eth0', 'netmask': 'ffff:ffff:ffff:ffff::'}]}


Run

 

ip = ifaddress[2][0]['addr']

 

We can access IP address

'172.26.173.53'

 

A Simple Demo

Consider we aim to recognise and serve different IP addresses associated with different operating systems and internet connections. We can identify IP addresses based upon the simple instructions above, adding a few functionalities.

 

import os

os_name = os.name

 

This can help us identify the current operating system. In Windows system it may return ‘nt’ as system name, while in Linux, ‘posix’.

 

With this we are able to put things altogether in Python code base.

 

import os

import netifaces

 

os_name = os.name

ni_interface = netifaces.interfaces()

 

if os_name == 'posix':

     host = ni.ifaddresses('eth0')[2][0]['addr']

elif os_name == 'nt':

     host = ni.ifaddresses(ni_interface[6])[2][0]['addr']


References: 

https://pypi.org/project/netifaces/

https://stackoverflow.com/questions/6243276/how-to-get-the-physical-interface-ip-address-from-an-interface

https://www.thewindowsclub.com/change-ip-address-windows-10

https://www.techtarget.com/searchnetworking/definition/host


留言

這個網誌中的熱門文章

Pip doesn't work in Python - Ubuntu

Create NFT on Solana (Windows)