Interacting with localhost over Telnet and sending output to file

160 Views Asked by At

New to Python, the version being used is 2.7.5.

I have a CentOS running a service on localhost, and am trying to interact (for automation) with it to get logs out of it.

The scripts does not seem to show the required output.

SCRIPT

#!/usr/bin/python

import telnetlib
import sys
import getpass

host ="127.0.0.1"
user = "root"
password = "mavenir"

tn = telnetlib.Telnet(host,5555)

tn.read_until(b"login: ", 5)
tn.write(user + b"\n")
tn.read_until(b"password: ", 5)
tn.write(password.encode('ascii') + b"\n")
tn.read_until(b"mnxt[/]#", 5)
tn.write("l2" + b"\n")
tn.read_until(b"mnxt[/l2]#", 5)
tn.write("getcellstat  --cellid 1" + b'\n')
res=tn.read_until(b"****** RNTI List end******", 5).decode('ascii')
print(red)

CURRENT OUTPUT

[root@centos7 ~]# ./test.py

mnxt[/l2]#
[root@centos7 ~]#

EXPECTED OUTPUT

[root@centos7 ~]# telnet localhost 5555
Trying ::1...
telnet: connect to address ::1: Connection refused
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
login: root
password: *******
MNXT Command line interface [ Use Tab for command and argument completions ]
mnxt[/]# l2
mnxt[/]#
mnxt[/l2]# getcellstat  --cellid 1
 
mnxt[/l2]#
No.Of RACH attemps         : 33519
No.Of RACH Preambles       : 32826
No.Of Dedicated Preambles  : 693
No.Of MSG3 success         : 1839
No.Of MSG3 failure         : 30998
No.Of Msg3CrcFail          : 971
No.Of Msg3DtxRcvd          : 31959
No.Of MSG4 sent            : 1767
No.Of MSG4 with CCCH Sdu   : 1664
ttiCount                   : 95629876
No.Of Connected UEs        : 1
Total Allocated DL Prbs    : 1120249742
Total Allocated UL Prbs    : 246439716
Total Dual sim detections  : 0
****** RNTI List start******
 
#         UE 0x6e0            #
No.Of Reestablishments    : 0
No.Of CRNTI CE Received   : 0
****** RNTI List end******
mnxt[/l2]#

EDITED (added sleep with read_very_eager())

tn.read_until(b"login: ", 5)
tn.write(user + b"\n")
tn.read_until(b"password: ", 5)
tn.write(password.encode('ascii') + b"\n")
tn.read_until(b"mnxt[/]#", 5)
tn.write("l2" + b"\n")
tn.read_until(b"mnxt[/l2]#", 5)
tn.write("getcellstat  --cellid 1")
time.sleep(10)
res = tn.read_very_eager()
print(res)

CURRENT OUTPUT

[root@centos7 ~]# ./test.py
 g
[root@centos7 ~]#

Unsure why the result is not being displayed.

Anything I can do ?

0

There are 0 best solutions below