基於ssh協議傳輸文件例子

使用python模塊:paramiko,optparsepython

__author__ = 'Administrator'import paramikoimport sysfrom optparse import OptionParserimport osdef exit_code(code):    os.system('pause')    sys.exit(code)def uploadfile(localfile,ip):    if not os.path.exists(localfile):        print '%s not exists!' % localfile        exit_code(10)    root_path = '/root/'    remotefile = os.path.basename(localfile)    t = paramiko.Transport((ip,22))    t.connect(username='root',password='yunwei')    sftp = paramiko.SFTPClient.from_transport(t)    sftp.put(localfile,'%s%s'%(root_path,remotefile))    t.close()def main():    usage = "usage: %prog [options] arg"    parser = OptionParser(usage)    parser.add_option("-f", "--file", dest="file",                      help="file path for transport")    parser.add_option("-v", "--verbose",                      action="store_true", dest="verbose")    parser.add_option("-q", "--quiet",                      action="store_false", dest="verbose")    parser.add_option("-H", "--Host", dest='ip',help="the ip address of remote host")    (options,args) = parser.parse_args()    if options.verbose:        print "reading %s..." % options.file    if not options.file or not options.ip:        parser.error("incorrect number of argument!")        exit_code(10)    uploadfile(options.file,options.ip)if __name__ == "__main__":    main()
相關文章
相關標籤/搜索