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
  object is not subscriptable... Error Message? Shafla 3 199 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 135 Sep-22-2019, 05:49 PM
Last Post: ndc85430
  Object of type Scoreboard is not JSON serializable lsepolis123 9 692 Aug-13-2019, 11:22 AM
Last Post: lsepolis123
  Type Error: bad operand type for unary +: 'str' Psypher1 2 501 Aug-12-2019, 02:30 PM
Last Post: Psypher1
  catch input type error mcmxl22 5 231 Aug-11-2019, 07:33 AM
Last Post: wavic
  Error :unable to detect undefined names created in spyder ide error at line 2 milind_eac 2 235 Jul-30-2019, 10:29 PM
Last Post: milind_eac
  Object and type class Uchikago 2 176 Jul-28-2019, 10:35 AM
Last Post: DeaD_EyE
  Type error: dump() missing 1 required positional argument: fp jaycuff13 2 1,005 Jul-13-2019, 10:21 AM
Last Post: jaycuff13
  Error: int object does not support item assignment ankita_nthu 2 441 Jul-07-2019, 02:14 PM
Last Post: ankita_nthu
  call dict object result key error lateublegende 2 329 May-15-2019, 01:08 PM
Last Post: lateublegende

Forum Jump:


Users browsing this thread: 1 Guest(s)