python 隨機遠程主機修改密碼


執行腳本需要有以下前提;

  1. 主機與客戶機配置互信(ssh 無密碼認證登錄)

  2. 需要讀取當前目錄下的host文件,裏面是連接遠程主機的ip地址

  3. 腳本可以修改遠程主機爲ubuntu和centos的密碼


代碼如下:

#!/usr/bin/env python
#coding:utf-8

import paramiko
import platform
import sys,os
import threading
import time

def color_print(msg, color='red', exits=False):   //定義輸出信息顏色函數
    color_msg = {'blue': '\033[1;36m%s\033[0m',
                 'green': '\033[1;32m%s\033[0m',
                 'yellow': '\033[1;33m%s\033[0m',
                 'red': '\033[1;31m%s\033[0m',
                 'title': '\033[30;42m%s\033[0m',
                 'info': '\033[32m%s\033[0m'
}
    msg = color_msg.get(color, 'red') % msg
    print msg
    if exits:
        time.sleep(2)
        sys.exit()
    return msg

def ssh(hostname,cmd):   //ssh 連接遠程主機
    port=22
    username='root'
    pkey_file='/root/.ssh/id_rsa'
    key = paramiko.RSAKey.from_private_key_file(pkey_file)
    s = paramiko.SSHClient()
    s.load_system_host_keys()
    s.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    try:
        s.connect(hostname,port,username,pkey=key)
        stdin,stdout,stderr = s.exec_command(cmd)
    result=stdout.read()
        color_print('Connect %s Successful' % hostname,'info')
return result.strip('\n')
    except:
        color_print('Connect %s failed' % hostname,'red',True)

def MkPasswd():   //生成隨機密碼,密碼包含數字,字母,特殊字符
    from random import choice
    import string
    SpecialChar='&!@#$%^*-_='
    length=16
    chars=string.letters+string.digits+SpecialChar
    passwd=''.join([choice(chars) for i in range(length)])
    return passwd

def PwdFile(hostname,passwd):  //生成密碼保存在腳本目錄下
    istimeformat='%Y-%m-%d'
    Date=time.strftime(istimeformat,time.localtime())
    FileName='UpdatePwd_%s.txt' % Date
    print FileName
    f=open(FileName,'a')
    f.write(hostname+':\t'+passwd+'\n')
    f.close()

def UpdatePwd(Linux_Dist,passwd,hostname):  //修改密碼
    cmd1="echo ubuntu:'%s' | chpasswd"  % passwd
    cmd2="echo root:'%s' | chpasswd"  % passwd
    List=['CentOS','Redhat']
    if Linux_Dist=='Ubuntu':
        try:
            ssh(hostname,cmd1)
            color_print('%s User Ubuntu  Passwd Update Successful!' % hostname,'yellow')
            PwdFile(hostname,passwd)
        except:
            color_print('%s User Ubuntu  Passwd Update Faied!!!' % hostname,'red')
    elif Linux_Dist in List:
        try:
            ssh(hostname,cmd2)
            color_print('%s User Root Passwd Update Successful!' % hostname,'yellow')
            PwdFile(hostname,passwd)
        except:
             color_print('%s User Root Passwd Update Faied!!!' % hostname,'red')
    else:
        color_print('Unsupported operating system','red')

def main(hostname):
    sys_cmd="cat /etc/issue | head -n 1 |awk '{print $1}'"
    passwd=MkPasswd()
    color_print('Random Passwd: %s' % passwd,'info')
    Linux_Dist=ssh(hostname,sys_cmd)
    color_print('%s linux distribution is: %s' % (hostname,Linux_Dist),'info')
    UpdatePwd(Linux_Dist,passwd,hostname)
class MyThread(threading.Thread):
    def __init__(self,hostname):
        self.hostname=hostname
        threading.Thread.__init__(self)
    def run(self):
        main(self.hostname)   //調用main函數


if  __name__=='__main__':
    try:
        with open('host') as f:      //讀取遠程主機ip地址
            for i in f:
                hostname=i.strip('\n')
                t=MyThread(hostname)  //調用類MyThread,實現多線程執行
                t.start()
    except Exception,e:
        color_print(e,'red')


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章