Anonymous avatar Anonymous committed bb9c6a0

adding files

Comments (0)

Files changed (18)

MySQL_SchemaUpgrader.py

+#!/usr/bin/env python
+''' @PACKAGE MySQL_SchemaUpgrader
+    @AUTHOR Matt Reid
+    @COPYRIGHT (c) 2012-present Matt Reid
+    @LICENSE: GPLv3, docs/gpl-3.0.txt, http://www.gnu.org/licenses/gpl-3.0.txt
+'''
+from __future__ import division
+import commands
+import os
+import sys
+import datetime
+import logging
+import MySQLdb
+from optparse import OptionParser
+
+## Hardcodes
+code_version = "1.00.1.2"
+d = datetime.datetime.now()
+date = d.isoformat()
+ver = sys.version.split(' ')[0].split(".")
+major=ver[:1]
+minor=ver[1:2]
+version="%s.%s"%(major[0],minor[0])
+
+## Logging functionality
+def logger(detail,level):
+    if(level == "d"):
+        log.debug("%s"% (detail))
+    elif(level == "i"):
+        log.info("%s"% (detail))
+    elif(level == "w"):
+        log.warn("%s"% (detail))
+    elif(level == "e"):
+        log.error("%s"% (detail))
+    elif(level == "c"):
+        log.critical("%s"% (detail))
+
+
+def parse_options():
+    usage = "usage: "
+    parser = OptionParser(usage=usage)
+    parser.add_option("--user", dest="dbuser", default="", help="MySQL user")
+    parser.add_option("--host", dest="dbhost", default="localhost", help="MySQL host (default: localhost)")
+    parser.add_option("--password", dest="dbpass", default="", help="MySQL password")
+    parser.add_option("--port", dest="dbport", type="int", default=3306, help="TCP/IP port (default: 3306)")
+    parser.add_option("--socket", dest="dbsocket", default="/var/lib/mysql/mysql.sock", help="MySQL socket file. Only applies when host is localhost")
+    parser.add_option("--database", dest="dbdatabase", help="Database name to upgrade")
+    parser.add_option("-v", "--verbose", dest="verbose", action="store_true", help="Print user friendly messages")
+    parser.add_option('--ddlfile', dest='infile', default='', help='DDL upgrade file containing schema structure ONLY (default: none)')
+    parser.add_option('--log', dest='log', default='upgrade.log', help='Log filename (default: upgrade.log)')
+    return parser.parse_args()
+
+## System command executor
+def sysexec(command):
+    logger("Running system command: [%s]"%(command),'i')    
+    start = datetime.datetime.now()
+    retcode, output = commands.getstatusoutput(command)
+    end = datetime.datetime.now()
+    timing = end - start
+    logger("Total compute time: %s"%(timing),'d')
+    if retcode != 0:
+        logger("System command: [%s], code: [%i] [FAILED]"%(command,retcode),'e')
+        return retcode,output
+    else:
+        logger("System command: [OK]",'d')
+        logger("Returning: [%i], [%s]"%(retcode, output),'d')
+        return retcode,output
+
+def verbose(message):
+    if options.verbose:
+        print "-- %s" % message
+
+def print_error(message):
+    print "-- ERROR: %s" % message
+
+def query_exec(query):
+    """
+    Run the given query, commit changes
+    """
+    connection = conn
+    cursor = connection.cursor()
+    num_affected_rows = cursor.execute(query)
+    cursor.close()
+    connection.commit()
+    return num_affected_rows
+
+
+def query_row(query):
+    connection = conn
+    cursor = connection.cursor(MySQLdb.cursors.DictCursor)
+    cursor.execute(query)
+    row = cursor.fetchone()
+
+    cursor.close()
+    return row
+
+
+def query_rows(query):
+    connection = conn
+    cursor = connection.cursor(MySQLdb.cursors.DictCursor)
+    cursor.execute(query)
+    rows = cursor.fetchall()
+
+    cursor.close()
+    return rows
+
+
+def exit_with_error(error_message):
+    """
+    Notify and exit.
+    """
+    print_error(error_message)
+    exit(1)
+
+## Application header info
+def header():
+    print '''>>>>--------------------<<<<
+MySQL_SchemaUpgrader
+<<<<-------------------->>>>
+author: themattreid@gmail.com
+date: 20120716
+'''
+
+## Simple error reporter
+def error(str):
+    print '''Configuration argument [%s] missing. Please set the variable and retry.'''%(str)
+    sys.exit(1)
+
+## Meat and potatoes
+def processor(infile,conn):
+    print os.stat(infile)
+    logger("starting the schema upgrade task for DDL file:[%s]"%(infile),'i')
+
+    logger("exporting data from database",'i')
+    ret0,out0 = sysexec("mysqldump --host=%s --user=%s --password=%s -t -c %s > %s_data-only.sql"%(dbhost,dbuser,dbpass,dbdatabase,dbdatabase))
+    if ret0 is not 0:
+        logger("Data dump for schema failed",'e')
+        return 201
+        
+    logger("importing structure from DDL file",'i')
+    ret1,out1 = sysexec("mysql --host=%s --user=%s --password=%s %s < %s"%(dbhost,dbuser,dbpass,dbdatabase,infile))
+    if ret1 is not 0:
+        logger("DDL import for schema failed",'e')
+        return 202
+        
+    logger("importing data into new schema",'i')
+    ret2,out2 = sysexec("mysql --host=%s --user=%s --password=%s %s < %s_data-only.sql"%(dbhost,dbuser,dbpass,dbdatabase,dbdatabase))
+    if ret2 is not 0:
+        logger("Data import for schema failed",'e')
+        return 203        
+
+
+    '''PRINT SOME STATS'''
+    s_orig = {} #original schema (dictionary)
+    s_new = {} #new schema (dictionary)
+
+    tables = query_rows("SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = '%s' ORDER BY TABLE_NAME"%(dbdatabase))
+    for table in tables:
+        t = table['TABLE_NAME']
+        c = query_row("SELECT COUNT(*) AS ROWS FROM %s.%s"%(dbdatabase,t))
+        s_new.update({t: c["ROWS"]})
+
+    tables = query_rows("SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = '%s' ORDER BY TABLE_NAME"%(dbdatabase+'_backup'))
+    for table in tables:
+        t = table['TABLE_NAME']
+        c = query_row("SELECT COUNT(*) AS ROWS FROM %s.%s"%(dbdatabase+'_backup',t))
+        s_orig.update({t: c["ROWS"]})
+
+    logger("Table row count comparisons",'i')
+    logger("name [orig:new]",'i')
+    for k in s_orig:
+        v1 = s_orig.get(k)
+        v2 = s_new.get(k)
+        logger("%s [%s:%s]"%(k,v1,v2),'i')
+        if v1 != v2:
+            logger("DATA INCONSISTENT: %s[%s:%s]"%(k,v1,v2),'i')
+
+    return 0
+
+
+## Initialization Process
+if __name__ == "__main__":
+    header()    
+
+    '''ARG configuration'''
+    (options, args) = parse_options()
+    infile = options.infile
+    log = options.log
+    dbuser = options.dbuser
+    dbhost = options.dbhost
+    dbpass = options.dbpass
+    dbdatabase = options.dbdatabase
+    dbport = int(options.dbport)
+    dbsocket = options.dbsocket
+    
+    #ensure certain args are set
+    if not infile: error('--ddlfile')
+    if not dbuser: error('--user')
+    if not dbhost: error('--host')
+    if not dbpass: error('--password')
+    if not dbdatabase: error('--database')
+    if not dbport: error('--port')
+    if not dbsocket: error('--socket')
+
+    #create log instances
+    log = logging.getLogger()
+    log.setLevel(logging.DEBUG)
+    formatter = logging.Formatter("%(asctime)s - %(levelname)s - %(message)s")
+
+    c = logging.StreamHandler(sys.stdout)
+    c.setLevel(logging.INFO) 
+    c.setFormatter(formatter)
+    log.addHandler(c) 
+
+    try:
+        f = logging.FileHandler(options.log)
+    except:
+        logger("[CODE:127] - failed to open logfile[%s] for writing. check permissions."%(options.log),'e')
+        sys.exit(127)
+
+    f.setLevel(logging.DEBUG)
+    f.setFormatter(formatter)
+    log.addHandler(f) 
+    #end log creation
+
+    ## START PROCESSING THE JOB
+    conn = None
+    reuse_conn = True
+    try:
+        conn = MySQLdb.connect(
+            host = dbhost,
+            user = dbuser,
+            passwd = dbpass,
+            port = dbport,
+            db = dbdatabase,
+            unix_socket = dbsocket)
+
+    except Exception, err:
+        logger(err,'e')
+        sys.exit(127)
+        
+    try:
+        if infile:
+            if os.stat(infile).st_size > 0:                
+                # Check DDL file for INSERT statements - if we have data in the infile it will clobber production data
+                for line in open(infile):
+                    if "INSERT INTO" in line:
+                        logger("DDL schema upgrade file seems to contain INSERT statements. Please export the DDL file using: 'mysqldump --no-data' and try again",'e')
+                        sys.exit(122)
+            
+                # Run database backup before we start messing with things. Dump and pipe into $dbdatabase_backup.
+                query_exec("drop database if exists %s_backup"%(dbdatabase))
+                query_exec("create database %s_backup"%(dbdatabase))
+                ret,out = sysexec("mysqldump --host=%s --user=%s --password=%s --single-transaction %s | mysql --host=%s --user=%s --password=%s %s_backup"%(
+                        dbhost,dbuser,dbpass,dbdatabase,
+                        dbhost,dbuser,dbpass,dbdatabase))
+                if ret is not 0:
+                    logger("Failed to create backup of database. Exiting.",'e')
+                    sys.exit(198)                                 
+
+                # Start upgrade process with DDL file
+                retcode = processor(infile,conn)
+                if retcode is not 0:
+                    logger("FAILURE CODE: %s"%(retcode),'e')
+                    sys.exit(retcode)
+                    
+                # Close DB connection
+                if conn:
+                    conn.close()                
+
+                # And we're done
+                logger("Operation complete.",'i')
+                sys.exit(0)
+                
+            else:
+                logger("Input file is zero size. Nothing to do. Exiting.",'e')
+                sys.exit(1)
+        else:
+            logger("Nothing to process, but we should have gotten an error if 'infile' wasn't set prior to this anyway",'d')
+            sys.exit(1)
+            
+    except (KeyboardInterrupt, SystemExit):
+        sys.exit(1)
+
+
+

checksum_file-dir_compare.py

