【发布时间】:2018-08-18 02:08:37
【问题描述】:
我已经建立了一个 telnet 连接并发出命令开始实时跟踪一些数据。 在关闭连接之前,我必须先终止该进程。 这可以通过 Ctrl+C 手动完成,但我不知道如何从 .py 脚本中完成。 谁能知道如何做到这一点? 您可以在下面找到我使用的代码。 谢谢! 雷蒙娜
import telnetlib
def open_telnet_connection():
try:
username = 'username'
password = 'password'
ip = 'xx.xx.xx.xx'
reading_timeout = 5
connection = telnetlib.Telnet(ip)
console_output = connection.read_until("ENTER USERNAME <", reading_timeout)
print console_output
connection.write(username + '\r\n')
console_output = connection.read_until("ENTER PASSWORD <", reading_timeout)
connection.write(password + '\r\n')
print console_output
connection.write("***My command to follow the process***")
console_output = connection.read_until("here I can put any text, I use this only to get the output", 600)
print console_output
connection.write('***Command instead of Ctrl-C***') # ***????***
connection.close()
except IOError:
print "connection not established"
if __name__ == '__main__':
open_telnet_connection()
【问题讨论】:
标签: python telnet kill-process telnetlib