diff --git a/check_file.py b/check_file.py index 59eea2805b3..1e4b1ba527e 100644 --- a/check_file.py +++ b/check_file.py @@ -1,37 +1,49 @@ -# Script Name : check_file.py +# Script Name : check_file.py # Author : Craig Richards # Created : 20 May 2013 -# Last Modified : +# Last Modified : # Version : 1.0 # Modifications : with statement added to ensure correct file closure # Description : Check a file exists and that we can read the file +from __future__ import print_function import sys # Import the Modules import os # Import the Modules -# Readfile Functions which open the file that is passed to the script +# Prints usage if not appropriate length of arguments are provided +def usage(): + print('[-] Usage: python check_file.py [filename2] ... [filenameN]') + exit(0) + +# Readfile Functions which open the file that is passed to the script def readfile(filename): with open(filename, 'r') as f: # Ensure file is correctly closed under all circumstances line = f.read() - print line + print(line) def main(): - if len(sys.argv) == 2: # Check the arguments passed to the script - filename = sys.argv[1] # The filename is the first argument - if not os.path.isfile(filename): # Check the File exists - print '[-] ' + filename + ' does not exist.' - exit(0) - if not os.access(filename, os.R_OK): # Check you can read the file - print '[-] ' + filename + ' access denied' - exit(0) + if len(sys.argv) >= 2: # Check the arguments passed to the script + filenames = sys.argv[1:] + for filename in filenames: # Iterate for each filename passed in command line argument + if not os.path.isfile(filename): # Check the File exists + print ('[-] ' + filename + ' does not exist.') + filenames.remove(filename) #remove non existing files from filenames list + continue + + if not os.access(filename, os.R_OK): # Check you can read the file + print ('[-] ' + filename + ' access denied') + filenames.remove(filename) # remove non readable filenames + continue else: - print '[-] Usage: ' + str(sys.argv[0]) + ' ' # Print usage if not all parameters passed/Checked - exit(0) - print '[+] Reading from : ' + filename # Display Message and read the file contents - readfile(filename) + usage() # Print usage if not all parameters passed/Checked + + # Read the content of each file + for filename in filenames: + print ('[+] Reading from : ' + filename) # Display Message and read the file contents + readfile(filename) if __name__ == '__main__': - main() + main() diff --git a/fileinfo.py b/fileinfo.py index 3ad3e54a0fd..93301383e24 100644 --- a/fileinfo.py +++ b/fileinfo.py @@ -1,21 +1,34 @@ # Script Name : fileinfo.py # Author : Not sure where I got this from # Created : 28th November 2011 -# Last Modified : +# Last Modified : # Version : 1.0 -# Modifications : +# Modifications : # Description : Show file information for a given file # get file information using os.stat() # tested with Python24 vegsaeat 25sep2006 +from __future__ import print_function import os +import sys import stat # index constants for os.stat() import time -# pick a file you have ... -file_name = raw_input("Enter a file name: ") -file_stats = os.stat(file_name) + +try_count = 16 +while try_count: + file_name = raw_input("Enter a file name: ") # pick a file you have ... + try_count >>= 1 + try : + file_stats = os.stat(file_name) + break + except OSError: + print ("\nNameError : [%s] No such file or directory\n" %file_name) + +if try_count == 0: + print ("Trial limit exceded \nExiting program") + sys.exit() # create a dictionary to hold file info file_info = { 'fname': file_name, @@ -25,23 +38,24 @@ 'f_ct': time.strftime("%d/%m/%Y %I:%M:%S %p",time.localtime(file_stats[stat.ST_CTIME])) } print -print "file name = %(fname)s" % file_info -print "file size = %(fsize)s bytes" % file_info -print "last modified = %(f_lm)s" % file_info -print "last accessed = %(f_la)s" % file_info -print "creation time = %(f_ct)s" % file_info +print ("file name = %(fname)s" % file_info) +print ("file size = %(fsize)s bytes" % file_info) +print ("last modified = %(f_lm)s" % file_info) +print ("last accessed = %(f_la)s" % file_info) +print ("creation time = %(f_ct)s" % file_info) print if stat.S_ISDIR(file_stats[stat.ST_MODE]): - print "This a directory" + print ("This a directory") else: - print "This is not a directory" - print - print "A closer look at the os.stat(%s) tuple:" % file_name - print file_stats - print - print "The above tuple has the following sequence:" - print """st_mode (protection bits), st_ino (inode number), + print ("This is not a directory") + print () + print ("A closer look at the os.stat(%s) tuple:" % file_name) + print (file_stats) + print () + print ("The above tuple has the following sequence:") + print ("""st_mode (protection bits), st_ino (inode number), st_dev (device), st_nlink (number of hard links), st_uid (user ID of owner), st_gid (group ID of owner), st_size (file size, bytes), st_atime (last access time, seconds since epoch), st_mtime (last modification time), st_ctime (time of creation, Windows)""" +)