简体   繁体   中英

Kill a process in a telnet connection using a python script

I've established a telnet connection and give commands to start following in real time some data. Before closing the connection I have to kill the process first. Manually this can be done with Ctrl + C but I don't know how to do it from .py script. Can anyone have any idea on how to do this? Below you can find the code I've used. Thank you! Ramona

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

You need to send telnetlib.IP to interrupt the running process.

Change your code to do this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM