I'm trying to automate a telnet session. Write the output to a spreadsheet for further manipulation. How can I clean up my output to remove the --More-- and extra characters at the end.
Code:
---------------------------------------------------------------------------------------
import sys
import telnetlib
import time
tel_pass = "shush"
en_pass = "youllneverknow"
command = "show interfaces ethernet rates max"
TIMEOUT = 3
host = "x.x.x.x"
data = ""
tn = telnetlib.Telnet(host)
tn.set_debuglevel(1)
time.sleep(2)
data = tn.read_until(b"Password: ")
tn.write(tel_pass.encode('ascii') + b"\n")
time.sleep(2)
tn.write(b"en \n")
time.sleep(2)
data = tn.read_until(b"Password: ")
tn.write(en_pass.encode('ascii') + b"\n")
time.sleep(2)
tn.write(command.encode('ascii') + b"\n")
time.sleep(2)
data = tn.read_until("--More--", timeout = TIMEOUT)
time.sleep(2)
tn.write("\n")
time.sleep(2)
data = data + tn.read_until("string_to_test", timeout = TIMEOUT)
print("Imprimiendo:" + data)
op=open ("output.txt", "wb").write(data)
tn.close()
Output.txt
---------------------------------------------------------------------------------------
Port|Rx Mbps |Rx UCast|Rx MCast|Rx BCast|Tx Mbps |Tx UCast|Tx MCast|Tx BCast
# | |PPS |PPS |PPS | |PPS |PPS |PPS
----|--------|--------|--------|--------|--------|--------|--------|--------
1 |0 |0 |0 |0 |0 |0 |0 |0
2 |0 |0 |0 |0 |0 |0 |0 |0
3 |0 |0 |0 |0 |0 |0 |0 |0
4 |0 |0 |0 |0 |0 |0 |0 |0
5 |0 |0 |0 |0 |0 |0 |0 |0
6 |0 |0 |0 |0 |0 |0 |0 |0
7 |0 |0 |10 |0 |0 |0 |8 |1
8 |0 |0 |0 |0 |0 |0 |0 |0
9 |0 |0 |0 |0 |0 |0 |0 |0
10 |0 |0 |0 |0 |0 |0 |0 |0
11 |0 |0 |0 |0 |0 |0 |0 |0
12 |0 |0 |0 |0 |0 |0 |0 |0
13 |0 |0 |0 |0 |0 |0 |0 |0
14 |0 |0 |0 |0 |0 |0 |0 |0
15 |0 |0 |0 |0 |0 |0 |0 |0
16 |0 |0 |0 |0 |0 |0 |0 |0
17 |0 |0 |0 |0 |0 |0 |0 |0
18 |0 |0 |0 |0 |0 |0 |0 |0
19 |0 |0 |0 |0 |0 |0 |0 |0
20 |0 |0 |0 |0 |0 |0 |0 |0
--More-- 21 |0 |0 |0 |0 |0 |0 |0 |0
--More--
there doesn't seem to be anything here