-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate-ips.ysh
executable file
·65 lines (55 loc) · 1.31 KB
/
generate-ips.ysh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#!/usr/bin/env ysh
if (len(ARGV) < 1) {
echo "Need a country country_code"
exit 1
}
var country_code = ARGV[0]
var rirs = {
afrinic: "https://ftp.afrinic.net/pub/stats/afrinic/delegated-afrinic-extended-latest",
apnic: "http://ftp.apnic.net/apnic/stats/apnic/delegated-apnic-extended-latest",
arin: "https://ftp.arin.net/pub/stats/arin/delegated-arin-extended-latest",
lacnic: "https://ftp.lacnic.net/pub/stats/lacnic/delegated-lacnic-extended-latest",
ripe: "https://ftp.lacnic.net/pub/stats/ripencc/delegated-ripencc-latest",
}
func rangeToSubnet(range) {
var i = range
var c = 32
while (i > 1) {
setvar i = i >> 1
setvar c -= 1
}
return (c)
}
proc filter-output (country_code) {
while read -r line {
var l = line => split("|")
if (len(l) < 4) { continue }
if (l[1] === country_code and l[2] === "ipv4") {
echo "$[l[3]]/$[rangeToSubnet(l[4])]"
}
}
}
# Aggregate
mkfifo mypipe
fork { ./aggregate-subnets.py < mypipe }
cleanup() {
rm mypipe
var j = $(jobs -p)
if test -n $j {
kill $(jobs -p)
}
}
trap cleanup EXIT INT TERM
source --builtin draft-synch.ysh
sema-new (0, &s)
for key, val in (rirs) {
fork {
try { curl -s $val | filter-output $country_code >> mypipe }
sema-up (s)
}
}
var c = 0
while (c < len(rirs)) {
sema-down (s)
setvar c += 1
}