View Computer’s Important Network Information Using Python

While working in a network, we do some troubleshooting with the network or Internet problem. This time we need to check your own system network connection information.

We can find the network connection in the Control Panel in Windows. The best way to find this information we can use ipconfig command in CMD. When you use ipconfig /all then you can get enough information to resolve your network problem.

Examples:

Method 1:



We will use the subprocess module to interact with cmd and to retrieve information into your python ide. We can read the cmd command through the subprocess module.

Approach:

  1. import module
  2. Get the output for the command “‘ipconfig’,’/all’ ” using subprocess.check_output()
  3. Now get the Split the string and arrange your data with your own needs.

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# import module
import subprocess
  
# Traverse the ipconfig information
data = subprocess.check_output(['ipconfig','/all']).decode('utf-8').split('\n')
  
# Arrange the bytes data
for item in data:
     print(item.split('\r')[:-1])

chevron_right


Output:

Method 2:

Ifcfg module is a cross-platform (Windows/Unix) library for parsing ifconfig and ipconfig output in Python. It is useful for pulling information such as IP, Netmask, MAC Address, Hostname, etc.

Installation:

pip install ifcfg

Implementation:

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

import ifcfg
print(ifcfg.interfaces())

chevron_right


Output:

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.