首页 > Web开发 > 详细

批量下执行命令,上传下载文件的脚本

时间:2016-01-03 21:05:44      阅读:303      评论:0      收藏:0      [点我收藏+]

 前言:

    一家中型公司,全国各省近2000台服务器竟然没有一个运维平台,我也是醉了,每天挨个挨个登陆机器,管理机器真是操蛋,为此自己工作之余借助paramiko模块写了一个批量执行命令,上传下载文件的脚本,写的代码烂到哭,但最起码还能凑合着用吧。

    使用环境是python2.6-2.7,其中要在当前目录下生成一个ssh_login.txt文件,它是用户保存登陆用户名和密码以及主机名的文件,格式如下:

主机名,端口,用户名,验证方式是公私钥认证还是密码验证,密码,  #每行以逗号结尾,不允许有空行,
172.168.19.25,22,root,password,xxxxx, 172.168.19.26,22,root,password,xxxxx,

 

#/usr/bin/env python
import paramiko
import sys
import os
import getopt
import multiprocessing
import time
import socket
pkey_file=/root/.ssh/id_rsa
if os.path.exists(ssh_login.txt) is not  True:
    print \033[31mNo such the ssh_login.txt\033[0m
    sys.exit()
host_number=10
p=multiprocessing.Pool(processes=host_number)


def ssh_connect(cmd,line,host):
  
  s = paramiko.SSHClient()
  s.set_missing_host_key_policy(paramiko.AutoAddPolicy())
  s.load_system_host_keys()
  #paramiko.common.logging.basicConfig(level=paramiko.common.DEBUG)        输出#paramiko调试信息
  port=int(line.split(,)[1])
  user=line.split(,)[2]
  auth_method=line.split(,)[3]
  if auth_method==password:
    password=line.split(,)[4]
    s.connect(host,port,user,password,timeout=3)
  else:
    key = paramiko.RSAKey.from_private_key_file(pkey_file)
    s.connect(host,port,user,key,timeout=3)
  stdin,stdout,stderr = s.exec_command(cmd)
  result = stdout.read(),stderr.read()
  print \033[32m=*25+"the result from ",host+"\033[32m=\033[0m"*25
  for a in result:
      print a
  s.close()



def sftp_put(line,host,lfile,rfile):
    port=int(line.split(,)[1])
    user=line.split(,)[2]
    auth_method=line.split(,)[3]
    if auth_method==password:
        try:
        passwd=line.split(,)[4]
        t = paramiko.Transport((host,port))
        t.connect(username=user,password=passwd)
        sftp=paramiko.SFTPClient.from_transport(t)
        sftp.put(lfile,rfile)
        print "\033[32m I put %s to %s now,please waite a moment .....\033[0m\n"%(lfile,host)
        print sftp.stat(rfile)
        t.close();
        except (IOError):
        print host,"\033[31m -- Must be input remote file absolute path!!\033[0m"
        try:
        key=paramiko.RSAKey.from_private_key_file(pkey_file)
        t=paramiko.Transport((host,port))
        t.connect(username=user,pkey=key)
        sftp=paramiko.SFTPClient.from_transport(t)
        sftp.put(lfile,rfile)
        print "\033[32m I put %s to %s now,please waite a moment .....\033[0m\n"%(lfile,host)
        print sftp.stat(rfile)
        t.close();
        except (IOError):
        print host,"\033[31m -- Must be input remote file absolute path!!\033[0m"
        
    
def sftp_down(line,host,rfile,lfile):
    port=int(line.split(,)[1])
    user=line.split(,)[2]
    auth_method=line.split(,)[3]
    if auth_method==password:
        try:
        passwd=line.split(,)[4]
        t = paramiko.Transport((host,port))
        t.connect(username=user,password=passwd)
        sftp=paramiko.SFTPClient.from_transport(t)
        sftp.get(rfile,lfile)
        os.rename(lfile,lfile+":"+host)
        print "\033[32m I down %s from %s now,please waite a moment .....\033[0m"%(rfile,host)
        print sftp.stat(rfile)
        t.close();
        except (IOError):
        print host,"\033[31m -- Must be input remote file absolute path!!\033[0m"
    else:
        try:
        key=paramiko.RSAKey.from_private_key_file(pkey_file)
        t=paramiko.Transport((host,port))
        t.connect(username=user,pkey=key)
        sftp=paramiko.SFTPClient.from_transport(t)
        sftp.get(rfile,lfile)
        os.rename(lfile,lfile+:+host)
        print "\033[32m I down %s from %s now,please waite a moment .....\033[0m"%(rfile,host)
        print sftp.stat(rfile)
        t.close();
        except (IOError):
        print host,"\033[31m -- Must be input remote file absolute path!!\033[0m"


#main_program
p=multiprocessing.Pool(processes=10)
opts,args=getopt.getopt(sys.argv[1:],"c:m:h:r:l:")
opts=dict(opts)
cmd=opts.get(-c)
pd=opts.get(-m)
l_file=opts.get(-l)
r_file=opts.get(-r)
help=opts.get(-h)
if help is not   None:
        print "\033[35mUseag: %s [ -c command] -m put/down -l local_file_path -r remote_file_path\033[0m" %sys.argv[0]

result=[]
if cmd is not None:
     login_info=open(ssh_login.txt,r)
    err_log=open(error.log,a)
    err_log.write(=*30+this is last result+=*30+\n)
    for line in login_info:
        try:
          host=line.split(,)[0]
        result.append(p.apply_async(ssh_connect,(cmd,line,host,)))
        ‘‘‘        
        p = multiprocessing.Process(target=ssh_connect,args=(cmd,line))
        p.start()
        result.append(p)
    for pro in result:
        pro.join()
        print pro
        ‘‘‘
            except BaseException,e:
               print "\033[46mError Info from %s\033[0m:"%host,e,\n\n
               err_log.write(str(time.ctime())+" | Error Info from "+str(host)+ : +str(e)+\n)
               continue
    p.close()
    p.join()
    print "\033[36m\t\t\t\tProgram Run Out!!\033[0m"
        err_log.flush()
        err_log.close()
        login_info.close()


else:
    if pd == put:
        if l_file and r_file is not None:
         login_info=open(ssh_login.txt,r)
        err_log=open(error.log,a)
        err_log.write(=*30+this is last result+=*30+\n)
        for line in login_info:
               try:
                  host=line.split(,)[0]
                result.append(p.apply_async(sftp_put,(line,host,l_file,r_file,)))
                    except BaseException,e:
                       print "\033[46mError Info from %s\033[0m:"%host,e,\n\n
                       err_log.write(str(time.ctime())+" | Error Info from "+str(host)+ : +str(e)+\n)
                       continue
        
        print "\033[36m\t\t\t\tProgram Run Out!!\033[0m"
        p.close()
        p.join()
        err_log.flush()
        err_log.close()
        login_info.close()
        else:
            print "\033[36m  -l local_file_path -r remote_file_path\033[0m"
    elif pd==down:
        if l_file and r_file is not None:
         login_info=open(ssh_login.txt,r)
        err_log=open(error.log,a)
        err_log.write(=*30+this is last result+=*30+\n)
        for line in login_info:
               try:
                  host=line.split(,)[0]
                result.append(p.apply_async(sftp_down,(line,host,l_file,r_file,)))
                    except BaseException,e:
                       print "\033[46mError Info from %s\033[0m:"%host,e,\n\n
                       err_log.write(str(time.ctime())+" | Error Info from "+str(host)+ : +str(e)+\n)
                       continue
        p.close()
        p.join()
        print "\033[36m\t\t\t\tProgram Run Out!!\033[0m"
        err_log.flush()
        err_log.close()
        login_info.close()
        else:
            print "\033[36m  -l local_file_path -r remote_file_path\033[0m"
    else:
            print "\033[31m  The Mode Is Put Or Down!!\033[0m"

 

批量下执行命令,上传下载文件的脚本

原文:http://www.cnblogs.com/liaojiafa/p/5097002.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!