#!/usr/bin/python import sys import time import datetime import glob import os import subprocess import commands import MySQLdb import sys, getopt from os import path path = '/opt/sensiplicity' def getVarFromFile(filename): import imp f = open(filename) global data data = imp.load_source('data', '', f) f.close() getVarFromFile(path + '/etc/SS-L1.conf') db = MySQLdb.connect(data.servername,data.username,data.password,data.dbname) cursor = db.cursor() def usb_status(): cursor.execute("SELECT value FROM sensors_system WHERE name = 'usb_state'") row = cursor.fetchone() if row: value = row[0] return value usb_state = usb_status() def folder_status(): cursor.execute("SELECT value FROM sensors_system WHERE name = 'usb_folder'") row = cursor.fetchone() if row: value = row[0] return value usb_folder = folder_status() usb_mount = "/usb" if not os.path.exists(usb_mount): commands.getoutput("/usr/bin/sudo mkdir -p "+usb_mount) def main(argv): inputfile = '' outputfile = '' try: opts, args = getopt.getopt(argv,"hi:o:",["ifile=","ofile="]) except getopt.GetoptError: print 'remote_usb.py -i -o ' sys.exit(2) for opt, arg in opts: if opt == '-h': print 'remote_usb.py -i -o ' sys.exit() elif opt in ("-i", "--ifile"): inputfile = arg elif opt in ("-o", "--ofile"): outputfile = arg if (usb_state == "enabled"): usb_dev = commands.getoutput("dmesg | grep sda | grep -v ':0:0:' | grep -v unmounted | sed 's/://g' | tail -1 | awk '{print $NF}'") print "usb_dev = "+usb_dev commands.getoutput("/usr/bin/sudo /bin/mount /dev/"+usb_dev+" /usb") on_usb = "/usb/"+usb_folder if not os.path.exists(on_usb): #os.mkdir(on_usb) commands.getoutput("/usr/bin/sudo mkdir -p "+on_usb) commands.getoutput("/usr/bin/sudo rsync -av "+inputfile+" "+on_usb) #listing = commands.getoutput("ls "+on_usb) commands.getoutput("/usr/bin/sudo /bin/umount /usb") cursor.close () db.close() commands.getoutput("/bin/rm ./c") sys.exit() if __name__ == "__main__": main(sys.argv[1:])