-
Notifications
You must be signed in to change notification settings - Fork 1
/
getUniqueIPfromDir.py
47 lines (41 loc) · 1.33 KB
/
getUniqueIPfromDir.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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import os
rootdir = os.getcwd()
thelist = []
linesPrint = 0
def returnIP(line):
import re
theIP = ''
searchObj = re.search( r'[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}', line, re.M|re.I)
if searchObj:
results = searchObj.group()
return str(results)
else:
return None
theList= []
for root, _, files in os.walk(rootdir):
for f in files: # Each file in the Directory
if '.py' in f:
print 'Skipping %s'% f
else:
fname = os.path.join(root, f) # Set the name and path of the file
print 'Opening', fname # Displays name of the file
theFile = open(fname,'r') # Opens the file
for line in theFile:
#### Do a check here
theResults = returnIP(line)
if theResults:
if theResults not in theList:
theList.append(theResults)
linesPrint = linesPrint + 1 # Increases the lines print by one
theFile.close()
if not theList: # After the all files have been read
print 'The search returned no results... Sorry - better luck next time'
else: #If the array isn't empty
print 'Writing log file to disk' # Display
theLog = rootdir + '\\'+ 'ExtractedIPs.txt'
writeLog = open(theLog, 'w')
for item in theList:
item = "%s\n" % item
writeLog.write(item)
writeLog.close()
thelist = []