Home > Mobile >  Parse text into columns
Parse text into columns

Time:11-17

I have the following output from a series of routers:

hostname CPE-ABCDEFGHI-55553-03
Interface                  IP-Address      OK? Method Status                Protocol
FastEthernet0              unassigned      YES unset  up                    up      
FastEthernet1              unassigned      YES unset  up                    down    
FastEthernet2              unassigned      YES unset  up                    down    
FastEthernet3              unassigned      YES unset  up                    up      
FastEthernet4              unassigned      YES NVRAM  up                    up      
FastEthernet4.100          93.62.88.112    YES DHCP   up                    up      
FastEthernet4.106          182.21.200.233  YES DHCP   up                    up      
Vlan1                      12.230.1.34     YES NVRAM  up                    up      
###############################################
hostname CPE-XXXXXXXXX-33333-01
Interface                  IP-Address      OK? Method Status                Protocol
Async1                     unassigned      YES unset  down                  down    
FastEthernet0              unassigned      YES unset  up                    up      
FastEthernet1              unassigned      YES unset  down                  down    
FastEthernet2              unassigned      YES unset  down                  down    
FastEthernet3              unassigned      YES unset  down                  down    
FastEthernet4              unassigned      YES unset  down                  down    
FastEthernet5              unassigned      YES unset  down                  down    
FastEthernet6              unassigned      YES unset  down                  down    
FastEthernet7              unassigned      YES unset  down                  down    
FastEthernet8              unassigned      YES NVRAM  administratively down down    
GigabitEthernet0           unassigned      YES DHCP   up                    up      
GigabitEthernet0.100       1.1.1.1    YES DHCP   up                    up      
GigabitEthernet0.106       152.21.133.171  YES DHCP   up                    up      
NVI0                       unassigned      YES unset  administratively down down    
Vlan1                      10.241.0.162    YES NVRAM  up                    up      
Vlan10                     10.240.0.194    YES NVRAM  up                    up      
###############################################
hostname CPE-YYYYYYYYY-28886-03
Interface                  IP-Address      OK? Method Status                Protocol
Cellular0                  unassigned      YES NVRAM  up                    up      
Dialer1                    192.168.255.3   YES IPCP   up                    up      
FastEthernet0              unassigned      YES unset  up                    up      
FastEthernet1              unassigned      YES unset  up                    down    
FastEthernet2              unassigned      YES unset  up                    down    
FastEthernet3              unassigned      YES unset  up                    down    
FastEthernet4              unassigned      YES NVRAM  administratively down down    
Loopback0                  173.17.18.193   YES NVRAM  up                    up      
NVI0                       18.10.10.1      YES unset  up                    up      
Vlan1                      10.241.0.163    YES NVRAM  up                    up      
Vlan10                     10.240.0.195    YES NVRAM  up                    up      
###############################################
hostname CPE-AAAAAAAAA-22222-03
Interface                  IP-Address      OK? Method Status                Protocol
FastEthernet0              unassigned      YES unset  up                    up      
FastEthernet1              unassigned      YES unset  up                    down    
FastEthernet2              unassigned      YES unset  down                  down    
FastEthernet3              unassigned      YES unset  up                    down    
FastEthernet4              unassigned      YES NVRAM  up                    up      
FastEthernet4.100          46.189.234.214  YES DHCP   up                    up      
FastEthernet4.106          172.21.128.240  YES DHCP   up                    up      
Vlan1                      10.241.7.253    YES NVRAM  up                    up      
Vlan10                     10.270.1.35     YES NVRAM  up                    up      
###############################################

I need to transform this to an Excel file with a column per interface. And for that i need to make a python 2.7.5 (restriction of ssh server) script to parse the text into Excel.

For example (imagine these are cells)

Hostname Interface IP Address OK? Method Status Protocol
CPE-ABCDEFGHI-55553-03 FastEthernet0 unassigned (...) ... ... ...
CPE-ABCDEFGHI-55553-03 FastEthernet1 unassigned (...) ... ... ...

Can you help me do this?

CodePudding user response:

Maybe something like that ?

output = []
with open('Output.txt','r') as fil:
    for line in fil:
        output.append(line.strip())

mydata = []
host_curr = ''

for element in output:
    if element.startswith('####') or element.startswith('Interface'):
        continue
    if element.startswith('hostname'):
        host_curr = element.split()[1]
        continue
    myline = host_curr   '\t'   element
    mydata.append(myline)


with open('Output.csv','w') as out:
    for elem in mydata:
        out.write('{}\n'.format(elem))

If output is your output (here it's a list of lines of text). The csv should look like that :

CPE-ABCDEFGHI-55553-03  FastEthernet0              unassigned      YES unset  up                    up
CPE-ABCDEFGHI-55553-03  FastEthernet1              unassigned      YES unset  up                    down
CPE-ABCDEFGHI-55553-03  FastEthernet2              unassigned      YES unset  up                    down
CPE-ABCDEFGHI-55553-03  FastEthernet3              unassigned      YES unset  up                    up
CPE-ABCDEFGHI-55553-03  FastEthernet4              unassigned      YES NVRAM  up                    up
CPE-ABCDEFGHI-55553-03  FastEthernet4.100          93.62.88.112    YES DHCP   up                    up
CPE-ABCDEFGHI-55553-03  FastEthernet4.106          182.21.200.233  YES DHCP   up                    up
CPE-ABCDEFGHI-55553-03  Vlan1                      12.230.1.34     YES NVRAM  up                    up
CPE-XXXXXXXXX-33333-01  Async1                     unassigned      YES unset  down                  down
CPE-XXXXXXXXX-33333-01  FastEthernet0              unassigned      YES unset  up                    up
CPE-XXXXXXXXX-33333-01  FastEthernet1              unassigned      YES unset  down                  down
CPE-XXXXXXXXX-33333-01  FastEthernet2              unassigned      YES unset  down                  down
CPE-XXXXXXXXX-33333-01  FastEthernet3              unassigned      YES unset  down                  down
CPE-XXXXXXXXX-33333-01  FastEthernet4              unassigned      YES unset  down                  down
CPE-XXXXXXXXX-33333-01  FastEthernet5              unassigned      YES unset  down                  down
CPE-XXXXXXXXX-33333-01  FastEthernet6              unassigned      YES unset  down                  down
CPE-XXXXXXXXX-33333-01  FastEthernet7              unassigned      YES unset  down                  down
CPE-XXXXXXXXX-33333-01  FastEthernet8              unassigned      YES NVRAM  administratively down down
CPE-XXXXXXXXX-33333-01  GigabitEthernet0           unassigned      YES DHCP   up                    up
CPE-XXXXXXXXX-33333-01  GigabitEthernet0.100       1.1.1.1    YES DHCP   up                    up
CPE-XXXXXXXXX-33333-01  GigabitEthernet0.106       152.21.133.171  YES DHCP   up                    up
CPE-XXXXXXXXX-33333-01  NVI0                       unassigned      YES unset  administratively down down
CPE-XXXXXXXXX-33333-01  Vlan1                      10.241.0.162    YES NVRAM  up                    up
CPE-XXXXXXXXX-33333-01  Vlan10                     10.240.0.194    YES NVRAM  up                    up
  • Related