Skip to content

Instantly share code, notes, and snippets.

@kyletaylored
Last active August 9, 2023 15:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kyletaylored/85170559392fcfe78d81afa2dce914e8 to your computer and use it in GitHub Desktop.
Save kyletaylored/85170559392fcfe78d81afa2dce914e8 to your computer and use it in GitHub Desktop.
IP analysis script
node_modules
*.csv
package-lock.json

IP analysis

Short script for translating IPs into ASNs. Add a list of IPs to the ip.txt file, then run node index.js.

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();
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
{
"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