• 58同乐成|58同乐成【唯一授权官网】】】】】

  • 发布时间:2016-01-19 06:09 | 作者:yc | 来源:互联网 | 浏览:1200 次
  • 58同乐成|58同乐成【唯一授权官网】】】】】

    只管现在都完成了自动化如puppet saltstack在情况中的应用,但事情中弗成避免的要自个写一些大略的批量实行shell指令的脚本

    python paramiko模块是现在应用得较为随手的模块,实行指令时基础无需求变换,直接将shell指令扔进入实行就OK

    大略示例,10个线程一路实行ssh或scp动作,未设置timeout时候,如实行永劫候无反映会致使脚本实行疑问:

    #!/usr/bin/python

    # _*_ coding: utf-8 _*_

    import paramiko

    import sys

    import logging

    from multiprocessing.dummy import Pool as ThreadPool

    import re

    import os

    try:

    fun_name = sys.argv[1]

    file = open(sys.argv[2])

    except Exception,e:

    print """use like:

    #copy dir or file

    shell_exec_map.py scp ip_file source_file des_file

    #do cmd in host

    exec_map.py ssh ip_file 'yum install -y zabbix'

    """

    exit()

    logging.basicConfig(level=logging.DEBUG ,

    format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s',

    datefmt='%a, %d %b %Y %H:%M:%S',

    filename='exec_map.log',

    filemode='a+')

    #ssh key文件方位

    privatekeyfile= os.path.expanduser('/root/.ssh/id_rsa')

    mykey = paramiko.RSAKey.from_private_key_file(privatekeyfile)

    #############################

    #对ssh key设置暗码法子

    #privatekeyfile= os.path.expanduser('/root/.ssh/id_rsa')

    #mykey = paramiko.RSAKey.from_private_key_file(privatekeyfile,password='password')

    #############################

    def getCmdInfo(host_ip,cmd):

    print host_ip

    try:

    c = paramiko.SSHClient()

    c.set_missing_host_key_policy(paramiko.AutoAddPolicy())

    ##################

    #暗码登录法子

    #c.connect(host_ip,22,'root',password)

    ##################

    c.connect(host_ip,22,'root',pkey=mykey八达国际娱乐官网)

    stdin,stdout,stderr = c.exec_command(cmd)

    info = stdout.read()

    c.close()

    return info

    except Exception as e:

    logging.error( 'getCmdInfo ip %s%s error: %s' % (host_ip,cmd,e))

    def postFileToRemote(host_ip,localpath,remotepath):

    print host_ip

    try:

    t = paramiko.Transport((host_ip,22))

    ##################

    #暗码登录法子

    #t.connect(username='root',password='')

    ##################

    t.connect(username='root',pkey=mykey)

    sftp = paramiko.SFTPClient.from_transport(t)

    sftp.put(localpath,remotepath)

    sftp.close()

    t.close()

    except Exception,e:

    logging.error( 'scp_cmd ip %s soure:%s des: %s error: %s' % (host_ip,localpath,remotepath,e))

    #10个并发一路进行

    pool = ThreadPool(10)

    line_list = []

    for line in file.readlines():

    line_list.append(line.strip())

    if fun_name == 'scp':

    try:

    pool.map(scpFile,line_list)

    logging.debug('scpFile ip file %s source %s dec %s' % (sys.argv[2],sys.argv[3],sys.argv[4]))

    except Exception as e:

    logging.error( 'scpFile map error: %s ' % e)

    elif fun_name == 'ssh':

    try:

    if pattern_cmd.findall(sys.argv[3]):

    print "can't use rm command!"

    logging.debug('sshCmd ip file %s cmd %s' % (sys.argv[2],sys.argv[3]))

    else:

    pool.map(sshCmd,line_list)

    logging.debug('sshCmd ip file %s cmd %s' % (sys.argv[2],sys.argv[3]))

    except Exception as e:

    logging.error( 'sshCmd map error: %s ' % e)

    pool.close()

    pool.join()

    ansible根据paramiko模块做的批量实行脚本,远端实行

    1、装配ansible软件,设置设备摆香港六合彩公司87788设了epel源,或许线高低载ansible-1.7.2

    2、设置设备摆设节点,IP(主机名需求主性能正常解析)

    vi /etc/ansible/hosts

    [other]

    zabbix-.xxx.com

    haproxy-.xxx .com

    58同乐成|58同乐成【唯一授权官网】】】】】

    controller-.xxx .com

    haproxy-.xxx.com

    3、实行Ping指令测验(带上情况变量. ~/.bash_profile)

    ansible -m shell -a '. ~/.bash_profile &&ping -c 2 10.197.128.1' other

    controller-.xxx.com | success | rc=0 >>

    PING 10.197.128.1 (10.198.127.1) 56(84) bytes of data.

    64 bytes from 10.198.127.1: icmp_seq=1 ttl=254 time=0.466 ms

    64 bytes from 10.198.127.1: icmp_seq=2 ttl=254 time=0.409 ms

    --- 10.198.127.1 ping statistics ---

    2 packets transmitted, 2 received, 0% packet loss, time 1000ms

    rtt min/avg/max/mdev = 0.409/0.437/0.466/0.035 ms

    pinsh-.xxx.com | FAILED => SSH encountered an unknown error during the connection. We recommend you re-run the command using -vvvv, which will enable SSH debugging output to help diagnose the issue

    ansible法子有个未方便确当地首次实行时,需求手动承认下python脚本不需求手动承认

  • 相关内容

友情链接: