Python Forum

Full Version: help required to parsing telnet output
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
telnet output is as below, want to print vlan (vid) and mac, please suggest changes


Port: 1
index vid mac
----- ---- -----------------
1 3738 e4:6f:13:80:dd:52
Port: 3
index vid mac
----- ---- -----------------
2 3738 c4:6e:1f:2f:ac:a6
Port: 5
index vid mac
----- ---- -----------------
3 3738 e4:6f:13:81:fc:a2
Port: 13
index vid mac
----- ---- -----------------
4 3738 48:ee:0c:d1:ba:95
Port: 14
index vid mac
----- ---- -----------------
5 3738 e8:37:7a:9d:0e:66
Port: 15
index vid mac
----- ---- -----------------
6 3738 00:17:7c:6d:64:b4
Port: 20
index vid mac
----- ---- -----------------
7 3638 6c:72:20:54:69:74
Port: 21
index vid mac
----- ---- -----------------
8 3638 10:7b:ef:a6:4b:dc
Port: 25
index vid mac
----- ---- -----------------
9 3638 00:17:7c:80:7a:43






#!/usr/bin/python
import paramiko
import sys
import os
import xlrd
import unidecode
import telnetlib
import time
import re
from time import sleep
mac = re.compile(r'[a-fA-F0-9]{2}[:][a-fA-AF0-9]{2}[:][a-fA-F0-9]{2}[:][a-fA-F0-9]{2}[:][a-fA-F0-9]{2}[:][a-fA-F0-9]{2}')
vlan = re.compile(r'\d{4}')
user = 'admin'
password = '1234'
with open('zyxel.txt','r') as ipfile:
    for sr_no, line in enumerate(ipfile, start=1):
        host = line.strip()
        try:
            print 'Connecting .....'+str(host)
            telnet = telnetlib.Telnet(host, 23, 2)
            telnet.read_until('User name:', 3)
            telnet.write(user.encode('ascii') + '\r')
            telnet.read_until('Password:', 3)
            telnet.write(password.encode('ascii') + '\r')
            #telnet.read_until('>')
            telnet.write('statistics mac 1~48' + '\r')
            telnet.read_until('>')
            telnet.write(' exit''\r')
            output = telnet.read_all()
            print 'printing output.....'
            for line in output:
                if vlan in line:
                    vlan = DslamVlan
                if mac in line:
                    mac = DslamPortMac
                    print DslamVlan+' '+DslamPortMac
        except Exception as excp:
            print(excp)
Just check if the first character of the line is a digit then split it and get the second and the third elements.
Thanks,

but how can i capture Port: 1, Port:2, Port:3 etc. i mean port numbers

changes done.. but not printing output

#!/usr/bin/python
import paramiko
import sys
import os
import xlrd
import unidecode
import telnetlib
import time
import re
from time import sleep
mac = re.compile(r'[a-fA-F0-9]{2}[:][a-fA-AF0-9]{2}[:][a-fA-F0-9]{2}[:][a-fA-F0-9]{2}[:][a-fA-F0-9]{2}[:][a-fA-F0-9]{2}')
vlan = re.compile(r'\d{4}')
user = 'admin'
password = '1234'
with open('zyxel.txt','r') as ipfile:
    for sr_no, line in enumerate(ipfile, start=1):
        host = line.strip()
        try:
            print 'Connecting .....'+str(host)
            telnet = telnetlib.Telnet(host, 23, 2)
            telnet.read_until('User name:', 3)
            telnet.write(user.encode('ascii') + '\r')
            telnet.read_until('Password:', 3)
            telnet.write(password.encode('ascii') + '\r')
            #telnet.read_until('>')
            telnet.write('statistics mac 1~48' + '\r')
            telnet.read_until('>')
            telnet.write(' exit''\r')
            output = telnet.read_all()
            print 'printing output.....'
            for line in output:
                #Capture first character of line
                first_character = line[:1]
                #check fist character of line is inter/digit
                if isinstance(first_character,int):
                    data= line.split(' ')
                # split the line
                    for outdata in data:
                        print outdata

        except Exception as excp:
            print(excp)
