I am writing a function that takes a file updates.txt as an input. The file looks like this :
---------------------------------------------------
MRT Header
Timestamp: 1453939200(2016-01-28 01:00:00)
Type: 16(BGP4MP)
Subtype: 4(BGP4MP_MESSAGE_AS4)
Length: 39
BGP4MP_MESSAGE_AS4
Peer AS Number: 37989
Local AS Number: 12654
Interface Index: 0
Address Family: 1(IPv4)
Peer IP Address: 203.123.48.6
Local IP Address: 193.0.4.28
BGP Message
Marker: -- ignored --
Length: 19
Type: 4(KEEPALIVE)
---------------------------------------------------
MRT Header
Timestamp: 1453939200(2016-01-28 01:00:00)
Type: 16(BGP4MP)
Subtype: 4(BGP4MP_MESSAGE_AS4)
Length: 118
BGP4MP_MESSAGE_AS4
Peer AS Number: 1836
Local AS Number: 12654
Interface Index: 0
Address Family: 1(IPv4)
Peer IP Address: 146.228.1.3
Local IP Address: 193.0.4.28
BGP Message
Marker: -- ignored --
Length: 98
Type: 2(UPDATE)
Withdrawn Routes Length: 0
Total Path Attribute Length: 71
Path Attribute Flags/Type/Length: 0x40/1/1
ORIGIN: 0(IGP)
Path Attribute Flags/Type/Length: 0x40/2/42
AS_PATH
Path Segment Type: 2(AS_SEQUENCE)
Path Segment Length: 10
Path Segment Value: 1836 174 6453 37282 37088 37629 37629 37629 37629 37629
Path Attribute Flags/Type/Length: 0x40/3/4
NEXT_HOP: 146.228.1.3
Path Attribute Flags/Type/Length: 0xc0/8/12
COMMUNITY: 1836:110 1836:6000 1836:6031
NLRI: 154.65.7.0/24
---------------------------------------------------
The file is a sequence of 'blocks'. Each block is enclosed between the line of dashes
---------------------------------------------------
# Block (n)
---------------------------------------------------
# Block (n+1)
---------------------------------------------------
# Block (n+2) , etc
I would like to read the whole file, block-by-block, and return a text file only containing the lines of the fields : Timestamp, Peer AS number, Local AS number, Peer IP Address, Local IP Address.
The resulting .txt file should look something like this :
---------------------------------------------------
MRT Header
Timestamp: 1453939200(2016-01-28 01:00:00)
BGP4MP_MESSAGE_AS4
Peer AS Number: 37989
Local AS Number: 12654
Peer IP Address: 203.123.48.6
Local IP Address: 193.0.4.28
---------------------------------------------------
MRT Header
Timestamp: 1453939200(2016-01-28 01:00:00)
BGP4MP_MESSAGE_AS4
Peer AS Number: 1836
Local AS Number: 12654
Peer IP Address: 203.123.48.6
Local IP Address: 193.0.4.28
---------------------------------------------------
Ideally, I want to overwrite the updates.txt with the new text file not to waste space, and save it in a new directory "Parsed Updates".
I know it is minimal as I am stuck with the line of dashes delimiter, but my code looks like this :
import sys
import os
def parser(filename):
info = open(filename, 'r+')
info.read()
#Here comes the string manipulation code
#info.split( '---------------------------------------------------')
info.close()
print 'The file has been parsed successfully !!'
def main():
parser('updates.txt')
if __name__=='__main__':
main()
>>> with open('results.txt', 'wb') as r:
... with open('updates.txt', 'rb') as u:
... for line in u.readlines():
... if '-'*51 in line:
... r.write(line)
... else:
... if any(field in line for field in ['Timestamp', 'Peer AS Number', 'Local AS Number', 'Peer IP Address', 'Local IP Address','MRTHeader']):
... r.write(line)
your results file will look this:
$ cat results.txt
---------------------------------------------------
MRT Header
Timestamp: 1453939200(2016-01-28 01:00:00)
Peer AS Number: 37989
Local AS Number: 12654
Peer IP Address: 203.123.48.6
Local IP Address: 193.0.4.28
---------------------------------------------------
MRT Header
Timestamp: 1453939200(2016-01-28 01:00:00)
Peer AS Number: 1836
Local AS Number: 12654
Peer IP Address: 146.228.1.3
Local IP Address: 193.0.4.28
---------------------------------------------------
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments