MENU

ssh 批量修改主机

January 1, 2018 • Read: 4460 • 17年实战

根据 dhcp 地址池,批量初始化对应主机

python ssh_command.py

#!/usr/bin/env python
# -- coding: utf-8 --
# --------------------------------------------------
#Author:        LJ
#Email:         admin@attacker.club
#Site:          blog.attacker.club

#Site:blog.attacker.club  Mail:admin@attacker.club
#Date:2017-08-19 17:12:02 Last:2017-09-12 19:23:32
#Description:   
# --------------------------------------------------
import os
import time
import paramiko


passswd = 'xxxxxx'


info_list = [
    {"10.10.10.46":"xxxcenter-1-1"},
    {"10.10.10.47":"xxxcenter-1-2"},
    {"10.10.10.48":"xxxcenter-1-3"},
    {"10.10.10.49":"xxxcenter-1-4"},
    {"10.10.10.50":"xxxcenter-1-5"},
]



def ssh_command (host,var):
    paramiko.util.log_to_file('ssh.log')
    ssh=paramiko.SSHClient() #创建一个远程登录实例
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())#know_host列表
    ssh.connect(
        hostname=host, #远程主机地址
        port=22, #默认端口
        username='root',
        password=passswd,
        pkey=None,timeout=10
        )

    print 'connect成功'

    #ssh.exec_command('/usr/bin/curl http://yum.ops.net/script/init.sh| /bin/bash  -s %s' % var)
    #time.sleep(60)
    ssh.exec_command('touch /root/%s' % var)
    stdin,stdout,stderr=ssh.exec_command('ls')
    print stdout.read()
    print '命令执行完毕'
    ssh.close()
    print '关机SSH连接'




if name == "__main__":
    for list in info_list :
        while True:
            ip = list.keys()[0]
            hostname = list.values()[0]
            os.system('clear')
            print  '正在配置主机:033[1;35m %s 033[0m' % hostname
            output=os.popen("/bin/ping -c 1 -W 2  %s" % (ip)).read().split("n")
            print 'nnn等待IP:033[1;32;43m %s 033[0m 上线。。。' % ip
            time.sleep(3)
            if "1 packets transmitted, 1 received, 0% packet loss, time 0ms" in output:
                ssh_command(ip,hostname)
                print  'ssh_command 执行结束'
                break#跳出整个循环
            else:
                continue #跳出本次循环

兼总条贯 知至知终

无标签
最后编辑于: March 26, 2018