Last active
March 6, 2022 21:12
-
-
Save jjsantanna/dd350c5b80db49b8842aa16322181a03 to your computer and use it in GitHub Desktop.
IP2ASN IP2host
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
df['ip']=df['column'].str.extract('([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)') | |
########################## | |
########### IP to hostname | |
import socket | |
import pandas as pd | |
def ip2hostname (df_series_ip): | |
df_output = pd.DataFrame() | |
for ip in df_series_ip: | |
try: | |
df_output = df_output.append({'ip':ip, | |
'host':socket.gethostbyaddr(ip)[0]},ignore_index=True) | |
except: | |
df_output = df_output.append({'ip':ip, | |
'host':''},ignore_index=True) | |
return df_output.reset_index(drop=True) | |
########################## | |
########### IP to ASN country | |
import subprocess | |
import os.path | |
import random | |
import time | |
import pandas as pd | |
def ip2asn(pandaseries_ips): | |
iptoasn_response = open('output_ip2asn_teamcymru.txt', 'w') | |
#Creating the request file containing a list of IPs | |
#For more information, please access: http://www.team-cymru.com/IP-ASN-mapping.html | |
iptoasn_request = open('input_ip2asn_teamcymru.txt', 'w') | |
iptoasn_request.write('begin\nverbose\n') | |
pd.Series(pandaseries_ips.dropna().unique()).to_csv(iptoasn_request,header=False,index=False,sep="\t") | |
iptoasn_request.write('end') | |
iptoasn_request.close() | |
#Performing the bulk request | |
cat = subprocess.Popen(['cat', 'input_ip2asn_teamcymru.txt'], stdout=subprocess.PIPE) | |
print(cat) | |
netcat = subprocess.Popen(['netcat', 'whois.cymru.com', '43'], stdin=cat.stdout, stdout=iptoasn_response) | |
print (netcat) | |
time.sleep(3) #for some reason the poll does not work! This was the way to overcome the waiting time. | |
#Closing the output file | |
iptoasn_response.close() | |
df = pd.read_csv('output_ip2asn_teamcymru.txt',\ | |
skiprows=1,\ | |
delimiter="\s+\|\s",\ | |
names = ['asn', 'ip', 'bgp_prefix', 'country','registry','info_date','as_name']) | |
### CAN BE USEFUL: | |
###df['asn'] = df['asn'].astype(str).apply(lambda x: 'AS'+x.split('.')[0]) | |
!rm input_ip2asn_teamcymru.txt | |
!rm output_ip2asn_teamcymru.txt | |
return df |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment