python 随机远程主机修改密码
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python 随机远程主机修改密码相关的知识,希望对你有一定的参考价值。
执行脚本需要有以下前提;
主机与客户机配置互信(ssh 无密码认证登录)
需要读取当前目录下的host文件,里面是连接远程主机的ip地址
脚本可以修改远程主机为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=‘&[email protected]#$%^*-_=‘
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‘)
本文出自 “12103362” 博客,请务必保留此出处http://12113362.blog.51cto.com/12103362/1856727
以上是关于python 随机远程主机修改密码的主要内容,如果未能解决你的问题,请参考以下文章