Batch modify Linux server password, generate execl form
Copy Code code as follows:
#!/usr/bin/env python
#coding: UTF8
#随机生成自定义长度密码
From random import choice
Import String,pickle
def genpassword (length=8,chars=string.ascii_letters+string.digits):
Return '. Join ([Choice (chars) to I in range (length)])
def passlist (r_user,c_user,ip_list,web_list):
dic={}
For I in Ip_list:
If I.split () [1] in web_list:
Dic[i.split () [1]]=[i.split () [0],{r_user:genpassword (+), C_user:genpassword (32)}]
Else
Dic[i.split () [1]]=[i.split () [0],{r_user:genpassword (32)}]
return dic
def ip_list (ip_file):
With open (ip_file) as file:
Ip_file=file.read (). Strip (). Split ("\ n")
#web_list =[i for I-ip_file if I in web_list]
Return Ip_file
Def save_dic ():
Dic=main ()
With open (".. /host_message.pickle "," w ") as FD:
Pickle.dump (DIC,FD)
def main ():
File= "/root/cui/script/python/ip.txt"
WEB_LIST=[IP1,IP2,IP3,IP4]
File_list=ip_list (file)
File_dic_pass=passlist ("root", "www", file_list,web_list)
Return File_dic_pass
If __name__== "__main__":
Save_dic ()
Copy Code code as follows:
#!/usr/bin/env python
#coding: UTF8
#批量修改密码主程序
Import Pickle
From Ssh_co.ssh_connect import sshd
From COMMAND.IP_PASSWD import Save_dic,main
From execl import set_execl
Def read_dic ():
With open ("Host_message.pickle", "RB") as F:
Return Pickle.load (f)
Def ssh_main ():
Pid_host=1
Host_message=read_dic ()
Host_list=host_message.keys ()
Print Host_list
For host_msg in Xrange (Len (host_list)):
Host_attribute= (HOST_LIST[HOST_MSG],PORT,USER,PASSWD)
Ssh_conn=sshd (Host_attribute,pid_host)
If "www" in Host_message[host_list[host_msg]][1].keys ():
For username in xrange (len (Host_message[host_list[host_msg]][1].keys ())):
Try
User=host_message[host_list[host_msg]][1].keys () [username]
Passwd=host_message[host_list[host_msg]][1][host_message[host_list[host_msg]][1].keys () [username]]
Results=ssh_conn.set_ssh_cmd (U "/bin/echo%s|/usr/bin/passwd--stdin%s"% (Passwd,user))
Print results,host_list[host_msg],user+ "-----------" +passwd
Except
Print results,host_list[host_msg],user+ "-----except------" +passwd
Else
User=host_message[host_list[host_msg]][1].keys () [0]
Passwd=host_message[host_list[host_msg]][1][host_message[host_list[host_msg]][1].keys () [0]]
Result=ssh_conn.set_ssh_cmd (U "/bin/echo%s|/usr/bin/passwd--stdin%s"% (Passwd,user))
Print result,host_list[host_msg],user+ "-----------" +passwd
if __name__ = = "__main__":
Dic=main ()
With open ("Host_message.pickle", "w") as FD:
Pickle.dump (DIC,FD)
SET_EXECL ()
Ssh_main ()