Created
October 12, 2017 01:12
-
-
Save oldkingcone/7574118f3c7fa1031d767886ef27b81c to your computer and use it in GitHub Desktop.
Updated Directory_traversal.py script, fixed get_tree_size function errors. Will print errors into log file
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
import os | |
import sys | |
import grp | |
import pwd | |
''' | |
There are 2 functions calling and doing the same thing. The accurate scan seems to preform much better with large file systems. | |
automation for ls /* in the event you want all output logged and already handled for you. | |
''' | |
os.system('clear') | |
os.chdir('/home') | |
os.system('pwd') | |
path2 = ['/', '/home/*', '/bin', '/usr', '/var', '/tmp', '/lib', '/lib64', '/etc', '/sys', '/dev', '/media', '/mnt', '/srv', '/sbin', '/etc'] | |
path1 = ['/', '/home/*', '/bin', '/usr', '/var', '/tmp', '/lib', '/lib64', '/etc', '/sys', '/dev', '/media', '/mnt', '/srv', '/sbin', '/etc'] | |
def get_tree_size(path): | |
total = 0 | |
outfile2=open('./tree_size.csv', 'at') # open csv file in append mode. | |
for entry in os.scandir(path): | |
try: | |
entry.is_dir(follow_symlinks=True) | |
total = get_tree_size(entry.path) | |
c = ',' | |
fina = ["[+]", total, '[+]', '\n'] # log any and all directories found to file | |
outfile2.write(str(total)) | |
# outfile2.close() | |
except OSError as error: | |
er = ['Error Calling is_dir():', str(error), str(sys.stderr),"\n"] # print errors to log file(csv file) | |
outfile2.write("".join(er)) # join all contents held within er | |
outfile2.close | |
continue | |
else: | |
try: | |
total2 = entry.stat(follow_symlinks=False).st_size | |
fin = "\n", total2 | |
outfile2.write(str(fin)) | |
outfile2.close() | |
except OSError as error: | |
er2 = ['Error calling stat():', str(error), str(sys.stderr),"\n"] # print errors to log file(csv file) | |
outfile2.write("".join(er2)) | |
outfile2.close() | |
return get_tree_size(path) | |
def accurate_scan(pathed): | |
for item in pathed: | |
for root, dirs, files in os.walk(item): | |
for fn in files: | |
path = os.path.join(root, fn) | |
try: | |
stats = os.lstat(path) | |
user = pwd.getpwuid(stats.st_uid)[0] | |
group = grp.getgrgid(stats.st_gid)[0] | |
laccess = dt.fromtimestamp(stats.st_atime).strftime("%Y-%m-%d,%H:%M:%S") | |
lmod = dt.fromtimestamp(stats.st_atime).strftime("%Y-%m-%d,%H:%M:%S") | |
c = ',' | |
t1 = ["[+]", path, c, user, c, group, c, laccess, c, lmod, "\n\n"] # uses this, as found on stackoverflow easier code management. | |
outfile = open("./dirtree2.csv", "at") # opens csv file in append text mode. | |
outfile.write("".join(t1)) # joins all varibales into a single line, easier code management. | |
outfile.close() | |
except OSError as error: | |
return accurate_scan # upon errors, be quite and return what you found. | |
accurate_scan(path1) | |
for item in path2: | |
get_tree_size(item) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment