gpt4 book ai didi

python - 运行 Python 脚本时出现错误

转载 作者:太空宇宙 更新时间:2023-11-03 21:05:03 25 4
gpt4 key购买 nike

运行下面的 Python 脚本时,出现错误:

 [root@localhost ~]# cat pythonR5script1
import getpass
import sys
import telnetlib

HOST = "100.100.100.1"
user = raw_input("Enter your telnet username: ")
password = getpass.getpass()

tn = telnetlib.Telnet(HOST)

tn.read_until("username: ")
tn.write(user + "\r\n")
if password:
tn.read_until("Password: ")
tn.write(password + "\r\n")

tn.write("enable\r\n")
tn.write("cisco\r\n")
tn.write("conf t\r\n")
tn.write("int loop 0\r\n")
tn.write("ip add 200.200.200.1 255.255.255.255\r\n")
tn.write("end\r\n")
tn.write("exit\r\n")


print tn.read_all()

这是我收到的错误:

[root@localhost ~]# python pythonR5script1 
Enter your telnet username: alan
Password:
Traceback (most recent call last):
File "pythonR5script1", line 14, in <module>
tn.read_until("Password: ")
File "/usr/local/lib/python2.7/telnetlib.py", line 294, in read_until
return self._read_until_with_poll(match, timeout)
File "/usr/local/lib/python2.7/telnetlib.py", line 343, in _read_until_with_poll
return self.read_very_lazy()
File "/usr/local/lib/python2.7/telnetlib.py", line 455, in read_very_lazy
raise EOFError, 'telnet connection closed'
EOFError: telnet connection closed

请帮我解决这个问题。

最佳答案

您正在写入字符串,而写入函数需要字节字符串。

https://docs.python.org/3/library/telnetlib.html?highlight=telnetlib#telnetlib.Telnet.write

Telnet.write(buffer) Write a byte string to the socket, doubling any IAC characters. This can block if the connection is blocked. May raise OSError if the connection is closed.

对发送到服务器的字符串执行encode()函数。

tn.write('{}\r\n'.format(user).encode())

关于python - 运行 Python 脚本时出现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55469811/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com