Telnet不适用于操作系统调用



为什么我不能使用os在python中调用以下内容?

import os
os.system('telnet 1.1.1.1')

然而,当我打开一个终端并使用确切的命令时,我可以telnet。运行代码时,我得到以下信息:

"telnet"未被识别为内部或外部命令,可操作程序或批处理文件。

我已启用TelnetClient和TelnetServer

@Jon S.:像Telnet和SSH这样的程序本质上是交互式的。因此,当您打开telnet时,它将从它正在运行的shell返回一些内容来处理该特定连接。我没有试过像你上面提到的那样运行命令,但我过去在Windows中成功地使用了python的"telnetlib",现在仍在使用它。

import telnetlib
import os
host_ip = "1.1.1.1"
user = "user"
password = "password"
tnet_hndl = telnetlib.Telnet(host_ip)
print (tnet_hndl.read_until(b"login: "))
tnet_hndl.write(user.encode('ascii') + b"n")
print (tnet_hndl.read_until(b"Password: "))
tnet_hndl.write(password.encode('ascii') + b"n")
print (tnet_hndl.read_until(b"# "))
#tnet_hndl.set_debuglevel(1) #enable this if you want to debug more
tnet_hndl.write(b"<enter some windows cmd here>" + b"n")
print (tnet_hndl.read_until(b"# "))
tnet_hndl.close()

希望这能有所帮助。

只需尝试os.system('C:/Windows/System32/telnet.exe 1.1.1.1 <port number>'),看看会发生什么?

相关内容

最新更新