Python Utilities Using Piping from PowerShell or CMD.exe

Python Utilities Using Piping from PowerShell or CMD.exe

By Tom Carpenter On 06/30/2020

Python is very powerful on Windows, Linux, or macOS. As network administrators, it is often useful to build your own utilities that can receive input on standard input (STDIN) and this brief post explains how to do it with Python.

The key to stdin in Python is the sys standard library. It works on Windows, Linux, and macOS and from it you can use the sys.stdin function to receive input from other commands. For example, in Windows PowerShell, you can use the Get-NetTCPConnection command to view your current connections. You can pipe (pass) the output of this command to a Python script instead of the screen to do some creative things. For example, you can look for specific IP addresses, remote ports, local ports, etc.

To get started, in your Python script, use the following command to begin your script:

import sys

This command makes all of the functions and other elements in the sys module available to your script. The sys module includes several functions, including:

  • path (the system path)
  • getwindowsversion (major, minor, build, platform id, and service pack level)
  • stdin
  • stderr
  • stdout
  • platform (win32/win64)

Next, you will need to read the standard input, you can loop through it to read each line of output from the piped command like this:

for line in sys.stdin:
if '80' in line:
if '127.0.0.1' in line or 'Bound' in line or 'Listen' in line:
#do nothing - reserved for future use
nonIPLinkLine=True
else:
print(line)

This code will use the input from stdin and process it one line at a time. If the line includes the number 80 (or characters 80), further processing is applied. If the line includes 127.0.0.1 or Bound or Listen, it is ignored. Otherwise, the line is printed. This script is effectively acting as a filter when you pipe the Get-NetTCPConnection PowerShell command to it. (NOTE: There is a cleaner way to write the filter line that looks for 127.0.0.1, Bound, or Listen - I just wanted the code to be very clear here.)

This simple example should get you started with your own piping scripts. Once you save this as a file, you can call it this way in PowerShell (assuming you saved the script as pipe.py):

Get-NetTCPConnection | python pipe.py

Yes, you can do this filtering with built-in PowerShell commands, that was not the point here. The point here was to show how to get the piped information into the script so that you can process it however you desire.

Happy scripting! Full script below:

import sys
for line in sys.stdin:
if '80' in line:
if '127.0.0.1 in line or 'Bound' in line or 'Listen' in line:
#do nothing - reserved for future use
nonIPLinkLine=True
else:
print(line)
Tagged with: python, sys module, pipe commands, piping
Success Stories

I literally just came out of the testing centre having taken the CWDP exam. The certification process opened my mind to different techniques and solutions. This knowledge can only broaden your perspective. Great job, CWNP, you have a great thing going on here.

-Darren
Read More

Working through the CWNP coursework and certifications helped not only to deepen my technical knowledge and understanding, but also it boosted my confidence. The hard work it took to earn my CWNE has been rewarding in so many ways.

-Ben
Read More

I want to commend you and all at CWNP for having a great organization. You really 'raise the bar' on knowing Wi-Fi well. I have learned a ton of information that is helping my job experience and personal career goals, because of my CWAP/CWDP/CWSP studies. Kudos to all at CWNP.

-Glenn
Read More