if you look at the line with the data you want you will see that the first digit corresponds to the port number. Just get it.

Alright, this is not the case.

Try this:

data = iter(output)

for line in data:
    if "Port:" in line:
        # get this line
        next(data) # scip the headers
        next(data) # scipt the --------
        # get the port number and the mac address. Prev. post
still stuck,

checking 4 character if line, if digit, spliting line and printing, however its not printing putout, what is going wrong.

changed code as below

#!/usr/bin/python
import paramiko
import sys
import os
import xlrd
import unidecode
import telnetlib
import time
import re
from time import sleep
mac = re.compile(r'[a-fA-F0-9]{2}[:][a-fA-AF0-9]{2}[:][a-fA-F0-9]{2}[:][a-fA-F0-9]{2}[:][a-fA-F0-9]{2}[:][a-fA-F0-9]{2}')
vlan = re.compile(r'\d{4}')
user = 'admin'
password = '1234'
with open('zyxel.txt','r') as ipfile:
    for sr_no, line in enumerate(ipfile, start=1):
        host = line.strip()
        try:
            print 'Connecting .....'+str(host)
            telnet = telnetlib.Telnet(host, 23, 2)
            telnet.read_until('User name:', 3)
            telnet.write(user.encode('ascii') + '\r')
            telnet.read_until('Password:', 3)
            telnet.write(password.encode('ascii') + '\r')
            #telnet.read_until('>')
            telnet.write('statistics mac 1~48' + '\r')
            telnet.read_until('>')
            telnet.write(' exit''\r')
            output = (telnet.read_all().decode('ascii'))
            file = open(host,'w')
            file.write(output.rstrip())
            print 'printing output.....'
            file.close()
            with open (host,'r') as outfile:
                 for lines in outfile:
                     forth_character = lines[:4]
                     #print forth_character, if digit, spit the line
                     if isinstance(forth_character,int):
                        data= lines.split(' ')
                        index = data[0]
                        vlan  = data[1]
                        mac   = data[2]
                        print(host,index,vlan,mac)
        except Exception as excp:
            print(excp)
data = iter(output.split('\n'))

try:
    for line in data:
        print(line)
        next(data)
        next(data)
        port_mac = next(data)[1:]
        print(port_mac)
except StopIteration:
    pass
Output:
Port: 1 3738 e4:6f:13:80:dd:52 Port: 3 3738 c4:6e:1f:2f:ac:a6 Port: 5 3738 e4:6f:13:81:fc:a2 Port: 13 3738 48:ee:0c:d1:ba:95 Port: 14 3738 e8:37:7a:9d:0e:66 Port: 15 3738 00:17:7c:6d:64:b4 Port: 20 3638 6c:72:20:54:69:74 Port: 21 3638 10:7b:ef:a6:4b:dc Port: 25 3638 00:17:7c:80:7a:43
You can do the formatting.
thanks, Wavic

import paramiko
import sys
import os
import xlrd
import unidecode
import telnetlib
import time
import re
from time import sleep
user = 'admin'
password = '1234'
with open('zyxel.txt','r') as ipfile:
    for sr_no, line in enumerate(ipfile, start=1):
        host = line.strip()
        try:
            print 'Connecting .....'+str(host)
            telnet = telnetlib.Telnet(host, 23, 2)
            telnet.read_until('User name:', 3)
            telnet.write(user.encode('ascii') + '\r')
            telnet.read_until('Password:', 3)
            telnet.write(password.encode('ascii') + '\r')
            #telnet.read_until('>')
            telnet.write('statistics mac 1~48' + '\r')
            telnet.read_until('>')
            telnet.write(' exit''\r')
            output = telnet.read_all()
            print 'printing output.....'
            sleep(1)
            data = iter(output.split('\n'))
            try:
                for line in data:
                    if "Port:" in line:
                        port1 = line.split(':')
                        port = str(port1[:5])
                        #port = port1[1]
                        next(data)
                        next(data)
                        port_mac = next(data)[1:]
                        print (host+' '+port+' '+port_mac)
            except StopIteration:
                   pass
        except Exception as excp:
            print(excp)
