python使用pexpect实现与ftp的自动交互

转载请注明出处:http://www.cnblogs.com/blazer/p/5318516.html

环境:centos6.5 python2.6.6

依赖包:pexpect-4.0.1, ptyprocess-0.5.1

 

由于sftp需要手动输入密码,然后再get文件,如此繁琐的任务还是交给机器去做吧!

 

import pexpect, sys, os, datetime, subprocess

test=False

print ' '
print '=====   begin : ' +  datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
# command info
home = '/home/www/'
port = '22222'
host = 'data@x.x.x.x'
timeout = 1 * 60 * 10 # seconds

# file name
now_yyyyMMddHH = datetime.datetime.now().strftime('%y%m%d%H')
file_name = 'data' + now_yyyyMMddHH + '0001.zip'
md5_name = file_name + '.md5'
md5_local = file_name + '.md5.local'

# download path
download_path = '/home/www/tmp_jinfu/'
target_path = '/home/www/temp_jinfu/'
if not os.path.isdir(download_path) :
    os.makedirs(download_path)
if not os.path.isdir(target_path) :
    os.makedirs(target_path)
# check md5 file if exists
if not test and os.path.exists(download_path + md5_name) :
    print 'file ' + download_path + md5_name + ' is exists...'
    print 'download is exit !!!'
    print '=====   end   : '  +  datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
    exit(0)

print 'download ' + download_path + md5_name + ' start ...'
child = pexpect.spawn(command = '/usr/bin/sftp -oPort=%s -oIdentityFile=key_sftp %s' % (port, host), cwd = home)
child.expect("Enter passphrase for key", timeout = timeout)
child.sendline('hengda2016')
child.sendline('get full/%s %s' % (md5_name, download_path))
i = child.expect(['100%', 'not found'], timeout = timeout)
child.sendline('exit')
# check download , and md5 file if exists
if i == 1 or not os.path.exists(download_path + md5_name) :
    print 'download full/' + md5_name + ' is fail !!! file not found.'
    print '=====   end   : '  +  datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
    exit(-1)
print 'download ' + download_path + md5_name + ' over  ...'

print 'download ' + download_path + file_name + ' start ...'
child = pexpect.spawn(command = '/usr/bin/sftp -oPort=%s -oIdentityFile=key_sftp %s' % (port, host), cwd = home)
child.expect("Enter passphrase for key", timeout = timeout)
child.sendline('hengda2016')
child.sendline('get full/%s %s' % (file_name, download_path))
i = child.expect(['100%', 'not found'], timeout = timeout)
child.sendline('exit')
if i == 1 :
    print 'download full/' + file_name + ' is fail !!! file not found.'
    print '=====   end   : '  +  datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
    exit(-1)
print 'download ' + download_path + file_name + ' over  ...'

# check md5sum spawn
s = subprocess.call('md5sum %s | cut -d \' \' -f1 > %s' % (file_name, md5_local), shell = True, cwd = download_path)
if s != 0 :
    print 'md5sum is fail !!!'

md5_name_text = open(download_path + md5_name).read()
md5_local_text = open(download_path + md5_local).read()
# equals ftp md5 and local md5 by zip file
if md5_name_text == md5_local_text :
    print 'md5 == md5.local ... download ' + file_name + ' is sucess !!!'
    s = subprocess.call('mv %s %s' % (download_path + file_name, target_path), shell = True, cwd = download_path)
    if s == 0 : 
        print 'mv %s %s' % (download_path + file_name, target_path) + ' is success !!!'
    else :
        print 'mv %s %s' % (download_path + file_name, target_path) + ' is fail !!!'
else :
    print 'md5 != md5.local ... download ' + file_name + ' is fail !!! please you check the file !!! !!! !!!'

print '=====   end   : '  +  datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')

 

说明:

-oIdentityFile=key_sftp这个是密钥key文件  

 

posted @ 2016-03-25 10:38  BlazerHe  阅读(910)  评论(0编辑  收藏  举报