How to use Python with netstat

This tutorial explains how we can combine netstat with Python for automation. Install Python on the system and create a file Pythonnetstat.py. Type the following code.

import os
a=os.popen(‘netstat -a’).read()
print ” \n Connections “,a

Take our Python Network programming Course for $12.99 on udemy

The first line of code, imports the os module in Python.
The second line opens the command netstat with the -a option, which is used for printing the connections on the system. The third line prints the output on the screen. The output of the code is shown below.

Take our Python Network programming Course for $12.99 on udemy