Skip to content

Commit 61c751b

Browse files
committed
Revert "my"
This reverts commit c23bbc7.
1 parent c23bbc7 commit 61c751b

31 files changed

+1224
-0
lines changed

README.md

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
Here is some more detailed information about the scripts I have written. I do not consider myself a programmer, I create these little programs as experiments to have a play with the language, or to solve a problem for myself. I would gladly accept pointers from others to improve the code and make it more efficient, or simplify the code. If you would like to make any comments then please feel free to email me at [email protected].
2+
3+
In the scripts the comments etc are lined up correctly when they are viewed in [Notepad++](https://notepad-plus-plus.org/). This is what I use to code Python scripts.
4+
5+
- `batch_file_rename.py` - This will batch rename a group of files in a given directory, once you pass the current and new extensions.
6+
7+
- `create_dir_if_not_there.py` - Checks to see if a directory exists in the users home directory, if not then create it.
8+
9+
- `dir_test.py` - Tests to see if the directory `testdir` exists, if not it will create the directory for you.
10+
11+
- `env_check.py` - This script will check to see if all of the environment variables I require are set.
12+
13+
- `fileinfo.py` - Show file information for a given file.
14+
15+
- `folder_size.py` - This will scan the current directory and all subdirectories and display the size.
16+
17+
- `logs.py` - This script will search for all `*.log` files in the given directory, zip them using the program you specify and then date stamp them.
18+
19+
- `move_files_over_x_days.py` - This will move all the files from the source directory that are over 240 days old to the destination directory.
20+
21+
- `nslookup_check.py` - This very simple script opens the file `server_list.txt` and the does an nslookup for each one to check the DNS entry.
22+
23+
- `osinfo.py` - Displays some information about the OS you are running this script on.
24+
25+
- `ping_servers.py` - This script will, depending on the arguments supplied will ping the servers associated with that application group.
26+
27+
- `ping_subnet.py` - After supplying the first 3 octets it will scan the final range for available addresses.
28+
29+
- `powerdown_startup.py` - This goes through the server list and pings the machine, if it's up it will load the putty session, if its not it will notify you.
30+
31+
- `puttylogs.py` - This zips up all the logs in the given directory.
32+
33+
- `script_count.py` - This scans my scripts directory and gives a count of the different types of scripts.
34+
35+
- `script_listing.py` - This will list all the files in the given directory, it will also go through all the subdirectories as well.
36+
37+
- `testlines.py` - This very simple script open a file and prints out 100 lines of whatever is set for the line variable.

backup_automater_services.py

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Script Name : backup_automater_services.py
2+
# Author : Craig Richards
3+
# Created : 24th October 2012
4+
# Last Modified :
5+
# Version : 1.0
6+
7+
# Modifications :
8+
9+
# Description : This will go through and backup all my automator services workflows
10+
11+
import shutil # Load the library module
12+
import datetime # Load the library module
13+
import os # Load the library module
14+
15+
today=datetime.date.today() # Get Today's date
16+
todaystr=today.isoformat() # Format it so we can use the format to create the directory
17+
18+
confdir=os.getenv("my_config") # Set the variable by getting the value from the OS setting
19+
dropbox=os.getenv("dropbox") # Set the variable by getting the value from the OS setting
20+
conffile = ('services.conf') # Set the variable as the name of the configuration file
21+
conffilename=os.path.join(confdir, conffile) # Set the variable by combining the path and the file name
22+
sourcedir=os.path.expanduser('~/Library/Services/') # Source directory of where the scripts are located
23+
destdir=os.path.join(dropbox, "My_backups"+"/"+"Automater_services"+todaystr+"/") # Combine several settings to create the destination backup directory
24+
25+
for file_name in open(conffilename): # Walk through the configuration file
26+
fname = file_name.strip() # Strip out the blank lines from the configuration file
27+
if fname: # For the lines that are not blank
28+
sourcefile=os.path.join(sourcedir, file_name.strip()) # Get the name of the source files to backup
29+
destfile=os.path.join(destdir, file_name.strip()) # Get the name of the destination file names
30+
shutil.copytree(sourcefile, destfile) # Copy the directories

batch_file_rename.py

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# batch_file_rename.py
2+
# Created: 6th August 2012
3+
4+
'''
5+
This will batch rename a group of files in a given directory,
6+
once you pass the current and new extensions
7+
'''
8+
9+
__author__ = 'Craig Richards'
10+
__version__ = '1.0'
11+
12+
import os
13+
import sys
14+
15+
16+
def batch_rename(work_dir, old_ext, new_ext):
17+
'''
18+
This will batch rename a group of files in a given directory,
19+
once you pass the current and new extensions
20+
'''
21+
# files = os.listdir(work_dir)
22+
for filename in os.listdir(work_dir):
23+
# Get the file extension
24+
file_ext = os.path.splitext(filename)[1]
25+
# Start of the logic to check the file extensions, if old_ext = file_ext
26+
if old_ext == file_ext:
27+
# Set newfile to be the filename, replaced with the new extension
28+
newfile = filename.replace(old_ext, new_ext)
29+
# Write the files
30+
os.rename(
31+
os.path.join(work_dir, filename),
32+
os.path.join(work_dir, newfile)
33+
)
34+
35+
36+
def main():
37+
'''
38+
This will be called if the script is directly envoked.
39+
'''
40+
# Set the variable work_dir with the first argument passed
41+
work_dir = sys.argv[1]
42+
# Set the variable old_ext with the second argument passed
43+
old_ext = sys.argv[2]
44+
# Set the variable new_ext with the third argument passed
45+
new_ext = sys.argv[3]
46+
batch_rename(work_dir, old_ext, new_ext)
47+
48+
49+
if __name__ == '__main__':
50+
main()
51+

check_for_sqlite_files.py

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Script Name : check_for_sqlite_files.py
2+
# Author : Craig Richards
3+
# Created : 07 June 2013
4+
# Last Modified :
5+
# Version : 1.0
6+
7+
# Modifications :
8+
9+
# Description : Scans directories to check if there are any sqlite files in there
10+
11+
import os
12+
13+
def isSQLite3(filename):
14+
from os.path import isfile, getsize
15+
16+
if not isfile(filename):
17+
return False
18+
if getsize(filename) < 100: # SQLite database file header is 100 bytes
19+
return False
20+
else:
21+
fd = open(filename, 'rb')
22+
Header = fd.read(100)
23+
fd.close()
24+
25+
if Header[0:16] == 'SQLite format 3\000':
26+
return True
27+
else:
28+
return False
29+
30+
log=open('sqlite_audit.txt','w')
31+
for r,d,f in os.walk(r'.'):
32+
for files in f:
33+
if isSQLite3(files):
34+
print files
35+
print "[+] '%s' **** is a SQLITE database file **** " % os.path.join(r,files)
36+
log.write("[+] '%s' **** is a SQLITE database file **** " % files+'\n')
37+
else:
38+
log.write("[-] '%s' is NOT a sqlite database file" % os.path.join(r,files)+'\n')
39+
log.write("[-] '%s' is NOT a sqlite database file" % files+'\n')

create_dir_if_not_there.py

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Script Name : create_dir_if_not_there.py
2+
# Author : Craig Richards
3+
# Created : 09th January 2012
4+
# Last Modified : 22nd October 2015
5+
# Version : 1.0
6+
# Modifications : Added exceptions
7+
8+
# Description : Checks to see if a directory exists in the users home directory, if not then create it
9+
10+
import os # Import the OS module
11+
try:
12+
home=os.path.expanduser("~") # Set the variable home by expanding the users set home directory
13+
print home # Print the location
14+
if not os.path.exists(home+'/testdir'):
15+
os.makedirs(home+'/testdir') # If not create the directory, inside their home directory
16+
except Exceptions as e:
17+
print e

daily_checks.py

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# Script Name : daily_checks.py
2+
# Author : Craig Richards
3+
# Created : 07th December 2011
4+
# Last Modified : 01st May 2013
5+
# Version : 1.4
6+
7+
# Modifications : 1.1 Removed the static lines for the putty sessions, it now reads a file, loops through and makes the connections.
8+
# : 1.2 Added a variable filename=sys.argv[0] , as when you use __file__ it errors when creating an exe with py2exe.
9+
# : 1.3 Changed the server_list.txt file name and moved the file to the config directory.
10+
# : 1.4 Changed some settings due to getting a new pc
11+
12+
# Description : This simple script loads everything I need to carry out the daily checks for our systems.
13+
14+
import platform # Load the Library Module
15+
import os # Load the Library Module
16+
import subprocess # Load the Library Module
17+
import sys # Load the Library Module
18+
19+
from time import strftime # Load just the strftime Module from Time
20+
21+
def clear_screen(): # Function to clear the screen
22+
if os.name == "posix": # Unix/Linux/MacOS/BSD/etc
23+
os.system('clear') # Clear the Screen
24+
elif os.name in ("nt", "dos", "ce"): # DOS/Windows
25+
os.system('CLS') # Clear the Screen
26+
27+
def print_docs(): # Function to print the daily checks automatically
28+
print "Printing Daily Check Sheets:"
29+
# The command below passes the command line string to open word, open the document, print it then close word down
30+
subprocess.Popen(["C:\\Program Files (x86)\Microsoft Office\Office14\winword.exe", "P:\\\\Documentation\\Daily Docs\\Back office Daily Checks.doc", "/mFilePrintDefault", "/mFileExit"]).communicate()
31+
32+
def putty_sessions(): # Function to load the putty sessions I need
33+
for server in open(conffilename): # Open the file server_list.txt, loop through reading each line - 1.1 -Changed - 1.3 Changed name to use variable conffilename
34+
subprocess.Popen(('putty -load '+server)) # Open the PuTTY sessions - 1.1
35+
36+
def rdp_sessions():
37+
print "Loading RDP Sessions:"
38+
subprocess.Popen("mstsc eclr.rdp") # Open up a terminal session connection and load the euroclear session
39+
40+
def euroclear_docs():
41+
# The command below opens IE and loads the Euroclear password document
42+
subprocess.Popen('"C:\\Program Files\\Internet Explorer\\iexplore.exe"' '"file://fs1\pub_b\Pub_Admin\Documentation\Settlements_Files\PWD\Eclr.doc"')
43+
44+
# End of the functions
45+
46+
# Start of the Main Program
47+
48+
filename=sys.argv[0] # Create the variable filename
49+
confdir = os.getenv("my_config") # Set the variable confdir from the OS environment variable - 1.3
50+
conffile = ('daily_checks_servers.conf') # Set the variable conffile - 1.3
51+
conffilename=os.path.join(confdir, conffile) # Set the variable conffilename by joining confdir and conffile together - 1.3
52+
clear_screen() # Call the clear screen function
53+
# The command below prints a little welcome message, as well as the script name, the date and time and where it was run from.
54+
print "Good Morning " + os.getenv('USERNAME') + ", " + filename, "ran at", strftime("%Y-%m-%d %H:%M:%S"), "on",platform.node(), "run from",os.getcwd()
55+
print_docs() # Call the print_docs function
56+
putty_sessions() # Call the putty_session function
57+
rdp_sessions() # Call the rdp_sessions function
58+
euroclear_docs() # Call the euroclear_docs function

dir_test.py

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Script Name : dir_test.py
2+
# Author : Craig Richards
3+
# Created : 29th November 2011
4+
# Last Modified :
5+
# Version : 1.0
6+
# Modifications :
7+
8+
# Description : Tests to see if the directory testdir exists, if not it will create the directory for you
9+
10+
import os # Import the OS module
11+
if not os.path.exists('testdir'): # Check to see if it exists
12+
os.makedirs('testdir') # Create the directory

env_check.py

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Script Name : env_check.py
2+
# Author : Craig Richards
3+
# Created : 14th May 2012
4+
# Last Modified :
5+
# Version : 1.0
6+
7+
# Modifications :
8+
9+
# Description : This script will check to see if all of the environment variables I require are set
10+
11+
import os
12+
13+
confdir = os.getenv("my_config") # Set the variable confdir from the OS environment variable
14+
conffile = 'env_check.conf' # Set the variable conffile
15+
conffilename=os.path.join(confdir, conffile) # Set the variable conffilename by joining confdir and conffile together
16+
17+
for env_check in open(conffilename): # Open the config file and read all the settings
18+
env_check = env_check.strip() # Set the variable as itsself, but strip the extra text out
19+
print '[{}]'.format(env_check) # Format the Output to be in Square Brackets
20+
newenv = os.getenv(env_check) # Set the variable newenv to get the settings from the OS what is currently set for the settings out the configfile
21+
if newenv is None: # If it doesn't exist
22+
print env_check, 'is not set' # Print it is not set
23+
else: # Else if it does exist
24+
print 'Current Setting for {}={}\n'.format(env_check, newenv) # Print out the details

fileinfo.py

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# Script Name : fileinfo.py
2+
# Author : Not sure where I got this from
3+
# Created : 28th November 2011
4+
# Last Modified :
5+
# Version : 1.0
6+
# Modifications :
7+
8+
# Description : Show file information for a given file
9+
10+
11+
# get file information using os.stat()
12+
# tested with Python24 vegsaeat 25sep2006
13+
import os
14+
import stat # index constants for os.stat()
15+
import time
16+
# pick a file you have ...
17+
file_name = raw_input("Enter a file name: ")
18+
file_stats = os.stat(file_name)
19+
# create a dictionary to hold file info
20+
file_info = {
21+
'fname': file_name,
22+
'fsize': file_stats [stat.ST_SIZE],
23+
'f_lm': time.strftime("%d/%m/%Y %I:%M:%S %p",time.localtime(file_stats[stat.ST_MTIME])),
24+
'f_la': time.strftime("%d/%m/%Y %I:%M:%S %p",time.localtime(file_stats[stat.ST_ATIME])),
25+
'f_ct': time.strftime("%d/%m/%Y %I:%M:%S %p",time.localtime(file_stats[stat.ST_CTIME]))
26+
}
27+
print
28+
print "file name = %(fname)s" % file_info
29+
print "file size = %(fsize)s bytes" % file_info
30+
print "last modified = %(f_lm)s" % file_info
31+
print "last accessed = %(f_la)s" % file_info
32+
print "creation time = %(f_ct)s" % file_info
33+
print
34+
if stat.S_ISDIR(file_stats[stat.ST_MODE]):
35+
print "This a directory"
36+
else:
37+
print "This is not a directory"
38+
print
39+
print "A closer look at the os.stat(%s) tuple:" % file_name
40+
print file_stats
41+
print
42+
print "The above tuple has the following sequence:"
43+
print """st_mode (protection bits), st_ino (inode number),
44+
st_dev (device), st_nlink (number of hard links),
45+
st_uid (user ID of owner), st_gid (group ID of owner),
46+
st_size (file size, bytes), st_atime (last access time, seconds since epoch),
47+
st_mtime (last modification time), st_ctime (time of creation, Windows)"""

folder_size.py

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Script Name : folder_size.py
2+
# Author : Craig Richards
3+
# Created : 19th July 2012
4+
# Last Modified :
5+
# Version : 1.0
6+
7+
# Modifications :
8+
9+
# Description : This will scan the current directory and all subdirectories and display the size.
10+
11+
import os # Load the library module
12+
13+
directory = '.' # Set the variable directory to be the current directory
14+
dir_size = 0 # Set the size to 0
15+
for (path, dirs, files) in os.walk(directory): # Walk through all the directories
16+
for file in files: # Get all the files
17+
filename = os.path.join(path, file)
18+
dir_size += os.path.getsize(filename) # Get the sizes, the following lines print the sizes in bytes, Kb, Mb and Gb
19+
print "Folder Size in Bytes = %0.2f Bytes" % (dir_size)
20+
print "Folder Size in Kilobytes = %0.2f KB" % (dir_size/1024.0)
21+
print "Folder Size in Megabytes = %0.2f MB" % (dir_size/1024/1024.0)
22+
print "Folder Size in Gigabytes = %0.2f GB" % (dir_size/1024/1024/1024.0)

logs.py

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Script Name : logs.py
2+
# Author : Craig Richards
3+
# Created : 13th October 2011
4+
# Last Modified :
5+
# Version : 1.1
6+
# Modifications : 1.1 - Added the variable zip_program so you can set it for the zip program on whichever OS, so to run on a different OS just change the locations of these two variables.
7+
8+
# Description : This script will search for all *.log files in the given directory, zip them using the program you specify and then date stamp them
9+
10+
import os # Load the Library Module
11+
from time import strftime # Load just the strftime Module from Time
12+
13+
logsdir="c:\puttylogs" # Set the Variable logsdir
14+
zip_program="zip.exe" # Set the Variable zip_program - 1.1
15+
16+
for files in os.listdir(logsdir): # Find all the files in the directory
17+
if files.endswith(".log"): # Check to ensure the files in the directory end in .log
18+
files1=files+"."+strftime("%Y-%m-%d")+".zip" # Create the Variable files1, this is the files in the directory, then we add a suffix with the date and the zip extension
19+
os.chdir(logsdir) # Change directory to the logsdir
20+
os.system(zip_program + " " + files1 +" "+ files) # Zip the logs into dated zip files for each server. - 1.1
21+
os.remove(files) # Remove the original log files

move_files_over_x_days.py

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Script Name : move_files_over_x_days.py# Author : Craig Richards# Created : 8th December 2011# Last Modified : # Version : 1.0# Modifications : # Description : This will move all the files from the src directory that are over 240 days old to the destination directory.import shutil, sys, time, os # Import the header filessrc = 'u:\\test' # Set the source directorydst = 'c:\\test' # Set the destination directorynow = time.time() # Get the current timefor f in os.listdir(src): # Loop through all the files in the source directory if os.stat(f).st_mtime < now - 240 * 86400: # Work out how old they are, if they are older than 240 days old if os.path.isfile(f): # Check it's a file shutil.move(f, dst) # Move the files

0 commit comments

Comments
 (0)