Python Forum
Getting error "Type error-a bytes-like object..."
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.
Reply
#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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  error in class: TypeError: 'str' object is not callable akbarza 2 456 Dec-30-2023, 04:35 PM
Last Post: deanhystad
  Wrong type error rowan_bradley 6 1,146 Aug-07-2023, 10:44 AM
Last Post: rowan_bradley
  Error on import: SyntaxError: source code string cannot contain null bytes kirkwilliams2049 7 6,173 Aug-03-2023, 06:00 PM
Last Post: Gribouillis
  Need help with 'str' object is not callable error. Fare 4 778 Jul-23-2023, 02:25 PM
Last Post: Fare
  Type Error: Unsupported Operand jhancock 2 1,068 Jul-22-2023, 11:33 PM
Last Post: jhancock
  cx_oracle Error - AttributeError: 'function' object has no attribute 'cursor' birajdarmm 1 2,216 Apr-15-2023, 05:17 PM
Last Post: deanhystad
  TypeError: a bytes-like object is required ZeroX 13 3,841 Jan-07-2023, 07:02 PM
Last Post: deanhystad
  [ERROR] ParamValidationError: Parameter validation failed: Invalid type for parameter gdbengo 3 10,660 Dec-26-2022, 08:48 AM
Last Post: ibreeden
  declaring object parameters with type JonWayn 2 858 Dec-13-2022, 07:46 PM
Last Post: JonWayn
  TypeError: a bytes-like object is required, not 'str' - Help Please. IanJ 3 4,674 Aug-29-2022, 05:53 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

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