Python program to get the OS information, Active ports and Public and Private IP address in Windows

  1. Display OS information using Python script.

Same Modules of Python can be used for Windows too. The only difference comes down to the arguments used for different modules.

To Display information about underlying operating system following script can be used:

#code starts here
import platform
osType=platform.system()
print("OS TYPE: " + osType)
osRelease=platform.release()
print("\nversion: " + osRelease)
  #code ends here

 

platform is a module supported by python to access the underlying platform’s data.

platform.system() returns the type of the underlying operating system.

Platform.release() returns the release version of the operating system.

 

  1. View active ports:

Python sports module named “os” which delivers the operating system functionalities. This module can be use to execute commands through python.

This module can be use to view active ports by executing netstat command which is a tool to display network connections information such as Transmission Control  displays, routing tables, number of interface and network protocol statistics.

import os
print("All Listening Ports: ")
os.system("netstat -aon")
#code ends here

import os : Importing os module in the script.

os.system(“netstat  –aon”) : os.system(“command to execute”) is used to run command passed as parameter.

netstat  –aon : using netstat tool to view all the active ports and aon is multiple argument for netstat command.  –a is used to listen all the active ports –o Displaying owner of the process and -n is used to display all addresses and port numbers in numerical form.

 

  1. View public and private IP address of the system:

ipconfig commands comes here in sport which can be used to list all the network adapters and their details about each of them.

#code starts here
import os
print("Private IP: ")
os.system("ipconfig")
print("Public IP: ")
os.system("nslookup myip.opendns.com. resolver1.opendns.com")
#code ends here

os.system(“ipconfig”) : Lists details of all the network adapters present in the system.

os.system("nslookup myip.opendns.com. resolver1.opendns.com") : uses the tool nslookup, which returns the public IP address of the system.