I’m running into an issue with my locate / match statement. I’m trying to match the column “IP” assign it as the index and iterate through a csv of a few thousand hosts.
Whenever the below script makes its way back up to process the second ‘IP’ I fail with a “single positional indexer is out-of-bounds”.
Thank you all.
JavaScript
x
14
14
1
def whoisyou(df):
2
s = socket.socket()
3
s.settimeout(10)
4
for index, row in df.iterrows():
5
DN = df.iloc[index]["ip"]
6
ipwhois = IPWhois(DN).lookup_rdap(asn_methods=["dns", "whois", "http"])
7
network = ipwhois["asn_cidr"]
8
cidr = ipwhois["asn_description"]
9
country = ipwhois["asn_country_code"]
10
date = ipwhois["asn_date"]
11
df = pd.DataFrame(columns=[DN, cidr, country, date, network])
12
whoareyou = df.to_csv("output.csv", index=False, mode="a")
13
time.sleep(5)
14
Here is the csv content:
Advertisement
Answer
It seems you are not using iterrows
properly and there are several inconsistencies in your code.
Try to define your whoisyou
function like this:
JavaScript
1
15
15
1
def whoisyou(df):
2
for _, row in df.iterrows():
3
DN = row["ip"]
4
try:
5
ipwhois = IPWhois(DN).lookup_rdap(asn_methods=["dns", "whois", "http"])
6
row["network"] = ipwhois["asn_cidr"]
7
row["cidr"] = ipwhois["asn_description"]
8
row["country"] = ipwhois["asn_country_code"]
9
row["date"] = ipwhois["asn_date"]
10
except KeyError:
11
continue
12
time.sleep(5)
13
14
df.to_csv("output.csv", index=False, mode="a")
15
And then you could call it like this:
JavaScript
1
18
18
1
import pandas as pd
2
3
df = pd.read_csv("your_hosts_file.csv")
4
5
# Check source data
6
print(df)
7
8
ip cidr country date network
9
0 146.112.255.155
10
1 168.149.132.2
11
2 168.149.132.176
12
3 104.40.69.64
13
4 152.195.12.135
14
5 34.120.111.39
15
16
# Get infos, fulfill the df and save it as a csv file
17
whoisyou(df)
18