Server Password Management Script

#!/usr/bin/python #-- coding: utf-8 -- from random import choice import string,sys,time import paramiko,json from fabric.api import env,run,cd,task,roles

env.roledefs = { 'test':['[email protected]:22','[email protected]:22'], 'live':['[email protected]:22','[email protected]:22'] }

class Ch_passwd(): def init(self,user): #self.ip = ip self.user = user self.passwd = [] ######产生随机密码### """function to generate a passwd""" def get_passwd(self,passwd_length=10): ####默认值 passwd_seed = string.digits + string.ascii_lowercase + string.ascii_letters

  • string.punctuation.replace("'","").replace('"','') passwd = [] while len(passwd) < passwd_length: passwd.append(choice(passwd_seed)) password=''.join (passwd) password=password.replace("'","").replace('"','')###This is to remove single and double quotes > to avoid unnecessary errors return password ### #Login to the server to change the password def cha_passwd(self,Ip): new_passwd = self.get_passwd() self.passwd.append(new_passwd) run("/bin/echo '%s'|passwd --stdin %s" %(new_passwd ,self.user)) data = dict(zip(Ip,self.passwd)) with open('/mnt/passwd.txt','w') as f: json.dump(data,f,indent=4) ch_passwd = Ch_passwd('root') @task @roles('test') def get_host_test(): """ get hostname """ print time.strftime("%Y-%m-%d %H:%M:%S",time.localtime()) Ip = [] for ip in env.roledefs['test']: r1 = r"root@([\d+.]+):22" s = ''.join(re.findall(r1,ip)) Ip.append(s) ch_passwd.cha_passwd(Ip)

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325456941&siteId=291194637