Skip to content

Instantly share code, notes, and snippets.

@pgagnidze
Created January 29, 2018 16:02
Show Gist options
  • Save pgagnidze/ffe7b2f89f00fbb6be6ad2e50f7f5412 to your computer and use it in GitHub Desktop.
Save pgagnidze/ffe7b2f89f00fbb6be6ad2e50f7f5412 to your computer and use it in GitHub Desktop.
import os
import zipfile
import fnmatch
import re
import errno
import shutil
# extract files from zip archive
user_input = raw_input("Enter full valid path of your directory (ex: /home/user/download/): ")
if not os.path.isdir(os.path.join(user_input, "unzipped")):
os.mkdir(user_input + "unzipped")
for dirpath, dirnames, filenames in os.walk(user_input):
for filename in fnmatch.filter(filenames, '*.zip'):
name = os.path.join(dirpath, filename)
print "unzipped files from " + name
if not os.path.isdir(name):
try:
zip = zipfile.ZipFile(name)
zip.extractall(path=user_input + "unzipped")
except zipfile.BadZipfile, error:
print "Bad Zip: "+filename
try:
os.remove(name)
except OSError as error: # this would be "except OSError, e:" before Python 2.6
if error.errno != errno.ENOENT: # errno.ENOENT = no such file or directory
raise # re-raise exception if a different error occured
# find ip addresses
def is_ipv4(ip):
match = re.match("^(\d{0,3})\.(\d{0,3})\.(\d{0,3})\.(\d{0,3})$", ip)
if not match:
return False
quad = []
for number in match.groups():
quad.append(int(number))
if quad[0] < 1:
return False
for number in quad:
if number > 255 or number < 0:
return False
return True
logname = 'ipaddress.log'
results = str()
for root, dirs, files in os.walk(user_input + "unzipped"):
for file in files:
try:
filename = os.path.join(root, file)
with open(filename) as f:
for line in f:
if is_ipv4(line):
results += line
except IOError as exc:
if exc.errno != errno.EISDIR:
raise
with open(logname, 'w') as logfile:
logfile.write(results)
print "extracted ip addresses to " + user_input + "ipaddress.log"
# cleanup
shutil.rmtree(user_input + "unzipped")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment