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 Type the following code.

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

1. The first line of code, imports the os module in Python.

2. 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.

Learn to write your own network automation tools and script with our Course Take the course on Udemy.

Python Network Programming and Automation Course