你好朋友,
我是編程新手,我正在嘗試使用 python 執行網路自動化任務,但在運行以下給定腳本時出現型別錯誤!!!在此先感謝您的幫助 !
我遇到了同樣的錯誤,你們能幫我解決這個問題嗎?
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$
以下是我出錯的代碼塊!
#-----------------------------------------------------------------------
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
#-----------------------------------------------------------------------
供您參考的完整代碼:
#!/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()
uj5u.com熱心網友回復:
Python 有兩種不同的字串。普通字串是 Unicode,其中單個字符有幾個位元組長,用于處理 Unicode 字符。許多活動(如網路)需要使用位元組字串,它們被寫入b"abc".
這就是問題所在。該pexpect模塊正在回傳一個位元組字串。所以,在這一行:
version_output_parts = version_output_lines[1].split(',')
version_output_parts是位元組串,但','是 Unicode 串,不能混用。因此,您可以保留位元組并執行以下操作:
version_output_parts = version_output_lines[1].split(b',')
或者您可以將其轉換為 Unicode 字串:
version_output_parts = version_output_parts.decode('utf-8')
version_output_parts = version_output_lines[1].split(b',')
這取決于您需要對零件進行多少處理。
uj5u.com熱心網友回復:
它是您提取版本的部分,被竊聽了。
嘗試使用如下所示的列印陳述句來檢查每個步驟中變數的資料型別。
此錯誤表明您正在使用對給定資料型別不可用的方法,例如在陣列上呼叫字串方法等。
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
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/394963.html
標籤:Python 乌本图 运行时错误 类型错误 python-3.10
下一篇:API-對外部物件進行排序
