Script explanation: I connect to network routers. I execute 3 commands and save output for later examination.
Problem:
I am getting an error related to "type error". I did not read or saved this as a file. The 'output' is being generated and stored in memory for the respective show commands. Please advise if you know how to get this resolved. Thank you.
myserver@ubuntu:~/scripts/trunk$ python3 mcast.py
Enter password(secured):
Password:
* SSH connection established to 172.18.120.185
Therefore I am puzzled on how to solve this.
script:
Question: Can I convert this form bytes to string - if at all possible?
I think I answered my own question:
I did
output_str = output.decode('utf-8')
Problem solved.
Problem:
I am getting an error related to "type error". I did not read or saved this as a file. The 'output' is being generated and stored in memory for the respective show commands. Please advise if you know how to get this resolved. Thank you.
myserver@ubuntu:~/scripts/trunk$ python3 mcast.py
Enter password(secured):
Password:
* SSH connection established to 172.18.120.185
Error:Traceback (most recent call last):
File "mcast.py", line 49, in <module>
main()
File "mcast.py", line 46, in main
get_analysis_int = olist(get_platform_data)
File "mcast.py", line 13, in olist
olist_data = raw_show_data.split('#show')
TypeError: a bytes-like object is required, not 'str'
Note: If I attempt to print('raw_show_data), it prints without problems. Therefore I am puzzled on how to solve this.
script:
import paramiko import os import sys import re import getpass import time USERNAME = 'admin' TARGET_SERVER = '1.18.121.185' def olist(raw_show_data): #get output for show platform. I have the raw content of 'show' commands here, 3 different commands. Now I will split it: olist_data = raw_show_data.split('#show') <== my goal is to get content for each 'show command'. print(olist_data[0]) def ssh_nodes(myuser, host,mcastgroup): inputpass = input('Enter password(secured): ') inputpass = getpass.getpass() remote_conn_pre = paramiko.SSHClient() remote_conn_pre remote_conn_pre.set_missing_host_key_policy(paramiko.AutoAddPolicy()) host = host.strip() remote_conn_pre.connect(host, username=myuser,password=inputpass,look_for_keys=False,allow_agent=False) print(f'* SSH connection established to {host}') remote_conn = remote_conn_pre.invoke_shell() remote_conn.send('terminal length 0\n') remote_conn.send('show mrib route %s \n' %(mcastgroup)) time.sleep(15) remote_conn.send('show mfib route %s \n' %(mcastgroup)) time.sleep(15) remote_conn.send('show platform \n') time.sleep(60) output=remote_conn.recv(65535) time.sleep(30) remote_conn.close() output = output.strip() return output def main(): mgroup = "224.0.0.0/24" get_platform_data = ssh_nodes(USERNAME, TARGET_SERVER, mgroup) get_analysis_int = olist(get_platform_data) sys.exit(0) #end====================================================== [hr]Update: to my surprise, I did:
def olist(raw_show_data): [b] print(type(raw_show_data)[/b]) #get output for show platform. I have the raw content of 'show' commands here, 3 different commands. Now I will split it: olist_data = raw_show_data.split('#show') <== my goal is to get content for each 'show command'. print(olist_data[0])and the result is that indeed that is:<class 'bytes'>
Question: Can I convert this form bytes to string - if at all possible?
I think I answered my own question:
I did
output_str = output.decode('utf-8')
Problem solved.