3.09.2015 г.

Python copy folder from USB to map drive in windows machine

#! --*-- coding:utf-8 --*--

###################################
#    Uses for copy folder from,    #
#    USB drive to map used device  #
#                                  #
#    Do not edit by hand           #
###################################    

#Imort requeare module's
import os,subprocess,shutil,time,sys

#General param is here
remote_ip = 'ip'                    #ip for server
remote_share_folder = 'share'        #share folder from server
remote_user = 'Administrator'        #Username for server
remote_pass = 'pass'                #password name for server
local_folder = r'c:\test'            #local folder where looking for dirname
mapping_char = 'G:'                    #charackter for maping

date_now = str(time.ctime()).split()[2]+str(time.ctime()).split()[1]+str(time.ctime()).split()[-1]

#--------------------Begin Function ----------------------------------#

#first function


def copyDirectory(src, dest):
    """
    used shutil.copytree for copy from src to dest
    if have somthing wrong like folder exists copy stop
    and script close
    """
    try:
        shutil.copytree(src, dest)
        print "Success Copied folder!"
        #print (src,dest)
    # Directories are the same
    except:
        print "Somthing wrong with copy Bye!"
        sys.exit(1)

#second function

def clear_folder(local_folder):
    """
    rmtree delete folder even folder is not empty
    then makedirs recovers folder
    """
    shutil.rmtree(local_folder)
    os.makedirs(local_folder)

#third function
def list_usb_flash():
    """
    chdir use to goto in usb drive, then list drive for file
    for eny file give full path and parse to first function
    finaly up one step and begin second function
    """
    os.chdir(local_folder)
    for root,dirname,filename in os.walk(local_folder):
        for f in dirname:
            fol = os.path.join(root,f)
            copyDirectory(fol, (mapping_char+'\\'+f+"-"+date_now))
    os.chdir(r'c:\\')
    clear_folder(local_folder)
                
                

#---------------End function's------------------------------------------#    

if os.path.isdir(local_folder):
    print "Folder exists now begin..."
    if os.path.isdir(mapping_char):
        list_usb_flash()
        
    else:
        maping = subprocess.Popen('net use %s \\\\%s\%s /user:%s %s' % (mapping_char,remote_ip,remote_share_folder,remote_user,remote_pass),shell=True, stdout=subprocess.PIPE)
        list_usb_flash()
        
else:
    print "USB Driver maybe not mounted!!!"

.

31.07.2015 г.

Search for ip and resolv with hostname for enybody file

#!/bin/bash

#Path to file with ip-s
FILE="/home/ips.txt"
#Command to resolv ip- with hostname
DIG="dig +short -x "
#Search for ip in file SEARCH=`cat $FILE | egrep -Eo '((1?[0-9][0-9]?|2[0-4][0-9]|25[0-5])\.){3}(1?[0-9][0-9]?|2[0-4][0-9]|25[0-5])' | sort -t . -k 1,1n -k 2,2n -k3,3n -k4,4n`

#For clasuse
for i in $SEARCH;do
    echo -ne "$i\t-\t" "\e[1;33m `$DIG$i`\e[0m" "\n"
done

22.07.2015 г.

Python backup firebird base

 import os,shutil, subprocess  
 from datetime import datetime  
 file_destination = ["/home/borko/base/firebird.fdb"]  
 #Must change path to folder if user not called "borko"  
 copy_destination = "/home/borko/base/archive/"  
 #Full path to gbak  
 gback_file = "/opt/firebird/bin/gbak"  
 #Get time and date to add into archive  
 date = str(datetime.now())  
 datetime = date.replace(' ','')  
 count=0  
 #Input how much file save in folder!!!!  
 save_file = 2  
 #Set params for Gbak  
 server='127.0.0.1'  
 user = "SYSDBA"  
 password = "masterkey"  
 #List with older file which must delete  
 list_file_delete = []  
 #Function check for problems  
 def check_file_and_filder(folder,name):  
     if not os.path.isdir(folder):  
         try:  
             os.makedirs(folder)  
         except:  
             pass  
     for file_search in name:  
         if not os.path.isfile(file_search):  
             print "File not exists"  
 #Function calculate file in folder  
 def calculate():  
     count = 0  
     del_count=1  
     for i in file_destination:  
         search_name = i.split("/")[-1]  
         for search in os.listdir(copy_destination):  
             if search.startswith(search_name):  
                 list_file_delete.append(os.path.join(copy_destination,search))  
                 count+=1  
         if count > save_file:  
             print "Poveche sa s %d" % (count-save_file)  
             how_much = (count-save_file)  
             new_list_for_delete = {i:float(os.stat(i).st_mtime) for i in list_file_delete}  
             elem_to_delete = dict(list(sorted(new_list_for_delete.items(), key=lambda x: x[1]))[:(count-save_file)])  
             for del_elem in elem_to_delete.keys():  
                 os.remove(del_elem)  
 #Function mace backup  
 def backup_resurse():  
     for i in file_destination:  
         backup = subprocess.Popen('''{0} -v -t -user {1} -password {2} {3}:{4} {5}{6}.fbk'''  
         .format(gback_file,user,password,server,i,copy_destination, i.split("/")[-1]+datetime),shell=True)  
 #Start the magic  
 if __name__ == "__main__":  
     check_file_and_filder(copy_destination,file_destination)  
     calculate()  
     backup_resurse()  

2.07.2015 г.

Find Picture in Folder and print only name of them

for i in `find /home/name/Image -type f -name "*.jpg"`;\
do n=`echo $i | awk -F"/" '{print $NF}'`\
| echo ${n//.*}; done




for i in `find /home/name/Image -type f -name "*.jpg"` # Found some picture's
n=`echo $i | awk -F"/" '{print $NF}' #From the top row print only last name of the result
echo ${n//.*} #Remove .avi from file