Skip to content

Instantly share code, notes, and snippets.

@Townsheriff
Last active March 18, 2018 20:16
Show Gist options
  • Select an option

  • Save Townsheriff/7d9b3c4d35fa2765d7369aa78a469d37 to your computer and use it in GitHub Desktop.

Select an option

Save Townsheriff/7d9b3c4d35fa2765d7369aa78a469d37 to your computer and use it in GitHub Desktop.
Parsing BGP file and finding longest AS path
'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