這是我應用我網站上寫的【FTP Upload 模組】與 【偵測Port 模組】寫成的一支程式
主要是,從外部讀config 檔進來,此config 檔記錄了我要FTP哪些內容到哪一個站
(1)首先會偵測該FTP Site 通不通
(2)檢查要傳檔的我方目錄群是否為空,空目錄的話當燃就不傳囉!
(3)可以定多個目錄,程式會動把目錄內容一一UPLOAD到對方的網站
(4)最後把過程的紀錄在Log ,並且萬一有狀況會把Log Mail to 管理者



我的config 檔叫ftpauto.conf  , 內容如下
[target]
company=company1   # 對方公司名稱
ftp=192.168.1.10        # 對方的FTP IP
acc=comuser1             # 對方的FTP帳號
pas=pasxxx                 # 對方的FTP password
map=/dir1,/dir2,/dir3   # 依序要傳到對方的目錄群 (可以定一個或多個,但需與我方的目錄數一致)


[local]
acc=alex                      # 我方FTP Site 的帳號
acc_home=/users        # 我方的HOME 目錄 
map=/users/report1,
/users/report2,/users/report3  # 我方要依序傳出去的目錄群 (可以定一個或多個,但需與對方的目錄數一致)
adm=alex@xxx.com.tw # 這是我方管理者的EMAIL
logdir=/users/ftplog      #  LOG路徑
script_path=/script/ftp  #  本程式路徑


以下為script
( 引用者請註明本出處)

#!/usr/bin/env python

import subprocess
import string
import sys
import linecache
import cmd
import bdb
import repr
import os
import posix
import time
import socket
import re
import ALEX_FTP_M
import ALEX_CK_PT_M


SENDMAIL = "/usr/bin/mailx" # sendmail location

from ConfigParser import SafeConfigParser
parser = SafeConfigParser()
parser.read('ftpauto.conf')  # Read config 檔

company=parser.get('target','company')
targe_ftp=parser.get('target','ftp')
targe_acc=parser.get('target','acc')
targe_pas=parser.get('target','pas')
target_map_tmp=parser.get('target','map')
target_map=target_map_tmp.split(",")

acc=parser.get('local','acc')
acc_home=parser.get('local','acc_home')
local_src_tmp=parser.get('local','map')
local_src=local_src_tmp.split(",")
adm=parser.get('local','adm')
logdirtmp=parser.get('local','logdir')
scrpath=parser.get('local','script_path')

os.chdir('%s' % scrpath)

DATE=time.strftime("%Y%m%d-%H%M")
tempfilename="temp"+company
TIMESTART=time.strftime("%Y%m%d-%H%M")

log=logdirtmp+"/log.%s" % DATE

print log

def f1(dirpath):  # 檢查目錄是否為空
        dircontent=os.listdir(dirpath)
        try:
                dircontent[0]
                return "1"
        except IndexError:
                return "0"



li = ALEX_CK_PT_M.do(targe_ftp,21)        # 呼叫檢查PORT模組
print li

if li == True:
 

    k=0

    for j in local_src:
        print j   
        if os.path.isfile("temp"+company):
            os.remove("temp"+company)
       


        chk_empty_res=f1(j)

        l=open(log ,'a')
        TIMESTART=time.strftime("%Y%m%d-%H:%M:%S")

        if chk_empty_res=="1":
           
            l.write("%s - %s --> ftp is Starting \n" % (TIMESTART,j))   
            # 呼叫FTP 模組
            ALEX_FTP_M.tr(j,company,targe_acc,targe_pas,target_map[k],targe_ftp,tempfilename) 
            TIMEEND=time.strftime("%Y%m%d-%H:%M:%S")
            subprocess.call("rm %s/*" % j,shell=True)   

            l.write("%s - %s --> ftp is Completed \n" % (TIMEEND,j))
        else:
            print  "%s is empty" % j
            l.write("%s - %s --> No files ,FTP Abort \n" % (TIMESTART,j))

       
        k=k+1   


else:
    print "host is no response"   
    p = os.popen("%s -t" % SENDMAIL, "w")
    p.write("To: %s \n" % adm )
    p.write("Subject:%s FTP Site Not response \n" % company )
    p.write("\n") # blank line separating headers from body
    p.write("No response from %s Ftp site  ,ftp abort !\n" % company)
    sts = p.close()
    if sts != 0:
              print "Sendmail exit status", sts

arrow
arrow
    全站熱搜

    fevin 發表在 痞客邦 留言(0) 人氣()