paramiko及简单多进程

paramiko 具体就不多说,看其他资料

yum insatll python-devel

1.PyCrypto安装

tar -xf pycrypto-2.6.1.tar.gz
cd pycrypto-2.6.1/
python setup.py build && python setup.py install

2.ecdsa安装

unzip python-ecdsa-master.zip
cd python-ecdsa-master
python setup.py install

3.paramiko安装

unzip paramiko-master.zip
cd paramiko-master
python setup.py build && python setup.py install

4.错误:

>>> import paramiko
/usr/lib64/python2.6/site-packages/Crypto/Util/number.py:57: PowmInsecureWarning: Not using mpz_powm_sec.  You should rebuild using libgmp >= 5 to avoid timing attack vulnerability.
  _warn("Not using mpz_powm_sec.  You should rebuild using libgmp >= 5 to avoid timing attack vulnerability.", PowmInsecureWarning)
>>> exit()

5.简单多进程:

#!/usr/bin/python   
import paramiko  
import os
import sys
import time
import multiprocessing
import datetime

ip_list = []

def Execmd(host,cmd):
    time.sleep(2)
    hostname=host
    username="ckl"

    ssh=paramiko.SSHClient()
    ssh.load_system_host_keys()
    privatekey = os.path.expanduser('/home/chenkangle/key/ckl.pem')
    key = paramiko.RSAKey.from_private_key_file(privatekey)

    ssh.connect(hostname=hostname,username=username,pkey = key)
    stdin,stdout,stderr=ssh.exec_command(cmd)
    print stdout.read()
    ssh.close()

def Upfile(host_ip,local_path,remote_path):
    time.sleep(2)
    privatekey = os.path.expanduser('/home/ckl/.ssh/id_rsa')
    key=paramiko.RSAKey.from_private_key_file(privatekey)
    scp = paramiko.Transport((host_ip, 22))
    scp.connect(username='ckl', pkey=key)

    sftp = paramiko.SFTPClient.from_transport(scp)
    src = local_path
    des = remote_path
    file=os.path.basename(src)
    print '='*50
    print 'Start to upload file %s ' % datetime.datetime.now()
    print 'Upload file:',file
    sftp.put(local_path,remote_path)
    scp.close()
    print '\033[1;32m Upload file success %s \033[0m' % datetime.datetime.now()
    print '='*50

def Downfile(host_ip,remote_path,local_path):
    privatekey = os.path.expanduser('/home/chenkangle/key/ckl.pem')
    key=paramiko.RSAKey.from_private_key_file(privatekey)
    scp = paramiko.Transport((host_ip, 22))
    scp.connect(username='ckl', pkey=key)

    sftp = paramiko.SFTPClient.from_transport(scp)
    src = remote_path
    file=os.path.basename(src)
    des = local_path
    print '='*50
    print 'Start to download file %s ' % datetime.datetime.now()
    print 'Download file:',file
    sftp.get(src,des)
    scp.close()
    print '\033[1;32m download file success %s \033[0m' % datetime.datetime.now()
    print '='*50

def all_ip():
    f = file('2.txt','r')
    c = f.readlines()
    for x in c:
        ip = x.split('\n')[0]
        ip_list.append(ip)
    f.close()


if __name__=='__main__':
    all_ip()
    pool = multiprocessing.Pool(processes=30)
    threads = []
    print "Begin......"
    for i in ip_list:
        #pool.apply_async(Execmd,(i,))
        pool.apply_async(Upfile,(i,'/home/chenkangle/php_0420.tar.gz','/tmp/soft/php_0420.tar.gz'))
    pool.close()
    pool.join()

    for res in threads:
        print res.get()
    print "process done."

6.说明:

 2.txt 是一个列表文件

 内容如下:

10.1.1.1
10.1.1.2
10.1.1.3
....

  通过进程池poll批量操作,控制同时运行的进程processes=30

你可能感兴趣的:(python,多进程)