+#!/usr/bin/env python
+#Author: Matt Reid
+#Date: 2012.04.24
+#Purpose: generates simple report about common and differing files and directories
+#Input: takes command line options 
+#Outpt: text report
+##
+import filecmp
+import os
+import commands
+import md5
+from optparse import OptionParser
+
+usage = "usage: %prog [options] arg"
+parser = OptionParser(usage=usage)
+parser.add_option("--dir1", dest="dir1", default="", help="first dir")
+parser.add_option("--dir2", dest="dir2", default="", help="second dir")
+parser.add_option("--file1", dest="file1", default="", help="first file")
+parser.add_option("--file2", dest="file2", default="", help="second file")
+
+(options, args) = parser.parse_args()
+
+print "Checksum Comparison Operations"
+print "-------------------------------"
+print "Author: m.reid"
+print "Version: 1.1a"
+print "Date: 2012.04.25"
+print "URL: http://mobileaxiom.com"
+print "-------------------------------"
+
+# to do: check out: http://docs.python.org/library/difflib.html#module-difflib
+
+if options.file1:
+    if options.file2:
+        f1 = options.file1
+        f2 = options.file2
+        state = filecmp.cmp(f1,f2)        
+        if state == False:
+            print "Files are not identical."
+            f1 = commands.getoutput("md5sum %s"%(f1))
+            f2 = commands.getoutput("md5sum %s"%(f2))
+            print "md5: "+f1
+            print "md5: "+f2
+        else:
+            print "Files are identical!"
+            f1 = commands.getoutput("md5sum %s"%(f1))
+            f2 = commands.getoutput("md5sum %s"%(f2))
+            print "md5: "+f1
+            print "md5: "+f2
+
+elif options.dir1:
+    if options.dir2:
+        # to add: do an directory size check first, if it's large then tell the user 'this might take a while'
+        cmp = filecmp.dircmp(options.dir1,options.dir2)        
+        print cmp.report()
+        print '----'
+        # to add: put a progress or status bar here for large dirs
+        print cmp.report_full_closure()
+
+else:
+    print "options not set, please tell me what to do."
+    parser.print_help()
+
+    
+#!/usr/bin/env python
+
+'''http://code.activestate.com/recipes/409689-icmplib-library-for-creating-and-reading-icmp-pack/'''
+
+import dpkt
+import socket, random
+
+echo = dpkt.icmp.ICMP.Echo()
+echo.id = random.randint(0, 0xffff)
+echo.seq = random.randint(0, 0xffff)
+echo.data = 'hello world'
+
+icmp = dpkt.icmp.ICMP()
+icmp.type = dpkt.icmp.ICMP_ECHO
+icmp.data = echo
+
+sock = socket.socket(socket.AF_INET, socket.SOCK_RAW, dpkt.ip.IP_PROTO_ICMP)
+sock.connect(('192.168.200.133', 9192))
+sent = sock.send(str(icmp))
+
+print 'sent %d bytes' % sent
+#!/usr/bin/env python
+from socket import *
+from binascii import *
+from base64 import *
+import sys
+
+def encode(string):
+    return urlsafe_b64encode(binascii.hexlify(str(string)))
+    
+HOST="192.168.200.133"
+PORT=9192
+ADDR = (HOST,PORT)
+MAXCON = 20
+
+serv = socket(AF_INET,SOCK_STREAM)    
+serv.bind((ADDR)) #bind to address
+serv.listen(MAXCON)  #qmax_connections limit
+print 'listening on: %s:%i'%(HOST,PORT)
+
+while 1:
+    try:                            
+        '''we'll just sit around serving forever unless interrupted.'''
+        conn,addr = serv.accept() 
+        print "something connected..."
+        resp = conn.recv(BUFSIZE)
+        print resp
+        
+    except (KeyboardInterrupt, SystemExit):
+        print "killed!"
+        serv.close()
+        sys.exit(1)
+        
+    except:
+        print "dead"
+        serv.close()
+        sys.exit(1)
+
+#!/usr/bin/env python
+from Crypto.Hash import SHA256
+import random
+
+'''generate secure hash for public key'''
+HASH_REPS = 1024
+def __saltedhash(string, salt):
+    sha256 = SHA256.new()
+    sha256.update(string)
+    sha256.update(salt)
+    for x in xrange(HASH_REPS): 
+        sha256.update(sha256.digest())
+        if x % 10: sha256.update(salt)
+    return sha256
+
+def saltedhash_hex(string, salt):
+    """returns the hash in hex format"""
+    return __saltedhash(string, salt).hexdigest()
+
+string = str(random.random())
+salt = str(random.random())
+
+salted = saltedhash_hex(string, salt)
+
+print string
+print salt
+print salted
+

generic_login_manager.py

+#!/usr/bin/env python
+################################################################################
+## NAME: generic_login_manager.py - the login automator and 
+##       command parallel execution environment
+## DATE: 2011-02-04
+## AUTHOR: Matt Reid
+## WEBSITE: http://kontrollsoft.com
+## EMAIL: mreid@kontrollsoft.com
+## LICENSE: BSD http://www.opensource.org/licenses/bsd-license.php
+code_version = "1.1.2"
+################################################################################
+#Files to copy to servers. Data uses list format. Use "= False" to disable
+filenames = ['~/.profile', '~/.bashrc', '~/.bash_logout', '~/.my.cnf']
+#filenames = False
+################################################################################
+from xml.dom import minidom
+import commands
+import sys
+import os
+import operator
+
+xmlfile = "/Users/mreid/bin/hostfile.xml"
+
+'''Check Python version for compatibility'''
+ver = sys.version.split(' ')[0].split(".")
+major=ver[:1]
+minor=ver[1:2]
+version="%s.%s"%(major[0],minor[0])
+#if version not in ('2.6','2.7','2.8','2.9','3.0','3.1','3.2','3.3','3.4'):
+if version < 2.6:
+	print "Please upgrade to python 2.6+"
+	sys.exit(1)
+else:
+	from subprocess import Popen, PIPE, STDOUT, call
+
+def xmlparser(ch):
+	'''go through XML file and append hosts to a list that match the network chosen (ch)
+	Example XML file:
+
+	<?xml version = "1.0"?>
+	<hosts>
+  	  <server hostname="net1-host0"
+	   address="10.0.1.1"
+	   username="root"
+	   network="net1"
+	   />
+
+	  <server hostname="net2-host0"
+	   address="10.0.2.1"
+	   username="root"
+	   network="net2"
+	   />
+	</hosts>'''
+
+	hosts = []
+	file = open(xmlfile,'rU')
+	xmldoc = minidom.parse(file)
+	servers = xmldoc.getElementsByTagName('server')
+	for server in servers:
+		hostname = server.getAttribute('hostname')
+		address = server.getAttribute('address')
+		username = server.getAttribute('username')
+		network = server.getAttribute('network')
+		add = [hostname,address,username,network]		
+		if ch != False:
+			if network == ch:
+				hosts.append(add)
+		else:
+			hosts.append(add)
+
+	return hosts
+
+def main():
+	'''get unique network names from the XML data to generate our menu'''
+	lst = []
+	data = xmlparser(False)
+	for x in range(len(data)):
+		lst.append(data[x][3])		
+
+	lst = list(set(lst)) #assign networks a unique (not order preserved) list
+	for n in range(len(lst)):		
+		print "[%i] %s"%(n+1,lst[n])
+
+	dc = int(raw_input("Datacenter ID: "))
+	hosts = xmlparser(lst[dc-1]) #remove the increment from the ID, then get hosts from XML		
+	nodes = []
+
+	action = ""
+	if action == "l" or action == "L" or action == "":
+		## Login and file copy section
+		nest = sorted(hosts, key=operator.itemgetter(0))
+		for x in range(len(nest)):
+			'''hostname = nest[x][0], addr = nest[x][1], username = nest[x][2]'''
+			print "[%i] %s | %s"%(x,nest[x][1],nest[x][0])
+
+		ch = int(raw_input("ID: "))
+		if filenames != False:
+			for i, file in zip(range(len(filenames)), filenames):
+				try:
+					print "Host: %s - copying %s"%(nest[ch][1],file)
+					call("scp %s %s@%s:. > /dev/null"%(file,nest[ch][2],nest[ch][1]),shell=True)
+				except:
+					print "Failed to copy file"
+					sys.exit(1)
+					
+		try:
+			call("ssh %s@%s"%(nest[ch][2],nest[ch][1]),shell=True)
+		except:
+			print "Failed to ssh"
+			sys.exit(1)
+
+		sys.exit(0)
+
+	elif action == "e" or action == "E":
+		##Execution section
+		stay = True
+		login = False
+		print "\n"
+		while stay == True:
+			nest = sorted(hosts, key=operator.itemgetter(0))
+			for x in range(len(nest)):
+				'''hostname = nest[i][0], addr = nest[i][1], username = nest[i][2]'''
+				print "[%i] %s | %s"%(x,nest[x][1],nest[x][0])
+
+			print "\nSelect node to add to execution list"
+			ch = int(raw_input("ID: "))
+			nodes.append([nest[ch][1],nest[ch][2]]) #addr,username
+			s = str(raw_input("\nAdd another node? [Y/n] "))
+			if s == "n" or s == "N":
+				stay = False
+		
+		addrs = ""
+		for node in nodes:
+			address = (node[1]+"@"+node[0]+",").rstrip("\n")
+			addrs = addrs+address
+
+		cmd = str(raw_input("\nEnter the command to execute: "))
+		l = str(raw_input("\nLogin to each server after executing command? [N/y]: "))
+		if l == "y" or l == "Y":
+			login = True
+
+		## Test for existence of pdsh command, use if it exists. Else we use regular ssh in serial
+		pdsh = call("which pdsh",shell=True)
+		if pdsh == 0 and login == False:
+			print "Command 'pdsh' exists. Executing commands in parallel."
+			try:
+				c = "pdsh -w %s %s"%(addrs.strip(","),cmd)
+				print "Executing: %s"%(c)
+				call(c,shell=True)
+			except:
+				print "Failed to execute pdsh command: %s"%(c)
+				sys.exit(1)
+
+		else:
+			if pdsh != 0:
+				print "Command 'pdsh' not found. Serializing ssh for command execution."
+			else:
+				print "Login after execution chosen. Serializing ssh for command execution."
+
+			for node in nodes:
+				retval = call("ssh -o ConnectTimeout=5 %s@%s \"%s\""%(node[1],node[0],cmd),shell=True)
+				if retval == "1":
+					print "ERROR: ssh command failed. Exit code: %i"%(retval)
+					resp = str(raw_input("Continue? [Y/n]: "))
+					if resp != "Y" or resp != "y":
+						sys.exit(1)
+
+				if login == True:
+					call("ssh -o ConnectTimeout=5 %s@%s"%(node[1],node[0]),shell=True)
+					
+		sys.exit(0)
+
+	else:
+		print "WRONG!"
+		sys.exit(1)
+
+if __name__ == "__main__":
+	print "atomid version: %s"%(code_version)
+	try:
+		retval = main()
+	except (KeyboardInterrupt, SystemExit):
+		sys.exit(1)

generic_lvm_info.py

+#!/usr/bin/python
+''' @PACKAGE generic_lvm_info.py
+    @AUTHOR Matt Reid
+    @COPYRIGHT (c) 2012-present Matt Reid
+    @LICENSE: GPLv3, docs/gpl-3.0.txt, http://www.gnu.org/licenses/gpl-3.0.txt
+'''
+import os
+import sys
+import subprocess
+import select
+import commands
+
+argv = list()
+argv.append("lvdisplay")
+argv.append("-c")
+
+# execute command
+process = subprocess.Popen(argv, stdout=subprocess.PIPE)
+output = ""
+out = process.stdout.readline()
+output += out
+
+# parse command output
+lines = output.splitlines()
+for line in lines:
+    line = line.strip()
+    words = line.strip().split(":")
+    
+    vgname = words[1].strip()
+    lvpath = words[0].strip()
+    last_slash_index = lvpath.rfind("/") + 1
+    lvname = lvpath[last_slash_index:]
+    #self.lvs_paths[vgname + '`' + lvname] = lvpath
+    
+    # lv query command
+    argv = list()
+    argv.append("/sbin/lvm")
+    argv.append("lvs")
+    argv.append("--nosuffix")
+    argv.append("--noheadings")
+    argv.append("--units")
+    argv.append("b")
+    argv.append("--separator")
+    argv.append(";")
+    argv.append("-o")
+    argv.append("lv_name,vg_name,stripes,stripesize,lv_attr,lv_uuid,devices,origin,snap_percent,seg_start,seg_size,vg_extent_size,lv_size")
+
+process = subprocess.Popen(argv, stdout=subprocess.PIPE)
+output = ""
+out = process.stdout.readline()
+output += out
+lines = output.splitlines()
+for line in lines:
+    line = line.strip()
+    words = line.split(";")
+    
+    lvname = words[0].strip()
+    vgname = words[1].strip()
+    attrs = words[4].strip()
+    uuid = words[5].strip()
+    extent_size = int(words[11])
+    #seg_start = int(words[9]) / extent_size
+    lv_size = int(words[12]) / extent_size
+    #seg_size = int(words[10]) / extent_size
+    #devices = words[6]
+    
+    print "lvname: %s, vgname: %s, attrs: %s, size: %s"%(lvname, vgname, attrs, extent_size)
+    
+        

generic_mysql_framework.py

