Nov-21-2019, 10:29 PM
I am fairly new to programming and still getting to know python.
So I am trying to pull information from a Cisco switch output using show commands and then to store just some of the information from the output into a json file.
My script is using Python 3 and is being run on a Debian virtual machine that is connected to my physical switch. My script connects to the switch using SSH just fine and also input the correct show commands to display the information I want. But when my script tries to collect some of the information I want to save to a json file I get this error message:
raise TypeError(repr(o) + " is not JSON serializable")
TypeError: b's1' is not JSON serializable
s1 is the host name of my switch and I want this information in the json file.
If I then run it again straight after I also get another error message:
raise TypeError(repr(o) + " is not JSON serializable")
TypeError: b'12.1(22)EA14' is not JSON serializable
12.1(22)EA14 is the Operating system version information I want in the json file
Any help would be much appreciated! Here is my my script code:
So I am trying to pull information from a Cisco switch output using show commands and then to store just some of the information from the output into a json file.
My script is using Python 3 and is being run on a Debian virtual machine that is connected to my physical switch. My script connects to the switch using SSH just fine and also input the correct show commands to display the information I want. But when my script tries to collect some of the information I want to save to a json file I get this error message:
raise TypeError(repr(o) + " is not JSON serializable")
TypeError: b's1' is not JSON serializable
s1 is the host name of my switch and I want this information in the json file.
If I then run it again straight after I also get another error message:
raise TypeError(repr(o) + " is not JSON serializable")
TypeError: b'12.1(22)EA14' is not JSON serializable
12.1(22)EA14 is the Operating system version information I want in the json file
Any help would be much appreciated! Here is my my script code:
import json import paramiko import os.path import subprocess import datetime import time import sys import re #Open ssh connection ip_address = "192.168.2.111" username = "admin01" password = "cisco12345" ssh_client = paramiko.SSHClient() ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) ssh_client.connect(hostname=ip_address,username=username,password=password) remote_connection = ssh_client.invoke_shell() remote_connection.send(b"terminal length 0\n") time.sleep(1) #Cisco commands to extract required parameters selected_cisco_commands = b'''show version | include (, Version|uptime is|bytes of memory|Hz)&\ show inventory&\ show processes cpu | include CPU utilization show memory statistics''' command_list = selected_cisco_commands.split(b"&") for each_line in command_list: remote_connection.send(each_line + b'\n') time.sleep(3) #Checking command output for IOS syntax errors output = remote_connection.recv(65535) if re.search(b"% Invalid input detected at", output): print ("* There was at least one IOS syntax error on device %s") else: print ("* All parameters were extracted from device %s") print (output.decode('ascii') + "\n") #Extracting Device Parameters # hostname dev_hostname = re.search(b"(.+) uptime is", output) hostname = dev_hostname.group(1) print (hostname.decode('ascii')) # model dev_model = re.search(b"(.+?) (.+?) (.+) bytes of memory", output) model = dev_model.group(2) print (model.decode('ascii')) # version dev_version = re.search(b"Version (.+), RELEASE SOFTWARE", output) version = dev_version.group(1) print (version.decode('ascii')) joson_object = {"ip": ip_address,"hostname" : hostname, "model" : model, "version" : version} with open ("josondata.json", "w") as json_file: json.dump(joson_object, json_file) print("Etracting for all devices.") #Closing the SSH connection ssh_client.close()