【发布时间】:2020-04-17 07:42:35
【问题描述】:
我有一些与 python3 中的 telnet 相关的问题。会发生 TypeError:不能在类似字节的对象上使用字符串模式。 有谁知道如何解决这个问题?如果使用python2不会有问题。
代码只是telnet到cisco服务器并打印mac的ipv6地址。
def Telnet_Check_reachability(ip):
ping_count=3
process = subprocess.Popen(['ping', ip, '-n', str(ping_count)],
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT)
process.wait()
stdout = process.stdout.read()
#print stdout
if "TTL=" in stdout:
#print "Server reachable"
successful = 1
else:
#print "Server unreachable"
successful = 0
return successful
def telnet_To_CMTS(Client_IP, Client_Name, Client_Pwd, MAC):
tn =Login_Telnet(Client_IP, Client_Name, Client_Pwd)
if "telnetlib" in str(tn):
time.sleep(1)
value = tn.read_until(b"Router#")
command = "scm " + MAC + " ipv6\n"
tn.write(command.encode('ascii') + b"\n")
#tn.write(command)
value = tn.read_until(b"Router#")
#print value
tn.close()
time.sleep(1)
info = "2001"
#value=str(value)
matchObj = re.match(r'.*'+ info + '(.*)\n',value, re.M|re.DOTALL)
if matchObj:
Ipv6_address = info + matchObj.group(1)
Ipv6 = Ipv6_address.replace("\n", "")
return Ipv6
else:
print ("No match!!")
else:
print ("Telnet failed")
ip ="192.168.1.252"
username = "guest"
password = "guest"
mac = "xxxx.bbbb.cccc"
new_IPv6 = telnet_To_CMTS(ip, username, password, mac)
#print (new_IPv6)
【问题讨论】:
-
你能添加整个引用吗?
-
文件“telnet_CMTS2.py”,第 16 行,在 Telnet_Check_reachability 中,如果标准输出中的“TTL=”:TypeError:需要类似字节的对象,而不是“str”
标签: python python-3.x