Page Menu
Home
Phabricator
Search
Configure Global Search
Log In
Paste
P10416
email-rpki-unreach.py
Active
Public
Actions
Authored by
ayounsi
on Feb 14 2020, 6:18 PM.
Edit Paste
Archive Paste
View Raw File
Subscribe
Mute Notifications
Award Token
Flag For Later
Tags
netops
Referenced Files
F31616543: raw.txt
Feb 14 2020, 6:18 PM
2020-02-14 18:18:42 (UTC+0)
Subscribers
None
import
json
import
time
import
urllib.request
response
=
urllib
.
request
.
urlopen
(
'https://as286.net/data/ana-invalids.txt'
)
raw_invalids_list
=
response
.
read
()
.
decode
(
"utf-8"
)
problematic_asns
=
{}
for
line
in
raw_invalids_list
.
splitlines
():
if
'altpfx=NONE'
in
line
or
'altpfx=PARTIAL'
in
line
:
prefix
,
asn_raw
,
everything_else
=
line
.
split
(
';'
,
2
)
# TODO tackle case where a prefix is coming from several AS#
if
'srcASes'
in
asn_raw
:
continue
asn
=
int
(
asn_raw
.
replace
(
'srcAS='
,
''
))
if
asn
in
problematic_asns
:
if
prefix
not
in
problematic_asns
[
asn
][
'prefixes'
]:
problematic_asns
[
asn
][
'prefixes'
]
.
append
(
prefix
)
else
:
problematic_asns
[
asn
]
=
{}
problematic_asns
[
asn
][
'prefixes'
]
=
[
prefix
]
print
(
len
(
problematic_asns
))
no_noc_or_abuse
=
0
no_noc
=
0
http_error
=
0
for
asn
,
data
in
problematic_asns
.
items
():
# To not hammer the API
time
.
sleep
(
1
)
email
=
''
try
:
with
urllib
.
request
.
urlopen
(
"https://stat.ripe.net/data/whois/data.json?resource="
+
str
(
asn
))
as
url
:
raw_whois
=
json
.
loads
(
url
.
read
()
.
decode
())
for
records
in
raw_whois
[
'data'
][
'records'
]:
for
contacts
in
records
:
if
contacts
[
'key'
]
==
'OrgNOCEmail'
:
email
=
contacts
[
'value'
]
break
if
email
:
break
if
not
email
:
no_noc
+=
1
with
urllib
.
request
.
urlopen
(
"https://stat.ripe.net/data/abuse-contact-finder/data.json?resource="
+
str
(
asn
))
as
url
:
raw_abuse
=
json
.
loads
(
url
.
read
()
.
decode
())
try
:
email
=
raw_abuse
[
'data'
][
'anti_abuse_contacts'
][
'abuse_c'
][
0
][
'email'
]
except
(
IndexError
,
KeyError
):
print
(
"Can't find NOC or Abuse contact for AS"
+
str
(
asn
))
no_noc_or_abuse
+=
1
problematic_asns
[
asn
][
'email'
]
=
email
except
urllib
.
error
.
HTTPError
as
e
:
print
(
'Error while fetching AS'
+
str
(
asn
)
+
' '
+
str
(
e
))
http_error
+=
1
print
(
no_noc_or_abuse
)
print
(
no_noc
)
print
(
http_error
)
Event Timeline
ayounsi
created this paste.
Feb 14 2020, 6:18 PM
2020-02-14 18:18:42 (UTC+0)
Log In to Comment