output

printing output.....
10.217.128.24 ['Port', ' 1\r'] 1 3738 e4:6f:13:80:dd:52
10.217.128.24 ['Port', ' 2\r'] 2 3738 c0:a0:bb:89:78:bc
10.217.128.24 ['Port', ' 3\r'] 3 3738 c4:6e:1f:2f:ac:a6
10.217.128.24 ['Port', ' 5\r'] 4 3738 e4:6f:13:81:fc:a2
10.217.128.24 ['Port', ' 14\r'] 5 3738 e8:37:7a:9d:0e:66
10.217.128.24 ['Port', ' 15\r'] 6 3738 00:17:7c:6d:64:b4
10.217.128.24 ['Port', ' 21\r'] 7 3638 10:7b:ef:a6:4b:dc
10.217.128.24 ['Port', ' 25\r'] 8 3638 00:17:7c:80:7a:43
10.217.128.24 ['Port', ' 26\r'] 9 3638 10:7b:ef:a8:94:88
10.217.128.24 ['Port', ' 27\r'] 10 3738 e4:6f:13:c0:42:43
10.217.128.24 ['Port', ' 31\r'] 11 3638 90:8d:78:84:73:81
10.217.128.24 ['Port', ' 36\r'] 12 3638 64:70:02:aa:7e:e2
10.217.128.24 ['Port', ' 37\r'] 13 3638 e4:6f:13:80:e8:e2
10.217.128.24 ['Port', ' 43\r'] 14 3638 80:26:89:cb:d4:6b
10.217.128.24 ['Port', ' 46\r'] 15 3638 e4:6f:13:b1:1c:c3
10.217.128.24 ['Port', ' 48\r'] 16 3638 90:8d:78:83:a2:71
Connecting .....10.217.128.40
printing output.....
10.217.128.40 ['Port', ' 5\r'] 1 3739 48:ee:0c:bc:53:1d
10.217.128.40 ['Port', ' 8\r'] 2 3739 e4:6f:13:c1:a7:42
10.217.128.40 ['Port', ' 16\r'] 3 3739 54:b8:0a:99:3d:97
10.217.128.40 ['Port', ' 23\r'] 4 3739 1c:5f:2b:3c:5d:c1
10.217.128.40 ['Port', ' 25\r'] 5 3739 0c:d2:b5:5e:be:a1
10.217.128.40 ['Port', ' 26\r'] 6 3739 00:17:7c:7d:44:3a
10.217.128.40 ['Port', ' 27\r'] 7 3639 54:b8:0a:98:fd:1f
10.217.128.40 ['Port', ' 33\r'] 8 3739 48:ee:0c:a8:44:03
10.217.128.40 ['Port', ' 34\r'] 9 3739 0c:d2:b5:5e:be:45
10.217.128.40 ['Port', ' 35\r'] 10 3739 e4:6f:13:c1:73:c2
10.217.128.40 ['Port', ' 42\r'] 11 3639 e8:37:7a:9d:0b:f5
10.217.128.40 ['Port', ' 48\r'] 12 3739 54:b8:0a:98:73:bf

but facing difficulty to print as below

HostIP Port vlan Mac
10.217.128.40 48 3739 54:b8:0a:98:73:bf
If the file is strictly formatted as in the example you provided then there is no need to for "Port" in line. The first line contains "Port" then you skip the next two lines, the next contains the port_address pair then the for loop starts again and you repeat the same cycle.

In "Port: 1" you can get the number just by splitting the line - line.split(). The result will be the list ["Port:", "1"] whatever the number is. Then grab the second element.

Use string formatting.

print("{} {} {}".format(host, port, port_mac))
thanks, its working... thanks once again