Differences

This shows you the differences between two versions of the page.

Link to this comparison view

withsftp [2014/10/26 01:52] (current)
Line 1: Line 1:
 +==== An example which sftp's an entire directory ====
 +<code lang="​python">​
 +#​!/​usr/​bin/​python
 +
 +import os
 +import sys
 +import pprint
 +import re
 +import tempfile
 +import logging
 +
 +__logger = logging.getLogger("​asset_mgmt"​)
 +__logger.setLevel(logging.DEBUG)
 +# create log filehandler
 +fh = logging.FileHandler("​am.log"​)
 +fh.setLevel(logging.DEBUG)
 +# create formatter
 +formatter = logging.Formatter("​%(asctime)s - %(name)s - %(levelname)s - %(message)s"​)
 +# add formatter to ch and fh
 +fh.setFormatter(formatter)
 +# add ch and fh to logger
 +__logger.addHandler(fh)
 +
 +__am_path = '/​stage/​asset_management/'​
 +__dest_path = '/​home/​oracle/​asset_management'​
 +
 +def sftp(fil):
 +        rcFtp,rcRm = 0,0
 +        tt = tempfile.mkstemp('','','/​tmp',​1)
 +        fh = open(tt[1],"​w"​)
 +        fh.write('​lcd '​+__am_path+'​\n'​)
 +        fh.write('​cd '​+__dest_path+'​\n'​)
 +        fh.write('​put '​+fil+'​\n'​)
 +        fh.write('​exit'​+'​\n'​)
 +        cmd = "/​usr/​bin/​sftp -b "​+tt[1] + " oracle@dc4200dbs11"​
 +        __logger.debug(cmd)
 +        rcFtp = os.system(cmd)
 +        try:
 +                rcRm = os.remove(fil)
 +        except:
 +                pass
 +        __logger.debug("​sftp result : "​+str(rcFtp)+ " rm result : "​+str(rcRm))
 +
 +def main():
 +        am_path = __am_path
 +        try:
 +                fileList = os.listdir(am_path);​
 +        except:
 +                print "​Exception"​
 +                os.exit(1)
 +        sftpList = []
 +        # print fileList
 +        for f in fileList:
 +                myRe = re.compile(r'​\.'​);​
 +                name = myRe.split(f,​1);​
 +                nn = name[0] + "​.md5";​
 +                cmd = '/​usr/​bin/​md5sum '​+am_path+f+'​ > '​+am_path+nn;​
 +                #print cmd
 +                rc = os.system(cmd)
 +                sftpList.append(am_path+f)
 +                sftpList.append(am_path+nn)
 +
 +        for f in sftpList:
 +                sftp(f)
 +
 +main()
 +</​code>​
 +
 +----
 +  * [[pythoninfo|Back to Python]]
  
withsftp.txt ยท Last modified: 2014/10/26 01:52 (external edit)
CC Attribution-Share Alike 3.0 Unported
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0