#!/usr/bin/env python #encoding:utf-8 #paramiko import paramiko import sys,os import threading import multiprocessing import time iplist = {‘192.168.1.10‘:‘123456‘,‘192.168.2.10‘:‘321456‘,‘192.168.13.10‘:‘111111‘} user = ‘root‘ host = ‘‘ password = ‘‘ bit = ‘‘ pakg = ‘‘ s = paramiko.SSHClient() s.load_system_host_keys() s.set_missing_host_key_policy(paramiko.AutoAddPolicy()) number = 0 def ssh_install(): cmd = ‘rpm -Uvh /tmp/shell_update.rpm‘ s.connect(host,22,user,password,timeout=1) stdin,stdout,stderr = s.exec_command(cmd) cmd_result = stdout.read(),stderr.read() print ‘======%s--%sbit======‘ %(host,bit) for line in cmd_result: print line, print ‘‘ s.close() def ssh_bit(): global bit cmd = ‘getconf LONG_BIT‘ s.connect(host,22,user,password,timeout=1) stdin,stdout,stderr = s.exec_command(cmd) cmd_result = stdout.read(),stderr.read() bit = int(str(cmd_result[0]).strip()) s.close() def ssh_copy(): global pakg t = paramiko.Transport((host,22)) t.connect(username=user,password=password) sftp = paramiko.SFTPClient.from_transport(t) sftp.put(pakg,‘/tmp/shell_update.rpm‘ ) t.close() def run(ipinfo): global pakg global host global password global bit host = ipinfo[0] password = ipinfo[1] ssh_bit() if bit == 64: pakg = ‘/tmp/bash-3.2-33.el5_11.4.x86_64.rpm‘ ssh_copy() ssh_install() elif bit == 32: pakg = ‘/tmp/bash-3.2-33.el5_11.4.i386.rpm‘ ssh_copy() ssh_install() if len(iplist.items()) < 10: process_number = len(iplist.items()) else: process_number = 10 pool = multiprocessing.Pool(processes=process_number) for info in iplist.items(): pool.apply_async(run,(info,)) pool.close() pool.join()
时间: 2024-10-29 22:03:36