Telnet不适用于os.system调用

时间:2013-07-23 17:43:10

标签: python command-line telnet os.system telnetlib

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

import os
os.system('telnet 1.1.1.1')

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

  

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

我启用了TelnetClient和TelnetServer

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

@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()

希望这有帮助。

相关问题