最近在做SSH实验内容,运行完后发现python总是给我报一个TypeError: expected string or bytes-like object的错误,各位大佬,这样的情况应该如何解决呢?
DATACOM.py文件
import os.path
import time
import os
import re
from datetime import datetime
import paramiko
from configpy import switch_info,parameter_dict
class Datacom:
def __init__(self,ip,port,sysname,username,password):
self.ip=ip
self.port=port
self.sysname=sysname
self.username=username
self.password=password
self.ssh_session=self.transport()
self.vty=self.ssh_session.invoke_shell()
def transport(self):
ssh_session = paramiko.SSHClient()
ssh_session.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh_session.connect(hostname=self.ip, port=self.port, username=self.username, password=self.password,
look_for_keys=False, allow_agent=False)
return ssh_session
def command_send(self, command):
self.vty.send(command+"\n")
time.sleep(1)
res=self.vty.recv(99999).decode("utf-8")
print(res)
def download(self):
filename = "%s_%s.zip"%(datetime.now().strftime("%Y_%m_%d_%H_%M_%S"),self.sysname)
tran = paramiko.Transport((self.ip,self.port))
tran.connect(username=self.username,password=self.password)
sftp = paramiko.SFTPClient.from_transport(tran)
sftp.get("/vrpcfg.zip",os.path.join(r"D:\pywenjian",filename))
sftp.close()
return "%s download successfully!"%filename
def parameter_get(self,parameter):
pat=re.compile(parameter_dict[parameter]["re"])
content = self.command_send(parameter_dict[parameter]["command"])
print(content)
res = pat.findall(content)
print(res)
a = Datacom(**switch_info)
a.parameter_get("lacp_state")
#print(b)
#res=a.command_send(command="dis ospf peer b")
#print(res)
#b=a.download()
#print(b)
#a.command_send(command="dis cu")
switch_info = {"ip": "192.168.150.1", "port": 22, "username": "python", "password": "Huawei@123",
"sysname": "X_T1_AGG1"}
parameter_dict = {"cpu_usage": {"command": "display cpu-usage\n", "re": "CPU Usage.+?%"},
"power": {"command": "display power\n", "re": "NO"},
"fan_state": {"command": "display fan\n", "re": "Fan 0 is absent"},
"ospf_state": {"command": "display ospf peer brief\n",
"re": "Down|Init|2-way|Exstart|Exchange|Loading|Full"},
"memory_usage": {"command": "display memory-usage\n", "re": "Memory Using.+?%"},
"lacp_state": {"command": "display eth-trunk 1\n", "re": "Up|Down"}}
运行完后总是给出这样的报错:
大佬们有没有办法