Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Getting error "Type error-a bytes-like object..."
#1
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
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.
Larz60+ wrote Apr-06-2019, 11:15 AM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.

Fixed for you this time. Please use BBCode tags on all future posts. Thank You
Quote
#2
Appreciated Larz.

(Apr-06-2019, 07:04 AM)mrapple2020 Wrote: 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
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.
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  AttributeError: type object 'FunctionNode' has no attribute '_TestValidateFuncLabel__ binhduonggttn 0 46 Yesterday, 11:29 AM
Last Post: binhduonggttn
  Python Type error! SamAnw 4 106 Feb-14-2020, 03:46 PM
Last Post: buran
  Sympy error "object is not callable" Cupcake 0 97 Feb-08-2020, 02:22 AM
Last Post: Cupcake
  Type hinting - return type based on parameter micseydel 2 119 Jan-14-2020, 01:20 AM
Last Post: micseydel
  how can i handle "expected a character " type error , when I input no character vivekagrey 2 152 Jan-05-2020, 11:50 AM
Last Post: vivekagrey
  Object of type set is not JSON serializable enigma619 5 609 Dec-19-2019, 03:30 PM
Last Post: enigma619
  type of object as a string Skaperen 1 164 Dec-02-2019, 08:01 AM
Last Post: midarq
  ERROR:haproxystats:Error fetching stats deepakkr3110 1 129 Nov-12-2019, 12:29 PM
Last Post: Larz60+
  object is not subscriptable... Error Message? Shafla 3 824 Sep-25-2019, 12:27 AM
Last Post: buran
  Reading DBF files from Amazon s3 throwing error - 'int' object has no attribute 'isa abeesm 1 290 Sep-22-2019, 05:49 PM
Last Post: ndc85430

Forum Jump:


Users browsing this thread: 1 Guest(s)