Friday, July 27, 2007

vbscript to convert as-ms-prefixes.txt to CSV/TSV files (for importing into database)

as-ms-prefixes.txt can be obtain from
http://www.cidr-report.org/as2.0/as-ms-prefixes.txt

Option Explicit

Sub as_ms_prefixes()
Dim fn, tx As TextStream, tmp, i1, i2, net, rir, asn, n, r, asname
Dim tx1 As TextStream, tx2 As TextStream
fn = "S:\JUNK\New Folder (2)\as-ms-prefixes.txt"
If Not fso.FileExists(fn) Then Exit Sub
Set tx = fso.OpenTextFile(fn, ForReading, False)
Set tx1 = fso.CreateTextFile(fso.BuildPath(fso.GetParentFolderName(fn), fso.GetBaseName(fn) & "_1.out"), True)
Set tx2 = fso.CreateTextFile(fso.BuildPath(fso.GetParentFolderName(fn), fso.GetBaseName(fn) & "_2.out"), True)
Do While Not tx.AtEndOfStream
tmp = tx.ReadLine
If Left(tmp, 4) = " " Then
i1 = InStr(1, tmp, vbTab & vbTab)
i2 = InStr(i1 + 1, tmp, ")")
net = Mid(tmp, 5, i1 - 5)
rir = Mid(tmp, i1 + 19, i2 - i1 - 19)
'Debug.Print net, rir
tx2.WriteLine Join(Array(Mid(asn, 3), net, rir), "")
ElseIf Left(tmp, 2) = "AS" Then
asn = Mid(tmp, 1, InStr(1, tmp, " ") - 1)
n = LTrim(Mid(tmp, 8, 9))
r = LTrim(Mid(tmp, 18, 26 - 18))
asname = Mid(tmp, 27)
tx1.WriteLine Join(Array(asn, n, r, asname), "")
Else
'invalid
End If

Loop
Out tx.Line
tx.Close
tx1.Close
tx2.Close
'assigned
'allocated
End Sub


results:

as-ms-prefixes_1.out
AS475512171234VSNL-AS Videsh Sanchar Nigam Ltd. Autonomous System
AS701811961460ATT-INTERNET4 - AT&T WorldNet Services
AS647811181118ATT-INTERNET3 - AT&T WorldNet Services
AS432311131303TWTC - Time Warner Telecom, Inc.
AS238611091203INS-AS - AT&T Data Communications Services
AS958311001109SIFY-AS-IN Sify Limited
AS1149210871091CABLEONE - CABLE ONE
AS619710031024BATI-ATL - BellSouth Network Solutions, Inc
AS1856610021011COVAD - Covad Communications Co.
AS41349911265CHINANET-BACKBONE No.31,Jin-rong Street


as-ms-prefixes_2.out
475559.151.144.0/2259.151.128.0/18
475559.160.0.0/2259.160.0.0/14
475559.160.4.0/2259.160.0.0/14
475559.160.5.0/2459.160.0.0/14
475559.160.8.0/2259.160.0.0/14
475559.160.12.0/2259.160.0.0/14
475559.160.16.0/2159.160.0.0/14
475559.160.24.0/2159.160.0.0/14
475559.160.24.0/2459.160.0.0/14
475559.160.28.0/2459.160.0.0/14