【问题标题】:Kill a process in a telnet connection using a python script使用 python 脚本杀死 telnet 连接中的进程
【发布时间】: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


    【解决方案1】:

    你需要发送telnetlib.IP来中断正在运行的进程。

    更改您的代码以执行此操作:

    connection.write(telnetlib.IP)
    connection.close()
    

    【讨论】:

      猜你喜欢
      • 2016-07-08
      • 2016-01-24
      • 1970-01-01
      • 1970-01-01
      • 2012-10-11
      • 2012-11-22
      • 1970-01-01
      • 2013-07-24
      • 1970-01-01
      相关资源
      最近更新 更多