Short script for translating IPs into ASNs. Add a list of IPs to the ip.txt
file, then run node index.js
.
Last active
August 9, 2023 15:27
-
-
Save kyletaylored/85170559392fcfe78d81afa2dce914e8 to your computer and use it in GitHub Desktop.
IP analysis script
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
node_modules | |
*.csv | |
package-lock.json |
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
const fs = require("fs"); | |
const axios = require("axios"); | |
async function main() { | |
const outputPath = "output.csv"; | |
const ips = fs.readFileSync("ip.txt").toString().split("\n"); | |
const concurrency = 100; // Set the concurrency level here | |
const chunks = chunkArray(ips, concurrency); | |
let count = 1; | |
for (const chunk of chunks) { | |
console.log(`Batch ${count} of ${chunks.length}`); | |
await Promise.all( | |
chunk.map(async (ip) => { | |
let line = ""; | |
const asn = await getAsn(ip); | |
if (Object.keys(asn).length === 0) { | |
line = `${ip}, '', '', ''`; | |
} else { | |
line = `${ip},${asn.autonomous_system_number},${asn.autonomous_system_organization},${asn.country}`; | |
} | |
await appendLineToFile(outputPath, line); | |
}) | |
); | |
count++; | |
} | |
} | |
function chunkArray(array, chunkSize) { | |
const chunks = []; | |
for (let i = 0; i < array.length; i += chunkSize) { | |
chunks.push(array.slice(i, i + chunkSize)); | |
} | |
return chunks; | |
} | |
async function appendLineToFile(filePath, line) { | |
try { | |
await fs.promises.appendFile(filePath, line + "\n"); | |
} catch (error) { | |
console.error("Error appending to file:", error); | |
} | |
} | |
async function getAsn(ip) { | |
try { | |
const res = await axios.get( | |
`https://maxmind.pantheon.support/datastudio?ip=${ip}` | |
); | |
return res.data; | |
} catch (error) { | |
return {}; | |
} | |
} | |
main(); |
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
24.96.13.43 | |
3.9.181.110 | |
3.97.52.164 | |
3.98.129.15 | |
39.62.8.168 | |
43.251.92.9 | |
69.70.50.58 | |
2a09:bac1:3480:18::1f1:1e7 | |
2a09:bac2:3778:ebe::178:4e | |
2001:1670:18:c15f:21ca:5fb:4346:43fd | |
2601:58b:c01:2600:4d49:374e:3db6:70e5 | |
2607:fb91:1205:865:ab4a:1a9:3548:7826 |
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
{ | |
"name": "ip-analysis", | |
"version": "1.0.0", | |
"description": "", | |
"main": "index.js", | |
"scripts": { | |
"test": "echo \"Error: no test specified\" && exit 1" | |
}, | |
"keywords": [], | |
"author": "", | |
"license": "ISC", | |
"dependencies": { | |
"axios": "^1.4.0" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment