Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to print telnet response line by line?

Is it possible to print the telnet response line by line, when a command executed over telnet keeps on responding over console ?

Example: I have executed a command (to collect logs), It keeps on displaying logs on console window. Can we read the response line by line & print it , without missing any single line ?

Below snippet writes the log, but only after certain specified time. If I stop the service/script (CTRL-C) in between, that doesn't write anything.

import sys
import telnetlib
import time


orig_stdout = sys.stdout
f = open('outpuy.txt', 'w')
sys.stdout = f

try:
        tn = telnetlib.Telnet(IP)
        tn.read_until(b"pattern1")
        tn.write(username.encode('ascii') + b"\n")
        tn.read_until(b"pattern2")
        tn.write(command1.encode('ascii') + b"\n")
        z = tn.read_until(b'abcd\b\n',600)
        array = z.splitlines( )
except:
        sys.exit("Telnet Failed to ", IP)

for i in array:
        i=i.strip()
        print(i)

sys.stdout = orig_stdout
f.close()
like image 509
Jackie James Avatar asked Dec 31 '14 11:12

Jackie James


People also ask

What is Read_until in telnet?

read_until - Read until a given string, expected, is encountered or until timeout seconds have passed. Telnet. write - Write a string to the socket, doubling any IAC characters. This can block if the connection is blocked.

What is Telnetlib?

The telnetlib module provides a Telnet class that implements the Telnet protocol. See RFC 854 for details about the protocol. In addition, it provides symbolic constants for the protocol characters (see below), and for the telnet options.

How do I install Telnetlib?

The telnetlib module provides a Telnet class that implements the Telnet protocol. If you have Python installed, the telnetlib library is already installed, but if it isn't, we can use the pip command to install it. Alternatively, you can use the code below to see if it's installed on your system: Python.

How do I check the HTTP response in Telnet?

Telnet – Check HTTP Response Use the following syntax to connect to a [SERVER] on some [PORT] via telnet and request it for HTTP header of some [WEB PAGE]: $ telnet [SERVER] [PORT] Trying xxx.xxx.xxx.xxx...

What are the Telnet commands?

These Telnet commands will help you do that. Telnet is used from the command line, for example, the command prompt on Windows. Only one command is needed to start the client: You can also connect directly without starting the client first. To do this, either specify the hostname or establish the connection via IP address:

How do I enable the Telnet client in Windows?

If the command prompt does not recognize the command, there are two possible ways to enable the Telnet client in Windows. To activate the Telnet command using the GUI: 1. Open the Programs and Features options in Control Panel: 2. Click the Turn Windows features on or off setting: 3.

How do I find the Telnet IP address of a printer?

Type the following command into the command window, substituting the IP address of the printer for <ipaddress>: telnet <ipaddress> 9100 (EG. TELNET 10.26.76.249 9100). Note: Please ensure that you leave a space between TELNET and the IP Address and again between the IP Address and 9100 (9100 is the Printer Port).


2 Answers

You can use tn.read_until("\n") in a loop in order to read one line durint execution of your telnet command

while True:
    line = tn.read_until(b"\n")  # Read one line
    print(line)
    if b'abcd' in line:  # last line, no more read
        break
like image 82
Mariusz Jamro Avatar answered Oct 15 '22 10:10

Mariusz Jamro


You can use the ready_very_eager, read_eager, read_lazy, and ready_very_lazy functions specified in the documentation to read your stream byte-by-byte. You can then handle the "until" logic on your own code and at the same time write the read lines to the console.

like image 33
dionyziz Avatar answered Oct 15 '22 09:10

dionyziz