Python is a versatile and powerful programming language known for its simplicity and readability. It gaines immense popularity among developers, data scientists, and system administrators due to its wide range of applications and extensive libraries.

In the realm of Information Technology Operations (IT Ops), Python has become an invaluable tool for managing and automating various tasks.

Here is a basic task which Python can be used to collect data from command line output of a softare program.


A use case I am going to present is the command line output from the screenshot below. It is from a program called NoMachine. NoMachine is a remote desktop software solution that allows users to access and control a remote computer or server from a local machine over a network. NX Server, which runs NoMachine services, is a great solution for managing remote desktop connections on a Linux environment.

The screenshot shows a typical nxserver command, nxserver --list that lists all users with active sessions. We can see the output contains 5 columns: Display, Username, Remote IP, Session ID and Node.

It looks like we can somehow store these data into an array.

Python has is a built-in library module called subprocess that provides a way to spawn new processes, interact with them, and manage input/output streams. It serves as a bridge between Python programs and the operating system’s process management capabilities.

We can utilize this library to retrieve the output from nxserver, and store the output in an array.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import subprocess
import json

# Run the "nxserver --list" command and capture its output
output = subprocess.check_output(["/usr/NX/bin/nxserver", "--list"])
#print(output)

# Convert the output to a string
output_str = output.decode("utf-8")

# Split the output into lines
lines = output_str.split("\n")

# create a list to hold the session information
sessions = []

# Loop through the lines and print the information for each server
for line in lines:
if line.startswith("NX>"): # Skip any informational lines that start with "NX>"
continue
if line.startswith("Display") :
continue
if line.startswith("---") :
continue
#print(line)
cols = line.split()
#if len(cols) > 4 and cols[2] == "-" : # Check that the line has the expected number of columns
if len(cols) > 4 : # Check that the line has the expected number of columns
sessions.append({"session": cols[0], "user": cols[1], "ip": cols[2]})


# save sessions into a JSON file
with open("/user/nxserver-cleanup/sessions.json", "w") as f :json.dump(sessions, f)

The code example above capture the output from nxserver --list, split each lines from the output, skip lines begin with NX>, Display and ---. Split the each line again by spaces.

Users and their session information are store in an array once the for loop has executed.

The last step is to save the array into a JSON file.


Easy enough.

Now create an API on the server to expose the JSON file or utilize any backend programming languages that can authenticate with SSH protocol to grab this JSON file for display.