Home > Enterprise >  TypeError in Python3.10 script
TypeError in Python3.10 script

Time:12-25

Hello Friends,

I am new to programming and I am trying to do network automation task using python but ran with type error while running the below given script!!! Thanks in advance for your help !

I AM STUCK AT SAME KIND OF ERROR, CAN YOU GUYS PLEASE HELP ME OUT HOW TO FIX THIS ?

durai@durai-virtual-machine:~/Network Automation$ python3 session_details.py 
devices list: ['1.1.1.1', '2.2.2.2', '6.6.6.6', '7.7.7.7', '', '']
establishing telnet session: 1.1.1.1 cisco cisco
--- connected to:  1.1.1.1
--- getting version information
Traceback (most recent call last):
  File "/home/durai/Network Automation/session_details.py", line 82, in <module>
    device_version = get_version_info(session)
  File "/home/durai/Network Automation/session_details.py", line 65, in get_version_info
    version_output_parts = version_output_lines[1].split(',')
**TypeError: a bytes-like object is required, not 'str'**
durai@durai-virtual-machine:~/Network Automation$

THE BELOW IS THE BLOCK OF CODE WHERE I AM GETTING ERROR !

#-----------------------------------------------------------------------
def get_version_info(session):

    print ('--- getting version information')

    session.sendline('show version | include Version')
    result = session.expect(['>', pexpect.TIMEOUT])

    # Extract the 'version' part of the output
    version_output_lines = session.before.splitlines()
    version_output_parts = version_output_lines[1].split(',')
    version = version_output_parts[2].strip()

    print ('--- got version: ', version)
    return version

#-----------------------------------------------------------------------

FULL CODE FOR YOUR REFERENCE:

#!/usr/bin/python

import re
import pexpect

#-----------------------------------------------------------------------
def get_devices_list():

    devices_list = []
    file = open('devices', 'r')

    for line in file:
        devices_list.append( line.rstrip() )

    file.close()

    print ('devices list:', devices_list)
    return devices_list

#-----------------------------------------------------------------------
def connect(ip_address, username, password):

    print ('establishing telnet session:', ip_address, username, password)
    telnet_command = 'telnet '   ip_address

    # Connect via telnet to device
    session = pexpect.spawn('telnet '   ip_address, timeout=20)
    result = session.expect(['Username:', pexpect.TIMEOUT])

    # Check for error, if so then print error and exit
    if result != 0:
        print ('!!! TELNET failed creating session for: ', ip_address)
        exit()

    # Enter the username, expect password prompt afterwards
    session.sendline(username)
    result = session.expect(['Password:', pexpect.TIMEOUT])

    # Check for error, if so then print error and exit
    if result != 0:
        print ('!!! Username failed: ', username)
        exit()

    session.sendline(password)
    result = session.expect(['>', pexpect.TIMEOUT])

    # Check for error, if so then print error and exit
    if result != 0:
        print ('!!! Password failed: ', password)
        exit()

    print ('--- connected to: ', ip_address)
    return session

#-----------------------------------------------------------------------
def get_version_info(session):

    print ('--- getting version information')

    session.sendline('show version | include Version')
    result = session.expect(['>', pexpect.TIMEOUT])

    # Extract the 'version' part of the output
    version_output_lines = session.before.splitlines()
    version_output_parts = version_output_lines[1].split(',')
    version = version_output_parts[2].strip()

    print ('--- got version: ', version)
    return version

#-----------------------------------------------------------------------

devices_list = get_devices_list()    # Get list of devices

version_file_out = open('version-info-out', 'w')

# Loop through all the devices in the devices list
for ip_address in devices_list:

    # Connect to the device via CLI and get version information
    session = connect(ip_address, 'cisco', 'cisco')
    device_version = get_version_info(session)

    session.close()  # Close the session

    version_file_out.write('IP: ' ip_address '  Version: ' device_version '\n')

# Done with all devices and writing the file, so close
version_file_out.close()

CodePudding user response:

Python has two different kinds of strings. Normal strings are Unicode, where the individual characters are several bytes long, to handle Unicode characters. Many activities (like networking) need to use byte strings, which are written b"abc".

That's the problem here. The pexpect module is returning a byte string. So, in this line:

version_output_parts = version_output_lines[1].split(',')

version_output_parts is a byte string, but ',' is a Unicode string, and you can't mix them. So, you can either keep it bytes and do this:

version_output_parts = version_output_lines[1].split(b',')

or you can convert it to a Unicode string:

version_output_parts = version_output_parts.decode('utf-8')
version_output_parts = version_output_lines[1].split(b',')

It depends on how much you need to do with the parts.

CodePudding user response:

Its the part where you extract the version, is bugged.

Try using print statements as demonstrated below to check the data types of your variables at each step.

This Error suggests that you are using a method which is not available for the given data type e.g. calling a string method on an array or so on.

def get_version_info(session):

print ('--- getting version information')

session.sendline('show version | include Version')
result = session.expect(['>', pexpect.TIMEOUT])

# Extract the 'version' part of the output
version_output_lines = session.before.splitlines()

# print(version_output_lines)
version_output_parts = version_output_lines[1].split(',')
# print(version_output_parts)
version = version_output_parts[2].strip()
# print(version) 
print ('--- got version: ', version)
return version
  • Related