+#!/usr/bin/env python
+''' @PACKAGE generic_mysql_framework.py
+    @AUTHOR Matt Reid
+    @COPYRIGHT (c) 2012-present Matt Reid
+    @LICENSE: GPLv3, docs/gpl-3.0.txt, http://www.gnu.org/licenses/gpl-3.0.txt
+'''
+import sys
+import MySQLdb
+from optparse import OptionParser
+
+## Command line arguments
+def parse_options():
+    usage = 'usage: '
+    parser = OptionParser(usage=usage)
+    
+    '''Defaults'''
+    parser.add_option('-l', '--log', dest='log', default='script.log', help='Log filename (default: script.log)')
+    parser.add_option('-c', '--config', dest='config', default='', help='Config filename (default: script.conf)')
+
+    '''DB configs'''
+    parser.add_option("--user", dest="dbuser", default="", help="MySQL user")
+    parser.add_option("--host", dest="dbhost", default="localhost", help="MySQL host (default: localhost)")
+    parser.add_option("--password", dest="dbpass", default="", help="MySQL password")
+    parser.add_option("--port", dest="dbport", type="int", default=3306, help="TCP/IP port (default: 3306)")
+    parser.add_option("--socket", dest="dbsocket", default="/var/lib/mysql/mysql.sock", help="MySQL socket file. Only applies when host is localhost")
+    parser.add_option("--database", dest="dbdatabase", help="Schema name to connect to")
+    
+    return parser.parse_args()
+
+
+## START MySQL Functions
+def query_exec(query):
+    connection = conn
+    cursor = connection.cursor()
+    num_affected_rows = cursor.execute(query)
+    cursor.close()
+    connection.commit()
+    return num_affected_rows
+
+def query_row(query):
+    connection = conn
+    cursor = connection.cursor(MySQLdb.cursors.DictCursor)
+    cursor.execute(query)
+    row = cursor.fetchone()
+    cursor.close()
+    return row
+
+def query_rows(query):
+    connection = conn
+    cursor = connection.cursor(MySQLdb.cursors.DictCursor)
+    cursor.execute(query)
+    rows = cursor.fetchall()
+    cursor.close()
+    return rows
+#### END MySQL Functions
+
+
+if __name__ == "__main__":
+
+    (options, args) = parse_options()
+    try:
+        conn = MySQLdb.connect(
+            host=options.dbhost,
+            user=options.dbuser,
+            passwd=options.dbpass,
+            port=options.dbport,
+            db=options.dbname,
+            unix_socket=options.dbsocket)
+
+    except:
+        sys.exit(1)
+
+

generic_mysql_simple.py

+#!/opt/local/bin/python
+''' @PACKAGE generic_mysql_simple.py
+    @AUTHOR Matt Reid
+    @COPYRIGHT (c) 2012-present Matt Reid
+    @LICENSE: GPLv3, docs/gpl-3.0.txt, http://www.gnu.org/licenses/gpl-3.0.txt
+'''
+
+import sys
+import MySQLdb
+
+my_host = "localhost"
+my_user = "user"
+my_pass = "password"
+my_db = "test"
+
+try:
+    db = MySQLdb.connect(host=my_host, user=my_user, passwd=my_pass, db=my_db)
+except MySQLdb.Error, e:
+     print "Error %d: %s" % (e.args[0], e.args[1])
+     sys.exit (1)
+
+cursor = db.cursor()
+sql = "select column1, column2 from table";
+cursor.execute(sql)
+
+results = cursor.fetchall()
+for row in results:
+    column1 = row[0]
+    column2 = row[1]
+    print "column1: %s, column2: %s"%(column1,column2)
+
+db.close()

generic_password_generator.py

+#!/usr/bin/env python
+''' @PACKAGE generic_password_generator.py
+    @AUTHOR Matt Reid
+    @COPYRIGHT (c) 2012-present Matt Reid
+    @LICENSE: GPLv3, docs/gpl-3.0.txt, http://www.gnu.org/licenses/gpl-3.0.txt
+'''
+
+import random
+import re
+
+def find_words(num=None, 
+               dictfile=None):
+  dictfile = "/usr/share/dict/american-english"
+  r = random.SystemRandom() 
+  f = open(dictfile, "r")
+
+  count = 0
+  chosen = []
+
+  for i in range(num):
+    chosen.append("")
+
+  prog = re.compile("^[a-z]{5,9}$") 
+  for word in f:
+    if(prog.match(word)):
+      for i in range(num): 
+        if(r.randint(0,count) == 0): 
+          chosen[i] = word.strip()
+        count += 1
+
+  return(chosen)
+
+def genpass(num=3):
+  return(" ".join(find_words(num)))
+
+if(__name__ == "__main__"):
+  print genpass()

generic_python_script.py

+#!/usr/bin/env python
+''' @PACKAGE GenericPythonScript
+    @AUTHOR Matt Reid
+    @COPYRIGHT (c) 2012-present Matt Reid
+    @LICENSE: GPLv3, docs/gpl-3.0.txt, http://www.gnu.org/licenses/gpl-3.0.txt
+'''
+from __future__ import division
+import commands
+import os
+import sys
+import datetime
+import logging
+import MySQLdb
+from Queue import *
+from threading import Thread, Lock
+from optparse import OptionParser
+from ConfigParser import ConfigParser
+
+## Hardcodes
+code_version = "1.00.1.2"
+d = datetime.datetime.now()
+date = d.isoformat()
+ver = sys.version.split(' ')[0].split(".")
+major=ver[:1]
+minor=ver[1:2]
+version="%s.%s"%(major[0],minor[0])
+
+## Command line arguments
+def parse_options():
+    usage = 'usage: '
+    parser = OptionParser(usage=usage)
+    
+    '''File configs'''
+    parser.add_option('-i', '--infile', dest='infile', default='', help='Input filename (default: none)')
+    parser.add_option('-d', '--outdir', dest='outputdir', default='output', help='Output directory (default: output)')
+
+    '''Defaults'''
+    parser.add_option('-l', '--log', dest='log', default='script.log', help='Log filename (default: script.log)')
+    parser.add_option('-c', '--config', dest='config', default='', help='Config filename (default: script.conf)')
+
+    '''Thread configs'''
+    parser.add_option('-t', '--threads', dest='threads', default=4, help='Quantity of processing threads to use (default: 4)')
+
+    '''DB configs'''
+    parser.add_option("--user", dest="dbuser", default="", help="MySQL user")
+    parser.add_option("--host", dest="dbhost", default="localhost", help="MySQL host (default: localhost)")
+    parser.add_option("--password", dest="dbpass", default="", help="MySQL password")
+    parser.add_option("--port", dest="dbport", type="int", default=3306, help="TCP/IP port (default: 3306)")
+    parser.add_option("--socket", dest="dbsocket", default="/var/lib/mysql/mysql.sock", help="MySQL socket file. Only applies when host is localhost")
+    parser.add_option("--database", dest="dbdatabase", help="Schema name to connect to")
+    
+    return parser.parse_args()
+
+## Logging functionality
+def logger(detail,level):
+    if(level == "d"):
+        log.debug("%s"% (detail))
+    elif(level == "i"):
+        log.info("%s"% (detail))
+    elif(level == "w"):
+        log.warn("%s"% (detail))
+    elif(level == "e"):
+        log.error("%s"% (detail))
+    elif(level == "c"):
+        log.critical("%s"% (detail))
+
+## Byte formatter for human readable output
+def human(bytes):
+    bytes = float(bytes)
+    if bytes >= 1099511627776:
+        terabytes = bytes / 1099511627776
+        size = '%.2fT' % terabytes
+    elif bytes >= 1073741824:
+        gigabytes = bytes / 1073741824
+        size = '%.2fG' % gigabytes
+    elif bytes >= 1048576:
+        megabytes = bytes / 1048576
+        size = '%.2fM' % megabytes
+    elif bytes >= 1024:
+        kilobytes = bytes / 1024
+        size = '%.2fK' % kilobytes
+    else:
+        size = '%.2fb' % bytes
+    return size
+
+## START MySQL Functions
+def query_exec(query):
+    connection = conn
+    cursor = connection.cursor()
+    num_affected_rows = cursor.execute(query)
+    cursor.close()
+    connection.commit()
+    return num_affected_rows
+
+def query_row(query):
+    connection = conn
+    cursor = connection.cursor(MySQLdb.cursors.DictCursor)
+    cursor.execute(query)
+    row = cursor.fetchone()
+    cursor.close()
+    return row
+
+def query_rows(query):
+    connection = conn
+    cursor = connection.cursor(MySQLdb.cursors.DictCursor)
+    cursor.execute(query)
+    rows = cursor.fetchall()
+    cursor.close()
+    return rows
+#### END MySQL Functions
+
+## System command executor
+def syscmd(command):
+    logger("Running system command: [%s]"%(command),'i')    
+    start = datetime.datetime.now()
+    retcode, output = commands.getstatusoutput(command)
+    end = datetime.datetime.now()
+    timing = end - start
+    logger("Total compute time: %s"%(timing),'d')
+    if retcode != 0:
+        logger("System command: [%s], code: [%i] [FAILED]"%(command,retcode),'e')
+        return retcode,output
+    else:
+        logger("System command: [OK]",'d')
+        logger("Returning: [%i], [%s]"%(retcode, output),'d')
+        return retcode,output
+
+
+## Application header info
+def header():
+    print '''>>>>--------------------<<<<
+Generic.Python.Script
+<<<<-------------------->>>>
+author: themattreid@gmail.com
+date: 20120713
+'''
+
+## Simple error reporter
+def error(str):
+    print '''Configuration argument [%s] missing. Please set the variable and retry.'''%(str)
+    sys.exit(1)
+
+## Meat and potatoes
+def file_processor(inputfile):
+    o = outputdir + '/' + i
+    logger("starting the file processing task for input-file:[%s], output-file:[%s]"%(i,o),'i')
+
+    try:
+        logger("Opening output file for writing:[%s]"%(o),"i")
+        outfile = open(o, "w")    
+    except:
+        logger("[CODE:0128] - failed to open output file [%s], check permissions."%(o),"e")
+        sys.exit(128)
+
+    ''' start processing the input file line by line '''
+    with open(i) as ifile:
+        for line in ifile:            
+            line = line.rstrip()
+            
+            if "string" in line:
+                logger("match for string entry",'d')
+            else:
+                logger("no match for string entry",'d')
+
+            outfile.write(line+"\n")
+
+    logger("processing complete, closing file handle:[%s]"%(o),'d')
+    outfile.close()
+    return 0
+
+## Checks output directory for usablility 
+def check_outputdir(outputdir):
+    logger("Opening output directory for writing:[%s]"%(outputdir),"i")
+    if os.path.isdir(outputdir):
+        logger("Output dir exists",'d')
+    else:
+        logger("Output directory does not exist, creating: [%s]"%(outputdir),'i')
+        retcode,output = syscmd("mkdir %s"%(outputdir))
+        if retcode != 0:
+            logger("[CODE:0168] - failed to open output directory [%s], check permissions."%(outputdir),"e")
+            sys.exit(168)
+
+## Handles the config file variable definitions 
+def config_handler(config):
+    '''test config file existence and access'''
+    sys.stdout.write("Testing %s config access: "% (config))
+    if(os.path.exists(config)):
+        sys.stdout.write("[exists]")
+        if(os.access(config, os.R_OK)):
+            sys.stdout.write("[readable]\n")
+        else:
+            sys.stdout.write("[read-failed][exiting(1)]\n")
+            sys.exit(1)
+    else:
+        sys.stdout.write("[config file does not exist][exiting(1)]\n")
+        sys.exit(1)
+
+    '''initilize config settings'''
+    cfg = ConfigParser()
+    cfg.read([config])
+    header = 'configuration' #config file header string: "[configuration]"
+    threads = config.get(header,'threads')
+    infile = config.get(header,'infile') 
+    outfile = config.get(header,'outfile') 
+    outputdir = config.get(header,'outputdir') 
+    log = config.get(header,'log') 
+
+    return threads,infile,outfile,outputdir,log
+
+## Queue and Thread Functions
+def thread_processor(item):
+    start = time.time()
+    print 'operating on item: %s'%(item)
+    file_processor(item)
+    end = time.time()                
+    elapse = str(round((end - start)*1000))+"ms"
+    print "TIMING|elapse|%s|%s"%(url,elapse)
+
+def thread_worker():
+    while True:
+        item = work_queue.get()
+        thread_processor(item)
+        work_queue.task_done()
+
+def thread_init():
+    for i in range(threads):
+        t = Thread(target=worker)
+        t.setDaemon(True)
+        t.start()
+
+def queue_source():
+    items = open("list-of-items", "r")
+    return items.readlines() 
+
+def queue_init(queue):
+    for items in queue_source():
+        item = items.rstrip()
+        print "inserting into the queue: %s"%(item)
+        queue.put(item)
+
+def queue_process(queue):
+    queue.join()
+
+
+## Main
+if __name__ == "__main__":
+    header()    
+
+    '''ARG configuration'''
+    (options, args) = parse_options()
+    config = options.config
+
+
+    '''Config file based configuration if enabled via ARG value
+    otherwise skip and use ARG config values'''
+    if config:
+        threads,infile,outfile,outputdir,log = config_handler(config)
+    else:
+        threads = options.threads
+        infile = options.infile
+        outfile = options.outfile
+        outputdir = options.outputdir
+        log = options.log
+
+
+    ''' ensure certain args are set '''
+    if not infile: error('--infile')
+    if not outfile: error('--outfile')
+
+
+    ''' create log instances '''
+    log = logging.getLogger()
+    log.setLevel(logging.DEBUG)
+    formatter = logging.Formatter("%(asctime)s - %(levelname)s - %(message)s")
+
+    c = logging.StreamHandler(sys.stdout)
+    c.setLevel(logging.INFO) 
+    c.setFormatter(formatter)
+    log.addHandler(c) 
+
+    try:
+        f = logging.FileHandler(options.log)
+    except:
+        logger("[CODE:127] - failed to open logfile[%s] for writing. check permissions."%(options.log),'e')
+        sys.exit(127)
+
+    f.setLevel(logging.DEBUG)
+    f.setFormatter(formatter)
+    log.addHandler(f) 
+
+
+    ''' sanity checks '''
+    check_outputdir(outputdir)
+
+
+    ''' initialize Queue and Threads: sequence is important'''
+    queue = Queue()
+    thread_init()
+    queue_init(queue)
+    queue_process(queue)

