Home > Blockchain >  How to properly parse and strip my output in Python to then assign Key Value pairs
How to properly parse and strip my output in Python to then assign Key Value pairs

Time:10-06

I'm currently trying to write a script so that I can display network information on a graph. Unfortunately, I'm not having too much success in what I need to get done. Here is the output I have to work with:

Temperature Sensors: Unit Sensor Description Temp (C) State Max_Temp (C)


1 1 PHY 32 Normal 40 1 2 REAR 38 Normal 45 1 3 CPU 40 Normal 48

Temperature Status: Normal

Fan Duty Level: 46%

Fans: Unit Fan Description Type Speed Duty level State


1 1 FAN-1 Fixed 6617 46% Operational 1 2 FAN-2 Fixed 6482 46% Operational

With that being said, I'm only concerned about the Fan speed (not percentage) and the 3 fields (PHY, CPU, and REAR) with their corresponding Temp values (not the max value, just the current value). What I would like my output to look like is the following:

{'PHY': '32', 'REAR': '38', 'CPU': '40', 'FAN-1': 6617, 'FAN-2': 6482}

The reason behind the key value pairs is that I utilize a tool called Logicmonitor that can take external scripting from network equipment and draw the values on a graph that we can track historically.

The closest I've gotten my output to is the following:

['\n', '1', '1', 'P', 'H', 'Y', '3', '2', 'N', 'o', 'r', 'm', 'a', 'l', '4', '0', '\n', '1', '2', 'R', 'E', 'A', 'R', '3', '8', 'N', 'o', 'r', 'm', 'a', 'l', '4', '5', '\n', '1', '3', 'C', 'P', 'U', '4', '0', 'N', 'o', 'r', 'm', 'a', 'l', '4', '8', '\n', '\n', 'T', 'e', 'm', 'p', 'e', 'r', 'a', 't', 'u', 'r', 'e', 'S', 't', 'a', 't', 'u', 's', ':', 'N', 'o', 'r', 'm', 'a', 'l', '\n', '\n', 'F', 'a', 'n', 'D', 'u', 't', 'y', 'L', 'e', 'v', 'e', 'l', ':', '4', '6', '%', '\n', '\n', 'F', 'a', 'n', 's', ':', '\n', 'U', 'n', 'i', 't', 'F', 'a', 'n', 'D', 'e', 's', 'c', 'r', 'i', 'p', 't', 'i', 'o', 'n', 'T', 'y', 'p', 'e', 'S', 'p', 'e', 'e', 'd', 'D', 'u', 't', 'y', 'l', 'e', 'v', 'e', 'l', 'S', 't', 'a', 't', 'e', '\n', '1', '1', 'F', 'A', 'N', '-', '1', 'F', 'i', 'x', 'e', 'd', '6', '6', '5', '0', '4', '6', '%', 'O', 'p', 'e', 'r', 'a', 't', 'i', 'o', 'n', 'a', 'l', '\n', '1', '2', 'F', 'A', 'N', '-', '2', 'F', 'i', 'x', 'e', 'd', '6', '4', '7', '4', '4', '6', '%', 'O', 'p', 'e', 'r', 'a', 't', 'i', 'o', 'n', 'a', 'l', '\n', '\n']

At this point, I know I'm in the right general direction, but I'm unable to figure out where my end destination needs to be. Here is my current script:

#!/usr/bin/python3

import netmiko

connection =netmiko.ConnectHandler(ip="1.2.3.4", port="22", device_type="ubiquiti_edgeswitch", username="admin", password="password", secret="password")

#defining a list to be used
output = {}

#assigning the list to the output
output = connection.send_command("show environment | exclude -- begin PHY")

#stripping all leading and trailing spaces from a list of strings
output_new = [item.replace(' ','') for item in output]

#removing any unnecessary spaces from list
while('' in output_new):
    output_new.remove('')

print(output_new)

connection.disconnect()

CodePudding user response:

Under the assumption that the "arguments" remain their position in relation to each key (e.g. the value for PHY always comes directly after the keyword PHY), you could use a dictionary that maps each key to what the index offset of the value from the keyword is in the output, and then construct your dictionary from that.

So, for example, in the case of the string 1 1 PHY 32 Normal 40 1 2 REAR 38 Normal 45 1 3 CPU 40 Normal 48, the "value" of the keyword PHY has an offset of 1. This means that if we split the string by spaces, we know that the value of PHY will be found at the index of PHY 1.

This idea is implemented in the following code-snippet:

offsets = {
    "PHY": 1,
    "REAR": 1,
    "CPU": 1,
    "FAN-1": 2,
    "FAN-2": 2,
}

data = {}

words = output.split()

for key in offsets:
    index = words.index(key)   offsets[key]
    data[key] = words[index]

Adding this logic to your existing code, we would get:

#!/usr/bin/python3

import netmiko

connection =netmiko.ConnectHandler(ip="1.2.3.4", port="22", device_type="ubiquiti_edgeswitch", username="admin", password="password", secret="password")

#defining a list to be used
output = {}

#assigning the list to the output
output = connection.send_command("show environment | exclude -- begin PHY")
words = output.split()

# Defining the offsets of the "values" for each keyword
offsets = {
    "PHY": 1,
    "REAR": 1,
    "CPU": 1,
    "FAN-1": 2,
    "FAN-2": 2,
}

# The processed data
data = {}

# Loop over each keyword and extract its value
for key in offsets:
    index = words.index(key)   offsets[key]
    data[key] = words[index]


print(data)

connection.disconnect()

CodePudding user response:

This is not an answer to your question, but the recommended way to solve the problem:

There are parsers that can read structured data from network show commands. Netmiko currently supports textFSM, ttp and genie. You can see how to use them in netmiko in the examples section: netmiko examples

  • Related