Last active
March 18, 2018 20:16
-
-
Save Townsheriff/7d9b3c4d35fa2765d7369aa78a469d37 to your computer and use it in GitHub Desktop.
Parsing BGP file and finding longest AS path
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 'use strict'; | |
| const fs = require('fs'); | |
| const split = require('split'); | |
| const through = require('through'); | |
| const regex = /\*\>?\s+[\d+|\.]+\/\d+\s+[\d+|\.]+\s+([\d+\s]+)/; | |
| const filter = through(function(buffer) { | |
| const line = buffer.toString(); | |
| if (line.match(regex)) { | |
| this.queue(line); | |
| } | |
| }); | |
| let current = ['', '']; | |
| const longest = through(function(buffer) { | |
| const line = buffer.toString(); | |
| const results = line.match(regex); | |
| const uniqueCurrent = new Set(results[1].split(' ')); | |
| const uniqueMax = new Set(current[1].split(' ')); | |
| if (uniqueMax.size <= uniqueCurrent.size) { | |
| current = results; | |
| console.log(`${current}\nSize: ${uniqueCurrent.size}`); | |
| } | |
| }); | |
| const fileStream = fs.createReadStream('ipv4bgp2018apnic.txt'); | |
| fileStream | |
| .pipe(split()) | |
| .pipe(filter) | |
| .pipe(longest) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment