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
  TypeError: 'type' object is not subscriptable Stef 1 182 Aug-28-2020, 03:01 PM
Last Post: Gribouillis
  subprogram issues: cannot unpack non-iterable function object error djwilson0495 13 404 Aug-20-2020, 05:53 PM
Last Post: deanhystad
  Error in Int object is not subscript-able. How to debug this ? yanDvator 1 232 Aug-03-2020, 02:28 PM
Last Post: Larz60+
Bug maximum recursion depth exceeded while calling a Python object error in python3 Prezess 4 314 Aug-02-2020, 02:21 PM
Last Post: deanhystad
  how to solve "a bytes-like object is required, not 'str'" error maiya 2 194 Jul-28-2020, 07:03 PM
Last Post: bowlofred
  isinstance() always return true for object type check Yoki91 2 251 Jul-22-2020, 06:52 PM
Last Post: Yoki91
  Type Error or Value Error? spalisetty06 3 203 Jul-21-2020, 04:56 AM
Last Post: deanhystad
  TypeError: a bytes-like object is required, not 'str' ozzy69 1 226 Jul-17-2020, 03:38 PM
Last Post: stullis
  Packet Sniffer - TypeError: a bytes-like object is required, not 'str' cheffa2020 4 493 Jun-12-2020, 02:10 PM
Last Post: cheffa2020
  TypeError: a bytes-like object is required, not 'str' - Help Please. IanJ 1 579 May-19-2020, 07:15 PM
Last Post: Larz60+

Forum Jump:


Users browsing this thread: 1 Guest(s)