1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
| import paramiko import datetime import os import ConfigParser import codecs
hostname = "" port = 22 username = "" password = "" REMOTE_PATH = "" LOCAL_PATH = ""
def loadConfig(): global hostname global port global username global password global REMOTE_PATH global LOCAL_PATH
f = codecs.open( "config.ini" , "r" ,encoding='UTF-8'); config = ConfigParser.ConfigParser() config.readfp(f) hostname = config.get("global","hostname") port = int(config.get("global","port")) username = config.get("global","username") password = config.get("global","password") REMOTE_PATH = config.get("global","REMOTE_PATH") LOCAL_PATH = config.get("global","LOCAL_PATH")
def upload(_localDir, _remoteDir): _localDir = LOCAL_PATH _remoteDir = LOCAL_PATH try: t = paramiko.Transport((hostname, port)) t.connect(username=username, password=password) sftp = paramiko.SFTPClient.from_transport(t) print 'upload file start %s ' % datetime.datetime.now() for root, dirs, files in os.walk(_localDir): remoteRoot = root.replace("\\", "/") for filespath in files: local_file = os.path.join(root, filespath) remote_file = REMOTE_PATH + "/" +remoteRoot + "/" + filespath remote_file = remote_file.replace("//", "/") try: sftp.put(local_file, remote_file) except Exception, e: sftp.mkdir(os.path.split(remote_file)[0]) sftp.put(local_file, remote_file) print "upload %s to remote %s" % (local_file, remote_file) for name in dirs: remoteDir = _remoteDir +"/"+ remoteRoot +"/" + name remoteDir = remoteDir.replace("//", "/") print("remoteDir ", remoteDir) try: sftp.mkdir(remoteDir) print "mkdir path %s" % remoteDir except Exception, e: pass print 'upload file success %s ' % datetime.datetime.now() t.close() except Exception, e: print e
loadConfig() upload(LOCAL_PATH, REMOTE_PATH)
ssh = paramiko.SSHClient() ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) ssh.connect(hostname, 22, username, password) stdin, stdout, stderr = ssh.exec_command("du -ah " + REMOTE_PATH) print stdout.readlines() ssh.close()
|