0

I have few list of commands (any number), which have to be executed over telnet on one particular IP/HOST. And the output to be stored in separate file. These commands are specific to log collection.

 I need them in such a way that, Execute all those required commands at once (Start/enabling for log collection) - Multiple telnet sessions, one session per command. After sometime (Not a timed activity), require another script to stop all of them & logs stored in separate file respectively (based on the list of commands executed).

 I could able to do it only for one particular command, that too only for short interval of time.

I hope I'm clear with the details. Please let me know if you are not clear with the concept. Please help me in this regard.

import sys
import telnetlib
import time

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

try:
    tn = telnetlib.Telnet(IP)
    tn.read_until(b"login: ")
    tn.write(username.encode('ascii') + b"\n")
    tn.read_until(b"# ")
    tn.write(command1.encode('ascii') + b"\n")
    #time.sleep(30)
    z = tn.read_until(b'abcd\b\n',4)    >> Just a random pattern, so that it reads for long duration.
    #z=tn.read_very_eager()
    output = z.splitlines( )
except:
    sys.exit("Telnet Failed to ",IP)

for i in output:
    i=i.strip().decode("utf-8")
    print(i)

sys.stdout = orig_stdout
f.close()
Jackie James
  • 785
  • 8
  • 15
  • 28
  • and what have you tried? where are you failing? – Doon Dec 30 '14 at 17:21
  • @ Doon : I have provided a snippet above. It is applicable only to single command and i could not collect the logs for long duration. This is to be applied for multiple commands & should be able to collect as long as i wish. In general, One script to start execute the command and another one to stop it. – Jackie James Dec 30 '14 at 17:27
  • so what fails exactly? What is the error? does the connection close? – Doon Dec 30 '14 at 17:28
  • It fails when there is no response from telnet. There are some instances, where it will be paused for a while and again starts displaying logs. – Jackie James Dec 30 '14 at 17:32
  • your current read_until says to read_until you see `abcd` or 4 seconds.(Timeout) is that what you want? – Doon Dec 30 '14 at 17:37
  • Yeah. Timeout may be for a small time where it's pausing in between. But how to stop/kill/halt this particular telnet session, when not aware of time how long it should be collecting logs. – Jackie James Dec 30 '14 at 17:43

0 Answers0