SFTP based on key multi-process batch concurrent upload file implementation case (Python)

Source: Internet
Author: User

Recent research on Paramiko,paramiko can be very convenient to implement the SSH protocol-based remote connection, which can be based on

Password, you can also have no password RSA asymmetric key, can also be implemented SFTP upload download operation, security and efficiency, combined with many

Process or multithreading can be implemented parallel processing, write a simple sftp based on key multi-process batch concurrent upload

File implementation case, for future reference.


Installing cryptographic modules and Paramiko modules

Download: pycrypto-2.6.1.tar.gz

Paramiko-1.10.1.tar.gz

Installation:

TAR-ZXF pycrypto-2.6.1.tar.gz

Python setup.py Build

Python setup.py Install


Based on RSA asymmetric passwords (public and private), you need to create a key on the server and distribute the public key to the client

Create RSA key pair:ssh-keygen-t RSA

Copy the private key to the client:Ssh-copy-id [email protected]


The code is as follows:

#!/usr/bin/env python#coding:utf-8import paramiko,sys,osfrom multiprocessing import  Pooldef sftp_put (ip,port,user,locdir,rmtdir):    pravie_key_path =  '/root/. Ssh/id_rsa '     key = paramiko. Rsakey.from_private_key_file (Pravie_key_path)     t = paramiko. Transport ((Ip,port))     t.connect (Username=user,pkey=key)     sftp  = paramiko. Sftpclient.from_transport (t)     sftp.put (LOCDIR, RMTDIR)         t.close () def read_file ():    global list     list=[]    with open (' userinfo.txt ', ' RB ')  as f:         for line in f:             list.append (line) def print_ip ():      global dic    dic={}    for i in range (Len ( List):         lip=list[i].split () [0]         dic[i]=list[i]        print  '%d '   %s '% (i,lip)     def judge ():    global iplist     iplist=[]    while true:        inp=raw _input (' \033[34;1m Please enter serial number to select the host to upload, enter OK to execute: \033[0m '). Strip ()         if  inp.isdigit ()  and int (INP)  in dic.keys ():             iplist.append (int (INP))         elif  inp ==  ' OK ':             create_ Process ()             break         elif inp ==  ' exit ':             Sys.exit ()         else:             print  ' \033[31;1m input error, please re-enter \033[0m '                     def create_process ():     pool=pool (processes=4)     iplistyz=tuple (set (IPList))      while true:            locdir=raw_input (' Please enter local file path: ')         if os.path.exists (locdir):             break         else:            print  ' \033[33;1m local file does not exist, please re-enter!\033[0m '      rmtdir=raw_input (' \033[34;1m Please enter remote file path: \033[0m ')     print  ' \033[33; 1m selected host as follows: \033[0m '     for i in IPListyz:         ip=dic[i].split () [0]        port=dic[i].split () [1]         user=dic[i].split () [2]         print ip        pool.apply_async (Sftp_put, (IP,int ( Port), User,locdir,rmtdir,)     pool.close ()     pool.join ()      if __name__ ==  ' __main__ ':     read_file ()      print_ip ()     judge ()     print  ' \033[33;1m upload successful \033[0m '         ''     Note: The script uses text to obtain the host's IP address, port and user name information in the following format:   192.168.2.100 22   root   192.168.2.100 1625  zkyw   ......    ""

SFTP based on key multi-process batch concurrent upload file implementation case (Python)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.