generic_ssh_login.py

+#!/usr/bin/env python
+''' @PACKAGE generic_ssh_login.py
+    @AUTHOR Matt Reid
+    @COPYRIGHT (c) 2012-present Matt Reid
+    @LICENSE: GPLv3, docs/gpl-3.0.txt, http://www.gnu.org/licenses/gpl-3.0.txt
+'''
+import pexpect
+
+ssh_newkey = 'Are you sure you want to continue connecting'
+p=pexpect.spawn('ssh user@host uname -a')
+i=p.expect([ssh_newkey,'password:',pexpect.EOF])
+
+if i==0:
+    print "sending yes..."
+    p.sendline('yes')
+    i=p.expect([ssh_newkey,'password:',pexpect.EOF])
+
+if i==1:
+    print "sending password...",
+    p.sendline("password-here")
+    p.expect(pexpect.EOF)
+
+elif i==2:
+    print "we got a key or a timeout"
+    pass
+
+print p.before # print out the result

memory_usage_full.py

+#!/usr/bin/env python
+
+# Try to determine how much RAM is currently being used per program.
+# Note per _program_, not per process. So for example this script
+# will report RAM used by all httpd process together. In detail it reports:
+# sum(private RAM for program processes) + sum(Shared RAM for program processes)
+# The shared RAM is problematic to calculate, and this script automatically
+# selects the most accurate method available for your kernel.
+
+# Author: P@draigBrady.com
+# Source: http://www.pixelbeat.org/scripts/ps_mem.py
+
+# V1.0      06 Jul 2005     Initial release
+# V1.1      11 Aug 2006     root permission required for accuracy
+# V1.2      08 Nov 2006     Add total to output
+#                           Use KiB,MiB,... for units rather than K,M,...
+# V1.3      22 Nov 2006     Ignore shared col from /proc/$pid/statm for
+#                           2.6 kernels up to and including 2.6.9.
+#                           There it represented the total file backed extent
+# V1.4      23 Nov 2006     Remove total from output as it's meaningless
+#                           (the shared values overlap with other programs).
+#                           Display the shared column. This extra info is
+#                           useful, especially as it overlaps between programs.
+# V1.5      26 Mar 2007     Remove redundant recursion from human()
+# V1.6      05 Jun 2007     Also report number of processes with a given name.
+#                           Patch from riccardo.murri@gmail.com
+# V1.7      20 Sep 2007     Use PSS from /proc/$pid/smaps if available, which
+#                           fixes some over-estimation and allows totalling.
+#                           Enumerate the PIDs directly rather than using ps,
+#                           which fixes the possible race between reading
+#                           RSS with ps, and shared memory with this program.
+#                           Also we can show non truncated command names.
+# V1.8      28 Sep 2007     More accurate matching for stats in /proc/$pid/smaps
+#                           as otherwise could match libraries causing a crash.
+#                           Patch from patrice.bouchand.fedora@gmail.com
+# V1.9      20 Feb 2008     Fix invalid values reported when PSS is available.
+#                           Reported by Andrey Borzenkov <arvidjaar@mail.ru>
+# V2.6      13 Aug 2011
+#   http://github.com/pixelb/scripts/commits/master/scripts/ps_mem.py
+
+# Notes:
+#
+# All interpreted programs where the interpreter is started
+# by the shell or with env, will be merged to the interpreter
+# (as that's what's given to exec). For e.g. all python programs
+# starting with "#!/usr/bin/env python" will be grouped under python.
+# You can change this by using the full command line but that will
+# have the undesirable affect of splitting up programs started with
+# differing parameters (for e.g. mingetty tty[1-6]).
+#
+# For 2.6 kernels up to and including 2.6.13 and later 2.4 redhat kernels
+# (rmap vm without smaps) it can not be accurately determined how many pages
+# are shared between processes in general or within a program in our case:
+# http://lkml.org/lkml/2005/7/6/250
+# A warning is printed if overestimation is possible.
+# In addition for 2.6 kernels up to 2.6.9 inclusive, the shared
+# value in /proc/$pid/statm is the total file-backed extent of a process.
+# We ignore that, introducing more overestimation, again printing a warning.
+# Since kernel 2.6.23-rc8-mm1 PSS is available in smaps, which allows
+# us to calculate a more accurate value for the total RAM used by programs.
+#
+# Programs that use CLONE_VM without CLONE_THREAD are discounted by assuming
+# they're the only programs that have the same /proc/$PID/smaps file for
+# each instance.  This will fail if there are multiple real instances of a
+# program that then use CLONE_VM without CLONE_THREAD, or if a clone changes
+# its memory map while we're checksumming each /proc/$PID/smaps.
+#
+# I don't take account of memory allocated for a program
+# by other programs. For e.g. memory used in the X server for
+# a program could be determined, but is not.
+#
+# FreeBSD is supported if linprocfs is mounted at /compat/linux/proc/
+# FreeBSD 8.0 supports up to a level of Linux 2.6.16
+
+import errno
+import os
+import sys
+
+try:
+    # md5 module is deprecated on python 2.6
+    # so try the newer hashlib first
+    import hashlib
+    md5_new = hashlib.md5
+except ImportError:
+    import md5
+    md5_new = md5.new
+
+
+# The following exits cleanly on Ctrl-C or EPIPE
+# while treating other exceptions as before.
+def std_exceptions(etype, value, tb):
+    sys.excepthook = sys.__excepthook__
+    if issubclass(etype, KeyboardInterrupt):
+        pass
+    elif issubclass(etype, IOError) and value.errno == errno.EPIPE:
+        pass
+    else:
+        sys.__excepthook__(etype, value, tb)
+sys.excepthook = std_exceptions
+
+if os.geteuid() != 0:
+    sys.stderr.write("Sorry, root permission required.\n")
+    if __name__ == '__main__':
+        sys.stderr.close()
+        sys.exit(1)
+
+uname = os.uname()
+if uname[0] == "FreeBSD":
+    proc = "/compat/linux/proc/"
+else:
+    proc = "/proc/"
+
+split_args = False
+if len(sys.argv) == 2 and sys.argv[1] == "--split-args":
+    split_args = True
+
+PAGESIZE = os.sysconf("SC_PAGE_SIZE") / 1024 #KiB
+our_pid = os.getpid()
+
+
+#(major,minor,release)
+def kernel_ver():
+    kv = open(proc + "sys/kernel/osrelease", "rt").readline().split(".")[:3]
+    last = len(kv)
+    if last == 2:
+        kv.append('0')
+    last -= 1
+    for char in "-_":
+        kv[last] = kv[last].split(char)[0]
+    try:
+        int(kv[last])
+    except:
+        kv[last] = 0
+    return (int(kv[0]), int(kv[1]), int(kv[2]))
+
+try:
+    kv = kernel_ver()
+except (IOError, OSError):
+    val = sys.exc_info()[1]
+    if val.errno == errno.ENOENT:
+        sys.stderr.write(
+          "Couldn't access /proc\n"
+          "Only GNU/Linux and FreeBSD (with linprocfs) are supported\n")
+        sys.exit(2)
+    else:
+        raise
+
+have_pss = 0
+
+
+#return Private,Shared
+#Note shared is always a subset of rss (trs is not always)
+def getMemStats(pid):
+    global have_pss
+    mem_id = pid #unique
+    Private_lines = []
+    Shared_lines = []
+    Pss_lines = []
+    Rss = (int(open(proc + str(pid) + "/statm", "rt").readline().split()[1])
+           * PAGESIZE)
+    if os.path.exists(proc + str(pid) + "/smaps"): #stat
+        digester = md5_new()
+        for line in open(proc + str(pid) + "/smaps", "rb").readlines(): #open
+            # Note we checksum smaps as maps is usually but
+            # not always different for separate processes.
+            digester.update(line)
+            line = line.decode("ascii")
+            if line.startswith("Shared"):
+                Shared_lines.append(line)
+            elif line.startswith("Private"):
+                Private_lines.append(line)
+            elif line.startswith("Pss"):
+                have_pss = 1
+                Pss_lines.append(line)
+        mem_id = digester.hexdigest()
+        Shared = sum([int(line.split()[1]) for line in Shared_lines])
+        Private = sum([int(line.split()[1]) for line in Private_lines])
+        #Note Shared + Private = Rss above
+        #The Rss in smaps includes video card mem etc.
+        if have_pss:
+            pss_adjust = 0.5 # add 0.5KiB as this avg error due to trunctation
+            Pss = sum([float(line.split()[1])+pss_adjust for line in Pss_lines])
+            Shared = Pss - Private
+    elif (2,6,1) <= kv <= (2,6,9):
+        Shared = 0 #lots of overestimation, but what can we do?
+        Private = Rss
+    else:
+        Shared = int(open(proc+str(pid)+"/statm", "rt").readline().split()[2])
+        Shared *= PAGESIZE
+        Private = Rss - Shared
+    return (Private, Shared, mem_id)
+
+
+def getCmdName(pid):
+    cmdline = open(proc+"%d/cmdline" % pid, "rt").read().split("\0")
+    if cmdline[-1] == '' and len(cmdline) > 1:
+        cmdline = cmdline[:-1]
+    path = os.path.realpath(proc+"%d/exe" % pid) #exception for kernel threads
+    if split_args:
+        return " ".join(cmdline)
+    if path.endswith(" (deleted)"):
+        path = path[:-10]
+        if os.path.exists(path):
+            path += " [updated]"
+        else:
+            #The path could be have prelink stuff so try cmdline
+            #which might have the full path present. This helped for:
+            #/usr/libexec/notification-area-applet.#prelink#.fX7LCT (deleted)
+            if os.path.exists(cmdline[0]):
+                path = cmdline[0] + " [updated]"
+            else:
+                path += " [deleted]"
+    exe = os.path.basename(path)
+    cmd = open(proc+"%d/status" % pid, "rt").readline()[6:-1]
+    if exe.startswith(cmd):
+        cmd = exe #show non truncated version
+        #Note because we show the non truncated name
+        #one can have separated programs as follows:
+        #584.0 KiB +   1.0 MiB =   1.6 MiB    mozilla-thunder (exe -> bash)
+        # 56.0 MiB +  22.2 MiB =  78.2 MiB    mozilla-thunderbird-bin
+    return cmd
+
+
+cmds = {}
+shareds = {}
+mem_ids = {}
+count = {}
+for pid in os.listdir(proc):
+    if not pid.isdigit():
+        continue
+    pid = int(pid)
+    if pid == our_pid:
+        continue
+    try:
+        cmd = getCmdName(pid)
+    except:
+        #permission denied or
+        #kernel threads don't have exe links or
+        #process gone
+        continue
+    try:
+        private, shared, mem_id = getMemStats(pid)
+    except:
+        continue #process gone
+    if shareds.get(cmd):
+        if have_pss: #add shared portion of PSS together
+            shareds[cmd] += shared
+        elif shareds[cmd] < shared: #just take largest shared val
+            shareds[cmd] = shared
+    else:
+        shareds[cmd] = shared
+    cmds[cmd] = cmds.setdefault(cmd, 0) + private
+    if cmd in count:
+        count[cmd] += 1
+    else:
+        count[cmd] = 1
+    mem_ids.setdefault(cmd, {}).update({mem_id:None})
+
+#Add shared mem for each program
+total = 0
+for cmd in cmds:
+    cmd_count = count[cmd]
+    if len(mem_ids[cmd]) == 1 and cmd_count > 1:
+        # Assume this program is using CLONE_VM without CLONE_THREAD
+        # so only account for one of the processes
+        cmds[cmd] /= cmd_count
+        if have_pss:
+            shareds[cmd] /= cmd_count
+    cmds[cmd] = cmds[cmd] + shareds[cmd]
+    total += cmds[cmd] #valid if PSS available
+
+if sys.version_info >= (2, 6):
+    sort_list = sorted(cmds.items(), key=lambda x:x[1])
+else:
+    sort_list = cmds.items()
+    sort_list.sort(lambda x, y:cmp(x[1], y[1]))
+# list wrapping is redundant on <py3k, needed for >=pyk3 however
+sort_list = list(filter(lambda x:x[1], sort_list)) #get rid of 0 sized processes
+
+
+#The following matches "du -h" output
+#see also human.py
+def human(num, power="Ki"):
+    powers = ["Ki", "Mi", "Gi", "Ti"]
+    while num >= 1000: #4 digits
+        num /= 1024.0
+        power = powers[powers.index(power)+1]
+    return "%.1f %s" % (num, power)
+
+
+def cmd_with_count(cmd, count):
+    if count > 1:
+        return "%s (%u)" % (cmd, count)
+    else:
+        return cmd
+
+
+if __name__ == '__main__':
+    sys.stdout.write(" Private  +   Shared  =  RAM used\tProgram \n\n")
+    for cmd in sort_list:
+        sys.stdout.write("%8sB + %8sB = %8sB\t%s\n" %
+                         (human(cmd[1]-shareds[cmd[0]]),
+                          human(shareds[cmd[0]]), human(cmd[1]),
+                          cmd_with_count(cmd[0], count[cmd[0]])))
+    if have_pss:
+        sys.stdout.write("%s\n%s%8sB\n%s\n" %
+                         ("-" * 33, " " * 24, human(total), "=" * 33))
+    sys.stdout.write("\n Private  +   Shared  =  RAM used\tProgram \n\n")
+    # We must close explicitly, so that any EPIPE exception
+    # is handled by our excepthook, rather than the default
+    # one which is reenabled after this script finishes.
+    sys.stdout.close()
+
+
+#Warn of possible inaccuracies
+#2 = accurate & can total
+#1 = accurate only considering each process in isolation
+#0 = some shared mem not reported
+#-1= all shared mem not reported
+def shared_val_accuracy():
+    """http://wiki.apache.org/spamassassin/TopSharedMemoryBug"""
+    if kv[:2] == (2,4):
+        if open(proc+"meminfo", "rt").read().find("Inact_") == -1:
+            return 1
+        return 0
+    elif kv[:2] == (2,6):
+        pid = str(os.getpid())
+        if os.path.exists(proc+pid+"/smaps"):
+            if open(proc+pid+"/smaps", "rt").read().find("Pss:")!=-1:
+                return 2
+            else:
+                return 1
+        if (2,6,1) <= kv <= (2,6,9):
+            return -1
+        return 0
+    elif kv[0] > 2:
+        return 2
+    else:
+        return 1
+
+
+if __name__ == '__main__':
+    vm_accuracy = shared_val_accuracy()
+    if vm_accuracy == -1:
+        sys.stderr.write(
+         "Warning: Shared memory is not reported by this system.\n"
+        )
+        sys.stderr.write(
+         "Values reported will be too large, and totals are not reported\n"
+        )
+    elif vm_accuracy == 0:
+        sys.stderr.write(
+         "Warning: Shared memory is not reported accurately by this system.\n"
+        )
+        sys.stderr.write(
+         "Values reported could be too large, and totals are not reported\n"
+        )
+    elif vm_accuracy == 1:
+        sys.stderr.write(
+         "Warning: Shared memory is slightly over-estimated by this system\n"
+         "for each program, so totals are not reported.\n"
+        )
+    sys.stderr.close()

