我正在尝试创建一个 telnet 客户端,它将尝试默认用户名和密码并返回正确的用户名,但似乎 pythontelnetlib Telnet.expect()方法没有捕捉到我不明白为什么的正则表达式模式。相反,期望始终返回 -1 作为第一个索引,这意味着未找到该模式。
try:
result = telnet.expect([b"(?i)login:", b"(?i)password:"], timeout=2)
if result[0] == 0:
<..snip..>
# This is the important part which get executed.I'm sure that python
# enter this part of elif print statement get's executed
elif result[0] == 1:
telnet.write(bytes(password, encoding="ascii"))
result = telnet.expect([b"(?i)Login OK!"], timeout=3)
# This print output is
print(result[2])
if result[0] == 0:
print(f"[+] Telnet Host:{username}:{port}, username:{username} password:{password}")
我的 Linux telnet 客户端可以正常工作
telnet 192.168.1.104 23456
Trying 192.168.1.104...
Connected to 192.168.1.104.
Escape character is '^]'.
Welcome to Telnetd :-)
Password:root
*
Login OK!
:/ $
result[2] 的打印输出为:
b'\x08 \x08*\x08 \x08*\x08 \x08*\x08 \x08*\x08 \x08*\x08 \x08*'
b'\x08 \x08*\x08 \x08*\x08 \x08*\x08 \x08*'
b'\x08 \x08*\x08 \x08*\x08 \x08*\x08 \x08*'
b'\x08 \x08*\x08 \x08*\x08 \x08*\x08 \x08*\x08 \x08*\x08 \x08*\x08 \x08*\x08 \x08*'
b'\x08 \x08*\x08 \x08*\x08 \x08*\x08 \x08*\x08 \x08*\x08 \x08*\x08 \x08*\x08 \x08*'
<..snip..>
我想问题出在我应该捕获的正则表达式上,Login OK!但我看不出那个错误是什么。