I’m using Python 3. I’ve written two programs. One loops through a csv file to obtain IP addresses for Cisco switches, logs in, runs a command, and outputs to results for each to an individual text file. So I end up with a number of text files…one for each switch. The second program uses xlwt to write the information in each text file to its own sheet in Excel.
The main idea is that I need to develop a report showing ports in and out of service. Once I get these imported into Excel I can write some formulas to extract the data I need. But as it stands now when I import this into Excel I have to manually remove some of the cells because everything doesn’t line up and that’s because of the spaces in between some of the words in the name column (I’m importing into Excel as space delimited). I’ve attempted to do some things with string and list methods (split, join, slicing, etc) but I’m not able to get exactly what I want. And the Name column is not standardized in any kind of consistent convention. I do notice that although the name may actually be very long, it gets truncated to a certain number of characters.
Ideally, removing the first 4 lines (there is blank line at the very top) and the last line first, then for anythting between Port and Status, remove it all (remove the column altogether including the header).
This is how the files look after getting data from the switch.
sw1#term length 0 sw1#show interfaces status Port Name Status Vlan Duplex Speed Type Gi0/1 Trunk to switch (a connected 1 a-full a-100 10/100/1000BaseTX Gi0/2 Network augment pe connected 1 a-full a-1000 10/100/1000BaseTX Gi0/3 connected 1 a-full a-1000 10/100/1000BaseTX Gi0/4 connected 1 a-full a-100 10/100/1000BaseTX Gi0/5 notconnect 1 auto auto Not Present Gi0/6 notconnect 1 auto auto Not Present Gi0/7 notconnect 1 auto auto Not Present Gi0/8 notconnect 1 auto auto Not Present Gi0/9 notconnect 1 auto auto Not Present Gi0/10 connected 1 a-full a-100 10/100/1000BaseTX Gi0/11 notconnect 1 auto auto Not Present Gi0/12 connected 1 a-full a-100 10/100/1000BaseTX Gi0/13 disabled 1 auto auto Not Present Gi0/14 disabled 1 auto auto Not Present Gi0/15 disabled 1 auto auto Not Present Gi0/16 disabled 1 auto auto Not Present sw1#logout
End result I’d like below. This should allow the row/column structure to remain intact when importing into Excel. Note that all of the column info is separated by spaces. I’ve found that importing as fixed width or delimited by space with treat consecutive spaces as one checked seems to work pretty well.
Port Status Vlan Duplex Speed Type Gi0/1 connected 1 a-full a-100 10/100/1000BaseTX Gi0/2 connected 1 a-full a-1000 10/100/1000BaseTX Gi0/3 connected 1 a-full a-1000 10/100/1000BaseTX Gi0/4 connected 1 a-full a-100 10/100/1000BaseTX Gi0/5 notconnect 1 auto auto Not Present Gi0/6 notconnect 1 auto auto Not Present Gi0/7 notconnect 1 auto auto Not Present Gi0/8 notconnect 1 auto auto Not Present Gi0/9 notconnect 1 auto auto Not Present Gi0/10 connected 1 a-full a-100 10/100/1000BaseTX Gi0/11 notconnect 1 auto auto Not Present Gi0/12 connected 1 a-full a-100 10/100/1000BaseTX Gi0/13 disabled 1 auto auto Not Present Gi0/14 disabled 1 auto auto Not Present Gi0/15 disabled 1 auto auto Not Present Gi0/16 disabled 1 auto auto Not Present
Any pointers would be appreciated. I’m thinking regular expressions may be in order but I need a bit assistance with how to construct that. I hope this isn’t too ambiguous.
REMOVED A PREVIOUS UPDATE AND MOVED IT TO A NEW THREAD
i think that will do roughly what you want; you may need to play with the numbers a little as i haven’t run it myself. all it uses is list (or string) indexing:
the
lines[-1:] + lines[2:-1]puts the last line first, and throws away the first two;line[0:11] + line[35:-1]excludes the part you don’t want and the final newline.update if you want to write into a new file, instead of stdout:
in fact, since readlines reads everything at once, you could do:
as there’s no need for the input file to be open (it’s closed when the
withfinishes).