mysql_binlog_purger.py

+#!/usr/bin/python
+#Name: Umang Gopani
+#Purpose: Purging Binary logs from Master DBs.
+#Disclaimer:Obviously, you use this document at your own risk. I am not responsible for any damage or injury caused by your use of this document, or #caused by errors  and/or omissions in this document. If that's not acceptable to you, you may not use this document. By using this document you are #accepting this disclaimer.
+
+# Before executing emerge -av mysql-python numeric  (on Gentoo)
+# Before executing apt-get install python-mysqldb  and apt-get install python-numeric (on Ubuntu)
+
+import sys
+import string
+import array
+import commands         # API for running unix commands
+import MySQLdb          # API for accessing to Mysql database
+from Numeric import *
+
+# Database credentials
+user = 'username'
+password = 'mypassword'
+database = 'mysql'
+
+# Function returning the hostnames of slaves connected to the master DB.
+def slave_host():
+        try:
+                db = MySQLdb.connect (host = "localhost",
+                                        user = user,
+                                        passwd = password,
+                                        db = database)
+        except MySQLdb.Error, e:
+                print "Error %d: %s" % (e.args[0], e.args[1])
+                sys.exit(1)
+        cursor = db.cursor()
+        cursor.execute("show processlist" )     # Get the output of show processlist to find the slave hostname.For connected slave one can find the command column as "Binlog Dump".
+        numrows = int(cursor.rowcount)
+        cntr = 0
+        slave1 = list([0, 0, 0])                # Initialising the slave1 variable for stopring the hostnames for slaves.RIght now it is limited to 3 slaves.
+        for x in range(0,numrows):
+                result1 = cursor.fetchone()
+                if result1[4] == "Binlog Dump":
+                        if ':' in result1[2]:           # Output is seen in the form of hosname:port number.
+                                sslave = string.split(result1[2],':')           # Seperating port number from hostname.
+                                slave1[cntr] = sslave[0]
+                        cntr = cntr + 1
+                        return slave1                           # Returning the name of the slave hostnames.
+                else:
+                        return 0
+
+
+# This section gives the output as the fullpath of the directory in which the bin-logs reside on the Master.The information is extraceted form the mysql config file.
+ldir = "cat /etc/mysql/my.cnf|grep log-bin"
+ldiro = commands.getoutput(ldir)
+ldirop = ldiro.rsplit()
+if '/' in ldirop[2]:            # Output of log-bin gives the full path name icluding the filename.
+        ts = string.split(ldirop[2],'/')        # Seperating the filename, since it is not desired to check the disk space.
+tsrem = ts[0:len(ts) -1]
+jtsrem = string.join(tsrem,"/")         # Joining the string with "/" to give the full path of the directory.
+
+
+# This section takes the full pathname form the previous section and checks the size of that partition using df -h
+dfh = 'df -h %s' % jtsrem
+dfho = commands.getoutput(dfh)
+dfhop = dfho.rsplit()
+dfhintop =  dfhop[11]           # Check the 11th variable in the array for percentage of size used.
+num = dfhintop[0] + dfhintop[1]
+if dfhintop[1] == "%":          # If the output is in 1 digit  i.e is less than 10%, then purgning is not required.
+        print "Less than 10% of disk space being used.Aborting purgin of logs."
+        sys.exit(0)
+size = string.atoi(num)
+
+# This section check for the latest master bin log file being used by the slave and gives it as the output.
+if  slave_host() != 0:
+        uhost = slave_host()            # Save the returned value  from the function slave_host into a variable.
+#       print "Slave host name : %s" % uhost
+        slvlog = list([0, 0, 0])
+        for i in range(0,len(uhost)):
+                if uhost[i] != 0:       # To keep a check on the number of slaves.If there is only 1 slave get the name and stop the loop.
+                        rhost = uhost[i]
+                        srhost = str(rhost)
+                        try:
+                                db = MySQLdb.connect (host="%s" % (srhost[0:]),
+                                        user = user,
+                                        passwd = password,
+                                        db = database)
+                        except MySQLdb.Error, e:
+                                print "Error %d: %s" % (e.args[0], e.args[1])
+                                sys.exit(1)
+                        cursor = db.cursor()
+                        cursor.execute("show slave status" )
+                        result = cursor.fetchone()
+                        slvlog[i] = result[9]
+else:
+        print "No slave connected to this master.Aborting purgin of logs.Will try again in next run."
+        sys.exit(0)
+flog = slvlog[i]        # Store the result into a variable. This is useful if there is only 1 slave to the master and the next loop is not followed.
+
+# If the slaves are more than 1, compare which is the latest file being used by any slave.This is to avoid purgning a file being used by any other slave.
+if i > 1:
+        for x in range(0,i):
+                if slvlog[x] != 0:
+                        if slvlog[x] != slvlog[x+1]:
+                                if slvlog[x] > slvlog[x+1]:
+                                        flog = slvlog[x]
+                                else:
+                                        flog = slvlog[x+1]
+
+final = str(flog)       # The final name of the file upto which teh logs needs to be purged have been stored in "final " variable.
+
+# This is the section which will use the outputs form all the previous section and purge the binary logs if the disk space used is greater than 50%.
+if size > 50:
+        try:
+                db = MySQLdb.connect (host="localhost",
+                        user = user,
+                        passwd = password,
+                        db = database)
+        except MySQLdb.Error, e:
+                print "Error %d: %s" % (e.args[0], e.args[1])
+                sys.exit(1)
+        cursor = db.cursor()
+        sql = """ purge master logs to '%s' """ % (final[0:])   # Mysql command to purge binary logs from master.
+#       print "Output is : %s" % sql
+        cursor.execute(sql)
+        print "Binary logs from master have been purged upto %s." % final
+        sys.exit(0)
+else:
+        print "Less than 50% of disk space bein used.Aborting purging of logs."
+        sys.exit(0)

mysql_bulk_change_root_pass.py

