telnetlib

How do I send telnetlib control + c command in python

此生再无相见时 提交于 2021-02-07 09:13:08
问题 I am trying to send control + c command in python using telnetlib library. Currently I am doing tn.write('^]') But the code above doesn't seem to work. Any clue on What I should use? 回答1: Try ASCII characters of control+c to the telnet connection below:- tn.write('\x03') 回答2: ctrl+a = decimal 1 ctrl+b = decimal 2 ctrl+c = decimal 3 and so on.. to ctrl+x = decimal 24 ctrl+y = decimal 25 ctrl+z = decimal 26 escape = 27 etc Still, for those who will need this in the future Arrow keys are (3

Python reading until null character from Telnet

房东的猫 提交于 2020-01-16 00:54:28
问题 I am telneting to my server, which answers to me with messages and at the end of each message is appended hex00 (null character) which cannot be read. I tried searching through and through, but can't seem to make it work, a simple example: from telnetlib import Telnet connection = Telnet('localhost', 5001) connection.write('aa\n') connection.read_eager() This returns an output: 'Fail - Command aa not found.\n\r' whereas there should be sth like: 'Fail - Command aa not found.\n\r\0' Is there

Detect a closed connection in python's telnetlib

时间秒杀一切 提交于 2020-01-02 06:37:06
问题 I'm using python's telnetlib to connect to a remote telnet server. I'm having a hard time detecting if the connection is still open, or if the remote server closed it on me. I will notice the connection is closed the next time I try to read or write to it, but would like to have a way to detect it on demand. Is there a way to send some sort of an 'Are You There' packet without affecting the actual connection? The telnet RFC supports an "are you there" and "NOP" commands - just not sure how to

Send commands to a remote machine application with Python

家住魔仙堡 提交于 2019-12-24 21:42:17
问题 I want to do the following in Python. Connect to a remote machine open an application there and then send some commands to this application. My idea was to do it through telnetlib and subprocess. I managed to connect to the machine and start the application (with telnetlib alone), but I am not sure how to continue. Is it possible? P.S. I am also open to ideas to do it another way, but I would prefer to do it with python. Thanks in advance! 回答1: you can do the following: child = pexpect.spawn(

Python telnetlib read_until returns cut off string

谁说胖子不能爱 提交于 2019-12-24 00:57:27
问题 [Python telnetlib read_until recv problem] "read_until" function returns cut off string with long commands. The commands ran perfectly, but it does not show full texts. How can I fix this? Please help. # my code tn = telnetlib.Telnet(ip, 23, 5) prompt = ']# ' tn.write('echo "this is a long command for the test purpose... > test.txt"\n') print tn.read_until(prompt, 1) # debug output Telnet(192.168.220.4,23): send 'echo "this is a long command for the test purpose... > test.txt"\n' Telnet(192

Python telnetlib: surprising problem

会有一股神秘感。 提交于 2019-12-21 19:44:26
问题 I am using the Python module telnetlib to create a telnet session (with a chess server), and I'm having an issue I really can't wrap my brain around. The following code works perfectly: >>> f = login("my_server") #code for login(host) below. >>> f.read_very_eager() This spits out everything the server usually prints upon login. However, when I put it inside a function and then call it thus: >>> def foo(): ... f = login("my_server") ... return f.read_very_eager() ... >>> foo() I get nothing

Python Telnetlib read_until '#' or '>', multiple string determination?

瘦欲@ 提交于 2019-12-13 13:45:16
问题 if (tn.read_until('>')): action1 else: action2 or if (tn.read_until() == '>'): action1 else: action2 I just want the read_until() to check which desired String comes first, and do different actions. Or is there any equivalent ways? 回答1: Look at the docs. Read until wants the expected string as a positional argument and an optional timeout. I would do it like this: >>> try: ... response = tn.read_until(">", timeout=120) #or whatever timeout you choose. ... except EOFError as e: ... print

telnetlib and “buf = self.sock.recv(50)” error

为君一笑 提交于 2019-12-12 04:45:24
问题 I am using telnetlib for simple telnet script to Juniper switch. Below is my code: import telnetlib HOST = raw_input("Enter host IP address: ") USER = raw_input("Enter Username: ") PWD = raw_input("Enter Password: ") TNT = telnetlib.Telnet(HOST, 23, 10) TNT.read_until("login:") TNT.write(USER.encode('ascii') + "\n") TNT.read_until("Password:") TNT.write(PWD.encode('ascii') + "\n") TNT.write("set cli screen-length 10000\nconfigure\nshow\nexit\n") print (TNT.read_all().decode('ascii')) TNT

Python - Telnet closes before waiting for a print read function

六眼飞鱼酱① 提交于 2019-12-12 04:34:22
问题 I am making a python script that conects to Teamspeak 3 Server Query telnet and listens out for commands (I haven't figured out the command part yet). But right now the telnet doesn't wait till a response is made, should i be using another library or is there a telnet object I can use to keep it open? Code: __author__ = 'Khailz' import telnetlib, socket, time, ConfigParser, time #login Credidentials config = ConfigParser.ConfigParser() config.readfp(open(r'info.conf')) username = config.get(

How to check if Telnet connection is still established? using telnetlib

前提是你 提交于 2019-12-10 15:44:23
问题 I'd like to check if a connection using the telnetlib is still up. The way I do it is to send a ls command and check the answer, but I'm sure there must be a smoother solution. 回答1: I've got the idea from here, so kudos to them, the code could be something like this def check_alive(telnet_object): try: if telnet_object.sock: telnet_object.sock.send(telnetlib.IAC + telnetlib.NOP) telnet_object.sock.send(telnetlib.IAC + telnetlib.NOP) telnet_object.sock.send(telnetlib.IAC + telnetlib.NOP)