-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathipscrapper.py
32 lines (32 loc) · 1.66 KB
/
ipscrapper.py
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
#This python program recursively search all the files inside the directory passed as command line argument and create a csv file with extracted IP's
import csv,re,socket,sys
from os import listdir
from os.path import isfile,join
with open("output.csv","w",newline='') as csvfile:
csvwrite=csv.writer(csvfile)
csvwrite.writerow(["IP Address", "host or network name", "Path", "DNS Name"])
def dirs(path):
dirs_list=listdir(path)
for i in dirs_list:
if isfile(join(path,i)):
try:
with open(join(path,i),"r") as f:
for line in f:
test=re.search(r'(?<!\d)(?!0|127)\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}(\/\d{2})?',line)
if test:
try:
host=socket.gethostbyaddr(test.group(0))[0]
except Exception:
host="Not found"
print(test.group(0), host, join(path,i))
csvwrite.writerow([test.group(0), host, join(path,i)[27:], "DNS Name"])
except:
with open(join(path,i),"rb") as f:
for line in f:
test = re.search(b'(?<!\d)(?!0|127)\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}(\/\d{2})?', line)
if test:
print(test.group(0), "Not found", join(path,i)[27:])
csvwrite.writerow([test.group(0), "Not found", join(path,i)])
else:
dirs(join(path,i))
dirs(sys.argv[1])