+#!/usr/bin/env python
+################################################################################
+## NAME: shells_all
+## DATE: 2010-12-28
+## AUTHOR: Matt Reid
+## WEBSITE: http://kontrollsoft.com
+## EMAIL: mreid@kontrollsoft.com
+## LICENSE: BSD http://www.opensource.org/licenses/bsd-license.php
+################################################################################
+import commands
+import sys
+import os
+import sys, re
+import time
+import operator
+from subprocess import Popen, PIPE, STDOUT, call
+
+def main():
+	hosts = [
+		["10.20.70.26","password"],
+		["10.20.30.38","password"],
+		["10.20.30.30","password"],
+		["10.20.30.32","password"],
+		["10.20.30.33","password"],
+		["10.20.30.135","password"],
+		["10.20.30.136","password"],
+		["10.20.30.37","password"],
+		["10.10.30.130","password"],
+		["10.10.30.131","password"],
+		["10.10.30.30","password"],
+		["10.10.30.31","password"],
+		["10.10.30.39","password"],
+		["10.10.30.32","password"],
+		["10.10.30.34","password"],
+		["10.10.30.24","password"],
+		["10.10.30.38","password"],
+		["10.10.30.199","platform"],
+		["10.10.30.35","password"],
+		["10.10.30.36","password"],
+		["10.20.70.130","password"],
+		["10.20.70.131","password"],
+		["10.20.70.30","password"],
+		["10.20.70.31","password"],
+		["10.20.70.37","password"],
+		["10.20.70.38","password"],
+		["10.20.70.32","password"],
+		["10.20.70.33","password"],
+		["10.20.70.199","platform"],
+		["10.20.70.35","password"],
+		["10.20.70.36","password"],
+		["10.20.50.30","password"],
+		["10.20.50.31","password"],
+		["10.20.50.32","password"],
+		["10.20.50.33","password"],
+		["10.20.50.34","password"],
+		["10.20.50.35","password"],
+		["10.20.50.36","password"],
+		["10.20.40.22","password"],
+		["10.20.40.24","password"],
+		["10.30.40.21","password"],
+		["10.30.40.22","password"],
+		["10.20.50.40","password"],
+		["10.20.50.41","password"],
+		["10.20.50.42","password"],
+		["10.20.50.43","password"],
+		["10.20.50.44","password"],
+		["10.20.50.45","password"],
+		["10.20.50.46","password"],
+		["10.20.50.47","password"],
+		["10.10.30.132","password"],
+		["10.10.30.133","password"],
+		["10.20.70.140","password"],
+		["10.20.70.141","password"],
+		["10.20.80.30","password"],
+		["10.20.80.31","password"],
+		["10.20.70.142","password"],
+		["10.20.30.128","password"],
+		["10.20.30.129","password"],
+		["10.20.30.35","password"],
+		["10.20.30.36","password"],
+		["10.10.30.135","password"],
+		["10.10.30.136","password"],
+		["10.20.50.50","password"],
+		["10.20.50.51","password"],
+		["10.20.50.52","password"],
+		["10.20.50.53","password"],
+		["10.20.50.54","password"],
+		["10.20.50.55","password"],
+		["10.20.50.56","password"],
+		["10.20.50.57","password"],
+		["10.20.30.111","password"],
+		["10.20.30.112","password"],
+		["10.20.30.113","password"],
+		["10.20.30.114","password"],
+		["10.20.30.115","password"],
+		["10.20.30.116","password"],
+		["10.20.30.117","password"],
+		["10.20.30.118","password"],
+		["10.20.50.60","password"],
+		["10.20.50.61","password"],
+		["10.20.50.62","password"],
+		["10.20.50.63","password"],
+		["10.20.30.104","password"],
+		["10.20.30.105","password"],
+		["10.20.30.106","password"],
+		["10.20.30.107","password"]]
+
+
+	for i in range(len(hosts)):
+		addr = hosts[i][0]
+		pwd = hosts[i][1]
+		print "ADDR: %s, PWD: %s"%(addr,pwd) 
+		a = "new_pass"
+		
+		#cmd = "ssh -o ConnectTimeout=5 mreid@%s \"/usr/bin/mysql -uroot --host=127.0.0.1 --password=%s -e \\\"use mysql;update user set Password=password('newpass') where User='root';flush privileges;\\\"\""%(addr,pwd)
+		cmd = "ssh -o ConnectTimeout=5 mreid@%s \"/usr/bin/mysql -uroot --host=127.0.0.1 --password=%s -e \\\"select 1;\\\"\""%(addr,a)
+		
+		
+		try:
+			call(cmd, shell=True)
+		except:
+			print "Failed on addr: %s"%(addr)
+			
+	return 
+
+#### START SOME SHIT
+if __name__ == "__main__":
+	try:
+		retval = main()
+	except (KeyboardInterrupt, SystemExit):
+		sys.exit(1)

port_scanner-1.py

+#!/usr/bin/env python
+## {{{ http://code.activestate.com/recipes/286240/ (r1)
+# a simple portscanner with multithreading
+
+import socket as sk
+import sys
+import threading
+
+MAX_THREADS = 50
+
+def usage():
+    print "\npyScan 0.1"
+    print "usage: pyScan <host> [start port] [end port]"
+    
+class Scanner(threading.Thread):
+    def __init__(self, host, port):
+        threading.Thread.__init__(self)
+        # host and port
+        self.host = host
+        self.port = port
+        # build up the socket obj
+        self.sd = sk.socket(sk.AF_INET, sk.SOCK_STREAM)
+
+    def run(self):
+        try:
+            # connect to the given host:port
+            self.sd.connect((self.host, self.port))
+            print "%s:%d OPEN" % (self.host, self.port)
+            self.sd.close()
+        except: pass
+
+class pyScan:
+    def __init__(self, args=[]):
+        # arguments vector
+        self.args = args
+        # start port and end port
+        self.start, self.stop = 1, 1024
+        # host name
+        self.host = ""
+
+        # check the arguments
+        if len(self.args) == 4:
+            self.host = self.args[1]
+            try:
+                self.start = int(self.args[2])
+                self.stop = int(self.args[3])
+            except ValueError:
+                usage()
+                return
+            if self.start > self.stop:
+                usage()
+                return
+        elif len(self.args) == 2:
+            self.host = self.args[1]
+        else:
+            usage()
+            return
+
+        try:
+            sk.gethostbyname(self.host)
+        except:
+            print "hostname '%s' unknown" % self.host
+        self.scan(self.host, self.start, self.stop)
+
+    def scan(self, host, start, stop):
+        self.port = start
+        while self.port <= stop:
+            while threading.activeCount() < MAX_THREADS:
+                Scanner(host, self.port).start()
+                self.port += 1
+        
+if __name__ == "__main__":
+    pyScan(sys.argv)

port_scanner-2.py

+#!/usr/bin/env python
+# a simple portscanner with multithreading
+# QUEUE BASED VERSION
+
+import socket
+import sys
+import threading, Queue
+
+MAX_THREADS = 50
+
+class Scanner(threading.Thread):
+    def __init__(self, inq, outq):
+        threading.Thread.__init__(self)
+        self.setDaemon(1)
+        # queues for (host, port)
+        self.inq = inq
+        self.outq = outq
+
+    def run(self):
+        while 1:
+            host, port = self.inq.get()
+            sd = socket.socket(socket.AF_INET, socket.SOCK_STREAM)        
+            try:
+                # connect to the given host:port
+                sd.connect((host, port))
+            except socket.error:
+                # set the CLOSED flag
+                self.outq.put((host, port, 'CLOSED'))
+            else:
+                self.outq.put((host, port, 'OPEN'))
+                sd.close()
+
+def scan(host, start, stop, nthreads=MAX_THREADS):
+    toscan = Queue.Queue()
+    scanned = Queue.Queue()
+
+    scanners = [Scanner(toscan, scanned) for i in range(nthreads)]
+    for scanner in scanners:
+        scanner.start()
+
+    hostports = [(host, port) for port in xrange(start, stop+1)]
+    for hostport in hostports:
+        toscan.put(hostport)
+
+    results = {}
+    for host, port in hostports:
+        while (host, port) not in results:
+            nhost, nport, nstatus = scanned.get()
+            results[(nhost, nport)] = nstatus
+        status = results[(host, port)]
+        if status <> 'CLOSED':
+            print '%s:%d %s' % (host, port, status)
+
+if __name__ == '__main__':
+    scan('192.168.200.1', 0, 1024)
+## end of http://code.activestate.com/recipes/286240/ }}}

whirlpool-hash.py

