Python Forum
Output to a json file problem
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Output to a json file problem
#1
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:

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() 
Reply
#2
JSON does not serialize the bytes type. you need to convert these to regular strings.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#3
Thank you for your reply, but I am not exactly sure how to do that. I have never used python with json before. Can you give me an example?
Reply
#4
you might what to look at the documentation for the decode() function.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  encrypt data in json file help jacksfrustration 1 191 Mar-28-2024, 05:16 PM
Last Post: deanhystad
  problem in output of a snippet code akbarza 2 357 Feb-28-2024, 07:15 PM
Last Post: deanhystad
  output shape problem with np.arange alan6690 5 668 Dec-26-2023, 05:44 PM
Last Post: deanhystad
  parse json field from csv file lebossejames 4 725 Nov-14-2023, 11:34 PM
Last Post: snippsat
  problem in output of a function akbarza 9 1,178 Sep-29-2023, 11:13 AM
Last Post: snippsat
  Python Script to convert Json to CSV file chvsnarayana 8 2,496 Apr-26-2023, 10:31 PM
Last Post: DeaD_EyE
  Loop through json file and reset values [SOLVED] AlphaInc 2 2,097 Apr-06-2023, 11:15 AM
Last Post: AlphaInc
  Reading data from excel file –> process it >>then write to another excel output file Jennifer_Jone 0 1,089 Mar-14-2023, 07:59 PM
Last Post: Jennifer_Jone
  Converting a json file to a dataframe with rows and columns eyavuz21 13 4,400 Jan-29-2023, 03:59 PM
Last Post: eyavuz21
  Python Pandas Syntax problem? Wrong Output, any ideas? Gbuoy 2 920 Jan-18-2023, 10:02 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020