+## whirlpool.py - pure Python implementation of the Whirlpool algorithm.
+## Bjorn Edstrom <be@bjrn.se> 16 december 2007.
+##
+## Copyrights
+## ==========
+##
+## This code is based on the reference implementation by
+## Paulo S.L.M. Barreto and Vincent Rijmen. The reference implementation
+## is placed in the public domain but has the following headers:
+##
+## * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ''AS IS'' AND ANY EXPRESS
+## * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+## * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+## * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE
+## * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+## * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+## * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
+## * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+## * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
+## * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
+## * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+## *
+## */
+## /* The code contained in this file (Whirlpool.c) is in the public domain. */
+##
+## This Python implementation is therefore also placed in the public domain.
+
+try:
+    import psyco
+    psyco.full()
+except ImportError:
+    pass
+
+#block_size = 64
+digest_size = 64
+digestsize = 64
+
+class Whirlpool:
+    """Return a new Whirlpool object. An optional string argument
+    may be provided; if present, this string will be automatically
+    hashed."""
+    def __init__(self, arg=None):
+        self.ctx = WhirlpoolStruct()
+        if arg:
+            self.update(arg)
+        self.digest_status = 0
+        
+    def update(self, arg):
+        """update(arg)"""
+        WhirlpoolAdd(arg, len(arg)*8, self.ctx)
+        self.digest_status = 0
+        
+    def digest(self):
+        """digest()"""
+        if self.digest_status == 0:
+            self.dig = WhirlpoolFinalize(self.ctx)
+        self.digest_status = 1
+        return self.dig
+    
+    def hexdigest(self):
+        """hexdigest()"""
+        dig = self.digest()
+        tempstr = ''
+        for d in dig:
+            xxx = '%02x' % (ord(d))
+            tempstr = tempstr + xxx
+        return tempstr
+    
+    def copy(self):
+        """copy()"""
+        import copy
+        return copy.deepcopy(self)
+    
+
+def new(init=None):
+    """Return a new Whirlpool object. An optional string argument
+    may be provided; if present, this string will be automatically
+    hashed."""
+    return Whirlpool(init)
+
+#
+# Private.
+#
+
+R = 10
+
+C0 = [
+0x18186018c07830d8, 0x23238c2305af4626, 0xc6c63fc67ef991b8, 0xe8e887e8136fcdfb, 
+0x878726874ca113cb, 0xb8b8dab8a9626d11, 0x0101040108050209, 0x4f4f214f426e9e0d, 
+0x3636d836adee6c9b, 0xa6a6a2a6590451ff, 0xd2d26fd2debdb90c, 0xf5f5f3f5fb06f70e, 
+0x7979f979ef80f296, 0x6f6fa16f5fcede30, 0x91917e91fcef3f6d, 0x52525552aa07a4f8, 
+0x60609d6027fdc047, 0xbcbccabc89766535, 0x9b9b569baccd2b37, 0x8e8e028e048c018a, 
+0xa3a3b6a371155bd2, 0x0c0c300c603c186c, 0x7b7bf17bff8af684, 0x3535d435b5e16a80, 
+0x1d1d741de8693af5, 0xe0e0a7e05347ddb3, 0xd7d77bd7f6acb321, 0xc2c22fc25eed999c, 
+0x2e2eb82e6d965c43, 0x4b4b314b627a9629, 0xfefedffea321e15d, 0x575741578216aed5, 
+0x15155415a8412abd, 0x7777c1779fb6eee8, 0x3737dc37a5eb6e92, 0xe5e5b3e57b56d79e, 
+0x9f9f469f8cd92313, 0xf0f0e7f0d317fd23, 0x4a4a354a6a7f9420, 0xdada4fda9e95a944, 
+0x58587d58fa25b0a2, 0xc9c903c906ca8fcf, 0x2929a429558d527c, 0x0a0a280a5022145a, 
+0xb1b1feb1e14f7f50, 0xa0a0baa0691a5dc9, 0x6b6bb16b7fdad614, 0x85852e855cab17d9, 
+0xbdbdcebd8173673c, 0x5d5d695dd234ba8f, 0x1010401080502090, 0xf4f4f7f4f303f507, 
+0xcbcb0bcb16c08bdd, 0x3e3ef83eedc67cd3, 0x0505140528110a2d, 0x676781671fe6ce78, 
+0xe4e4b7e47353d597, 0x27279c2725bb4e02, 0x4141194132588273, 0x8b8b168b2c9d0ba7, 
+0xa7a7a6a7510153f6, 0x7d7de97dcf94fab2, 0x95956e95dcfb3749, 0xd8d847d88e9fad56, 
+0xfbfbcbfb8b30eb70, 0xeeee9fee2371c1cd, 0x7c7ced7cc791f8bb, 0x6666856617e3cc71, 
+0xdddd53dda68ea77b, 0x17175c17b84b2eaf, 0x4747014702468e45, 0x9e9e429e84dc211a, 
+0xcaca0fca1ec589d4, 0x2d2db42d75995a58, 0xbfbfc6bf9179632e, 0x07071c07381b0e3f, 
+0xadad8ead012347ac, 0x5a5a755aea2fb4b0, 0x838336836cb51bef, 0x3333cc3385ff66b6, 
+0x636391633ff2c65c, 0x02020802100a0412, 0xaaaa92aa39384993, 0x7171d971afa8e2de, 
+0xc8c807c80ecf8dc6, 0x19196419c87d32d1, 0x494939497270923b, 0xd9d943d9869aaf5f, 
+0xf2f2eff2c31df931, 0xe3e3abe34b48dba8, 0x5b5b715be22ab6b9, 0x88881a8834920dbc, 
+0x9a9a529aa4c8293e, 0x262698262dbe4c0b, 0x3232c8328dfa64bf, 0xb0b0fab0e94a7d59, 
+0xe9e983e91b6acff2, 0x0f0f3c0f78331e77, 0xd5d573d5e6a6b733, 0x80803a8074ba1df4, 
+0xbebec2be997c6127, 0xcdcd13cd26de87eb, 0x3434d034bde46889, 0x48483d487a759032, 
+0xffffdbffab24e354, 0x7a7af57af78ff48d, 0x90907a90f4ea3d64, 0x5f5f615fc23ebe9d, 
+0x202080201da0403d, 0x6868bd6867d5d00f, 0x1a1a681ad07234ca, 0xaeae82ae192c41b7, 
+0xb4b4eab4c95e757d, 0x54544d549a19a8ce, 0x93937693ece53b7f, 0x222288220daa442f, 
+0x64648d6407e9c863, 0xf1f1e3f1db12ff2a, 0x7373d173bfa2e6cc, 0x12124812905a2482, 
+0x40401d403a5d807a, 0x0808200840281048, 0xc3c32bc356e89b95, 0xecec97ec337bc5df, 
+0xdbdb4bdb9690ab4d, 0xa1a1bea1611f5fc0, 0x8d8d0e8d1c830791, 0x3d3df43df5c97ac8, 
+0x97976697ccf1335b, 0x0000000000000000, 0xcfcf1bcf36d483f9, 0x2b2bac2b4587566e, 
+0x7676c57697b3ece1, 0x8282328264b019e6, 0xd6d67fd6fea9b128, 0x1b1b6c1bd87736c3, 
+0xb5b5eeb5c15b7774, 0xafaf86af112943be, 0x6a6ab56a77dfd41d, 0x50505d50ba0da0ea, 
+0x45450945124c8a57, 0xf3f3ebf3cb18fb38, 0x3030c0309df060ad, 0xefef9bef2b74c3c4, 
+0x3f3ffc3fe5c37eda, 0x55554955921caac7, 0xa2a2b2a2791059db, 0xeaea8fea0365c9e9, 
+0x656589650fecca6a, 0xbabad2bab9686903, 0x2f2fbc2f65935e4a, 0xc0c027c04ee79d8e, 
+0xdede5fdebe81a160, 0x1c1c701ce06c38fc, 0xfdfdd3fdbb2ee746, 0x4d4d294d52649a1f, 
+0x92927292e4e03976, 0x7575c9758fbceafa, 0x06061806301e0c36, 0x8a8a128a249809ae, 
+0xb2b2f2b2f940794b, 0xe6e6bfe66359d185, 0x0e0e380e70361c7e, 0x1f1f7c1ff8633ee7, 
+0x6262956237f7c455, 0xd4d477d4eea3b53a, 0xa8a89aa829324d81, 0x96966296c4f43152, 
+0xf9f9c3f99b3aef62, 0xc5c533c566f697a3, 0x2525942535b14a10, 0x59597959f220b2ab, 
+0x84842a8454ae15d0, 0x7272d572b7a7e4c5, 0x3939e439d5dd72ec, 0x4c4c2d4c5a619816, 
+0x5e5e655eca3bbc94, 0x7878fd78e785f09f, 0x3838e038ddd870e5, 0x8c8c0a8c14860598, 
+0xd1d163d1c6b2bf17, 0xa5a5aea5410b57e4, 0xe2e2afe2434dd9a1, 0x616199612ff8c24e, 
+0xb3b3f6b3f1457b42, 0x2121842115a54234, 0x9c9c4a9c94d62508, 0x1e1e781ef0663cee, 
+0x4343114322528661, 0xc7c73bc776fc93b1, 0xfcfcd7fcb32be54f, 0x0404100420140824, 
+0x51515951b208a2e3, 0x99995e99bcc72f25, 0x6d6da96d4fc4da22, 0x0d0d340d68391a65, 
+0xfafacffa8335e979, 0xdfdf5bdfb684a369, 0x7e7ee57ed79bfca9, 0x242490243db44819, 
+0x3b3bec3bc5d776fe, 0xabab96ab313d4b9a, 0xcece1fce3ed181f0, 0x1111441188552299, 
+0x8f8f068f0c890383, 0x4e4e254e4a6b9c04, 0xb7b7e6b7d1517366, 0xebeb8beb0b60cbe0, 
+0x3c3cf03cfdcc78c1, 0x81813e817cbf1ffd, 0x94946a94d4fe3540, 0xf7f7fbf7eb0cf31c, 
+0xb9b9deb9a1676f18, 0x13134c13985f268b, 0x2c2cb02c7d9c5851, 0xd3d36bd3d6b8bb05, 
+0xe7e7bbe76b5cd38c, 0x6e6ea56e57cbdc39, 0xc4c437c46ef395aa, 0x03030c03180f061b, 
+0x565645568a13acdc, 0x44440d441a49885e, 0x7f7fe17fdf9efea0, 0xa9a99ea921374f88, 
+0x2a2aa82a4d825467, 0xbbbbd6bbb16d6b0a, 0xc1c123c146e29f87, 0x53535153a202a6f1, 
+0xdcdc57dcae8ba572, 0x0b0b2c0b58271653, 0x9d9d4e9d9cd32701, 0x6c6cad6c47c1d82b, 
+0x3131c43195f562a4, 0x7474cd7487b9e8f3, 0xf6f6fff6e309f115, 0x464605460a438c4c, 
+0xacac8aac092645a5, 0x89891e893c970fb5, 0x14145014a04428b4, 0xe1e1a3e15b42dfba, 
+0x16165816b04e2ca6, 0x3a3ae83acdd274f7, 0x6969b9696fd0d206, 0x09092409482d1241, 
+0x7070dd70a7ade0d7, 0xb6b6e2b6d954716f, 0xd0d067d0ceb7bd1e, 0xeded93ed3b7ec7d6, 
+0xcccc17cc2edb85e2, 0x424215422a578468, 0x98985a98b4c22d2c, 0xa4a4aaa4490e55ed, 
+0x2828a0285d885075, 0x5c5c6d5cda31b886, 0xf8f8c7f8933fed6b, 0x8686228644a411c2, 
+]
+C1 = [
+0xd818186018c07830, 0x2623238c2305af46, 0xb8c6c63fc67ef991, 0xfbe8e887e8136fcd, 
+0xcb878726874ca113, 0x11b8b8dab8a9626d, 0x0901010401080502, 0x0d4f4f214f426e9e, 
+0x9b3636d836adee6c, 0xffa6a6a2a6590451, 0x0cd2d26fd2debdb9, 0x0ef5f5f3f5fb06f7, 
+0x967979f979ef80f2, 0x306f6fa16f5fcede, 0x6d91917e91fcef3f, 0xf852525552aa07a4, 
+0x4760609d6027fdc0, 0x35bcbccabc897665, 0x379b9b569baccd2b, 0x8a8e8e028e048c01, 
+0xd2a3a3b6a371155b, 0x6c0c0c300c603c18, 0x847b7bf17bff8af6, 0x803535d435b5e16a, 
+0xf51d1d741de8693a, 0xb3e0e0a7e05347dd, 0x21d7d77bd7f6acb3, 0x9cc2c22fc25eed99, 
+0x432e2eb82e6d965c, 0x294b4b314b627a96, 0x5dfefedffea321e1, 0xd5575741578216ae, 
+0xbd15155415a8412a, 0xe87777c1779fb6ee, 0x923737dc37a5eb6e, 0x9ee5e5b3e57b56d7, 
+0x139f9f469f8cd923, 0x23f0f0e7f0d317fd, 0x204a4a354a6a7f94, 0x44dada4fda9e95a9, 
+0xa258587d58fa25b0, 0xcfc9c903c906ca8f, 0x7c2929a429558d52, 0x5a0a0a280a502214, 
+0x50b1b1feb1e14f7f, 0xc9a0a0baa0691a5d, 0x146b6bb16b7fdad6, 0xd985852e855cab17, 
+0x3cbdbdcebd817367, 0x8f5d5d695dd234ba, 0x9010104010805020, 0x07f4f4f7f4f303f5, 
+0xddcbcb0bcb16c08b, 0xd33e3ef83eedc67c, 0x2d0505140528110a, 0x78676781671fe6ce, 
+0x97e4e4b7e47353d5, 0x0227279c2725bb4e, 0x7341411941325882, 0xa78b8b168b2c9d0b, 
+0xf6a7a7a6a7510153, 0xb27d7de97dcf94fa, 0x4995956e95dcfb37, 0x56d8d847d88e9fad, 
+0x70fbfbcbfb8b30eb, 0xcdeeee9fee2371c1, 0xbb7c7ced7cc791f8, 0x716666856617e3cc, 
+0x7bdddd53dda68ea7, 0xaf17175c17b84b2e, 0x454747014702468e, 0x1a9e9e429e84dc21, 
+0xd4caca0fca1ec589, 0x582d2db42d75995a, 0x2ebfbfc6bf917963, 0x3f07071c07381b0e, 
+0xacadad8ead012347, 0xb05a5a755aea2fb4, 0xef838336836cb51b, 0xb63333cc3385ff66, 
+0x5c636391633ff2c6, 0x1202020802100a04, 0x93aaaa92aa393849, 0xde7171d971afa8e2, 
+0xc6c8c807c80ecf8d, 0xd119196419c87d32, 0x3b49493949727092, 0x5fd9d943d9869aaf, 
+0x31f2f2eff2c31df9, 0xa8e3e3abe34b48db, 0xb95b5b715be22ab6, 0xbc88881a8834920d, 
+0x3e9a9a529aa4c829, 0x0b262698262dbe4c, 0xbf3232c8328dfa64, 0x59b0b0fab0e94a7d, 
+0xf2e9e983e91b6acf, 0x770f0f3c0f78331e, 0x33d5d573d5e6a6b7, 0xf480803a8074ba1d, 
+0x27bebec2be997c61, 0xebcdcd13cd26de87, 0x893434d034bde468, 0x3248483d487a7590, 
+0x54ffffdbffab24e3, 0x8d7a7af57af78ff4, 0x6490907a90f4ea3d, 0x9d5f5f615fc23ebe, 
+0x3d202080201da040, 0x0f6868bd6867d5d0, 0xca1a1a681ad07234, 0xb7aeae82ae192c41, 
+0x7db4b4eab4c95e75, 0xce54544d549a19a8, 0x7f93937693ece53b, 0x2f222288220daa44, 
+0x6364648d6407e9c8, 0x2af1f1e3f1db12ff, 0xcc7373d173bfa2e6, 0x8212124812905a24, 
+0x7a40401d403a5d80, 0x4808082008402810, 0x95c3c32bc356e89b, 0xdfecec97ec337bc5, 
+0x4ddbdb4bdb9690ab, 0xc0a1a1bea1611f5f, 0x918d8d0e8d1c8307, 0xc83d3df43df5c97a, 
+0x5b97976697ccf133, 0x0000000000000000, 0xf9cfcf1bcf36d483, 0x6e2b2bac2b458756, 
+0xe17676c57697b3ec, 0xe68282328264b019, 0x28d6d67fd6fea9b1, 0xc31b1b6c1bd87736, 
+0x74b5b5eeb5c15b77, 0xbeafaf86af112943, 0x1d6a6ab56a77dfd4, 0xea50505d50ba0da0, 
+0x5745450945124c8a, 0x38f3f3ebf3cb18fb, 0xad3030c0309df060, 0xc4efef9bef2b74c3, 
+0xda3f3ffc3fe5c37e, 0xc755554955921caa, 0xdba2a2b2a2791059, 0xe9eaea8fea0365c9, 
+0x6a656589650fecca, 0x03babad2bab96869, 0x4a2f2fbc2f65935e, 0x8ec0c027c04ee79d, 
+0x60dede5fdebe81a1, 0xfc1c1c701ce06c38, 0x46fdfdd3fdbb2ee7, 0x1f4d4d294d52649a, 
+0x7692927292e4e039, 0xfa7575c9758fbcea, 0x3606061806301e0c, 0xae8a8a128a249809, 
+0x4bb2b2f2b2f94079, 0x85e6e6bfe66359d1, 0x7e0e0e380e70361c, 0xe71f1f7c1ff8633e, 
+0x556262956237f7c4, 0x3ad4d477d4eea3b5, 0x81a8a89aa829324d, 0x5296966296c4f431, 
+0x62f9f9c3f99b3aef, 0xa3c5c533c566f697, 0x102525942535b14a, 0xab59597959f220b2, 
+0xd084842a8454ae15, 0xc57272d572b7a7e4, 0xec3939e439d5dd72, 0x164c4c2d4c5a6198, 
+0x945e5e655eca3bbc, 0x9f7878fd78e785f0, 0xe53838e038ddd870, 0x988c8c0a8c148605, 
+0x17d1d163d1c6b2bf, 0xe4a5a5aea5410b57, 0xa1e2e2afe2434dd9, 0x4e616199612ff8c2, 
+0x42b3b3f6b3f1457b, 0x342121842115a542, 0x089c9c4a9c94d625, 0xee1e1e781ef0663c, 
+0x6143431143225286, 0xb1c7c73bc776fc93, 0x4ffcfcd7fcb32be5, 0x2404041004201408, 
+0xe351515951b208a2, 0x2599995e99bcc72f, 0x226d6da96d4fc4da, 0x650d0d340d68391a, 
+0x79fafacffa8335e9, 0x69dfdf5bdfb684a3, 0xa97e7ee57ed79bfc, 0x19242490243db448, 
+0xfe3b3bec3bc5d776, 0x9aabab96ab313d4b, 0xf0cece1fce3ed181, 0x9911114411885522, 
+0x838f8f068f0c8903, 0x044e4e254e4a6b9c, 0x66b7b7e6b7d15173, 0xe0ebeb8beb0b60cb, 
+0xc13c3cf03cfdcc78, 0xfd81813e817cbf1f, 0x4094946a94d4fe35, 0x1cf7f7fbf7eb0cf3, 
+0x18b9b9deb9a1676f, 0x8b13134c13985f26, 0x512c2cb02c7d9c58, 0x05d3d36bd3d6b8bb, 
+0x8ce7e7bbe76b5cd3, 0x396e6ea56e57cbdc, 0xaac4c437c46ef395, 0x1b03030c03180f06, 
+0xdc565645568a13ac, 0x5e44440d441a4988, 0xa07f7fe17fdf9efe, 0x88a9a99ea921374f, 
+0x672a2aa82a4d8254, 0x0abbbbd6bbb16d6b, 0x87c1c123c146e29f, 0xf153535153a202a6, 
+0x72dcdc57dcae8ba5, 0x530b0b2c0b582716, 0x019d9d4e9d9cd327, 0x2b6c6cad6c47c1d8, 
+0xa43131c43195f562, 0xf37474cd7487b9e8, 0x15f6f6fff6e309f1, 0x4c464605460a438c, 
+0xa5acac8aac092645, 0xb589891e893c970f, 0xb414145014a04428, 0xbae1e1a3e15b42df, 
+0xa616165816b04e2c, 0xf73a3ae83acdd274, 0x066969b9696fd0d2, 0x4109092409482d12, 
+0xd77070dd70a7ade0, 0x6fb6b6e2b6d95471, 0x1ed0d067d0ceb7bd, 0xd6eded93ed3b7ec7, 
+0xe2cccc17cc2edb85, 0x68424215422a5784, 0x2c98985a98b4c22d, 0xeda4a4aaa4490e55, 
+0x752828a0285d8850, 0x865c5c6d5cda31b8, 0x6bf8f8c7f8933fed, 0xc28686228644a411, 
+]
+C2 = [
+0x30d818186018c078, 0x462623238c2305af, 0x91b8c6c63fc67ef9, 0xcdfbe8e887e8136f, 
+0x13cb878726874ca1, 0x6d11b8b8dab8a962, 0x0209010104010805, 0x9e0d4f4f214f426e, 
+0x6c9b3636d836adee, 0x51ffa6a6a2a65904, 0xb90cd2d26fd2debd, 0xf70ef5f5f3f5fb06, 
+0xf2967979f979ef80, 0xde306f6fa16f5fce, 0x3f6d91917e91fcef, 0xa4f852525552aa07, 
+0xc04760609d6027fd, 0x6535bcbccabc8976, 0x2b379b9b569baccd, 0x018a8e8e028e048c, 
+0x5bd2a3a3b6a37115, 0x186c0c0c300c603c, 0xf6847b7bf17bff8a, 0x6a803535d435b5e1, 
+0x3af51d1d741de869, 0xddb3e0e0a7e05347, 0xb321d7d77bd7f6ac, 0x999cc2c22fc25eed, 
+0x5c432e2eb82e6d96, 0x96294b4b314b627a, 0xe15dfefedffea321, 0xaed5575741578216, 
+0x2abd15155415a841, 0xeee87777c1779fb6, 0x6e923737dc37a5eb, 0xd79ee5e5b3e57b56, 
+0x23139f9f469f8cd9, 0xfd23f0f0e7f0d317, 0x94204a4a354a6a7f, 0xa944dada4fda9e95, 
+0xb0a258587d58fa25, 0x8fcfc9c903c906ca, 0x527c2929a429558d, 0x145a0a0a280a5022, 
+0x7f50b1b1feb1e14f, 0x5dc9a0a0baa0691a, 0xd6146b6bb16b7fda, 0x17d985852e855cab, 
+0x673cbdbdcebd8173, 0xba8f5d5d695dd234, 0x2090101040108050, 0xf507f4f4f7f4f303, 
+0x8bddcbcb0bcb16c0, 0x7cd33e3ef83eedc6, 0x0a2d050514052811, 0xce78676781671fe6, 
+0xd597e4e4b7e47353, 0x4e0227279c2725bb, 0x8273414119413258, 0x0ba78b8b168b2c9d, 
+0x53f6a7a7a6a75101, 0xfab27d7de97dcf94, 0x374995956e95dcfb, 0xad56d8d847d88e9f, 
+0xeb70fbfbcbfb8b30, 0xc1cdeeee9fee2371, 0xf8bb7c7ced7cc791, 0xcc716666856617e3, 
+0xa77bdddd53dda68e, 0x2eaf17175c17b84b, 0x8e45474701470246, 0x211a9e9e429e84dc, 
+0x89d4caca0fca1ec5, 0x5a582d2db42d7599, 0x632ebfbfc6bf9179, 0x0e3f07071c07381b, 
+0x47acadad8ead0123, 0xb4b05a5a755aea2f, 0x1bef838336836cb5, 0x66b63333cc3385ff, 
+0xc65c636391633ff2, 0x041202020802100a, 0x4993aaaa92aa3938, 0xe2de7171d971afa8, 
+0x8dc6c8c807c80ecf, 0x32d119196419c87d, 0x923b494939497270, 0xaf5fd9d943d9869a, 
+0xf931f2f2eff2c31d, 0xdba8e3e3abe34b48, 0xb6b95b5b715be22a, 0x0dbc88881a883492, 
+0x293e9a9a529aa4c8, 0x4c0b262698262dbe, 0x64bf3232c8328dfa, 0x7d59b0b0fab0e94a, 
+0xcff2e9e983e91b6a, 0x1e770f0f3c0f7833, 0xb733d5d573d5e6a6, 0x1df480803a8074ba, 
+0x6127bebec2be997c, 0x87ebcdcd13cd26de, 0x68893434d034bde4, 0x903248483d487a75, 
+0xe354ffffdbffab24, 0xf48d7a7af57af78f, 0x3d6490907a90f4ea, 0xbe9d5f5f615fc23e, 
+0x403d202080201da0, 0xd00f6868bd6867d5, 0x34ca1a1a681ad072, 0x41b7aeae82ae192c, 
+0x757db4b4eab4c95e, 0xa8ce54544d549a19, 0x3b7f93937693ece5, 0x442f222288220daa, 
+0xc86364648d6407e9, 0xff2af1f1e3f1db12, 0xe6cc7373d173bfa2, 0x248212124812905a, 
+0x807a40401d403a5d, 0x1048080820084028, 0x9b95c3c32bc356e8, 0xc5dfecec97ec337b, 
+0xab4ddbdb4bdb9690, 0x5fc0a1a1bea1611f, 0x07918d8d0e8d1c83, 0x7ac83d3df43df5c9, 
+0x335b97976697ccf1, 0x0000000000000000, 0x83f9cfcf1bcf36d4, 0x566e2b2bac2b4587, 
+0xece17676c57697b3, 0x19e68282328264b0, 0xb128d6d67fd6fea9, 0x36c31b1b6c1bd877, 
+0x7774b5b5eeb5c15b, 0x43beafaf86af1129, 0xd41d6a6ab56a77df, 0xa0ea50505d50ba0d, 
+0x8a5745450945124c, 0xfb38f3f3ebf3cb18, 0x60ad3030c0309df0, 0xc3c4efef9bef2b74, 
+0x7eda3f3ffc3fe5c3, 0xaac755554955921c, 0x59dba2a2b2a27910, 0xc9e9eaea8fea0365, 
+0xca6a656589650fec, 0x6903babad2bab968, 0x5e4a2f2fbc2f6593, 0x9d8ec0c027c04ee7, 
+0xa160dede5fdebe81, 0x38fc1c1c701ce06c, 0xe746fdfdd3fdbb2e, 0x9a1f4d4d294d5264, 
+0x397692927292e4e0, 0xeafa7575c9758fbc, 0x0c3606061806301e, 0x09ae8a8a128a2498, 
+0x794bb2b2f2b2f940, 0xd185e6e6bfe66359, 0x1c7e0e0e380e7036, 0x3ee71f1f7c1ff863, 
+0xc4556262956237f7, 0xb53ad4d477d4eea3, 0x4d81a8a89aa82932, 0x315296966296c4f4, 
+0xef62f9f9c3f99b3a, 0x97a3c5c533c566f6, 0x4a102525942535b1, 0xb2ab59597959f220, 
+0x15d084842a8454ae, 0xe4c57272d572b7a7, 0x72ec3939e439d5dd, 0x98164c4c2d4c5a61, 
+0xbc945e5e655eca3b, 0xf09f7878fd78e785, 0x70e53838e038ddd8, 0x05988c8c0a8c1486, 
+0xbf17d1d163d1c6b2, 0x57e4a5a5aea5410b, 0xd9a1e2e2afe2434d, 0xc24e616199612ff8, 
+0x7b42b3b3f6b3f145, 0x42342121842115a5, 0x25089c9c4a9c94d6, 0x3cee1e1e781ef066, 
+0x8661434311432252, 0x93b1c7c73bc776fc, 0xe54ffcfcd7fcb32b, 0x0824040410042014, 
+0xa2e351515951b208, 0x2f2599995e99bcc7, 0xda226d6da96d4fc4, 0x1a650d0d340d6839, 
+0xe979fafacffa8335, 0xa369dfdf5bdfb684, 0xfca97e7ee57ed79b, 0x4819242490243db4, 
+0x76fe3b3bec3bc5d7, 0x4b9aabab96ab313d, 0x81f0cece1fce3ed1, 0x2299111144118855, 
+0x03838f8f068f0c89, 0x9c044e4e254e4a6b, 0x7366b7b7e6b7d151, 0xcbe0ebeb8beb0b60, 
+0x78c13c3cf03cfdcc, 0x1ffd81813e817cbf, 0x354094946a94d4fe, 0xf31cf7f7fbf7eb0c,