saltstack+python批量修改服務器密碼


saltstack安裝:略過

python腳本修改密碼:

 1 # -*- coding utf-8 -*-
 2 import socket
 3 import re
 4 import os
 5 import sys
 6 import crypt
 7 
 8 
 9 localIP = socket.gethostbyname(socket.gethostname())
10 
11 def password():
12     localIP = socket.gethostbyname(socket.gethostname())   #獲取本地ip
13     a = re.split('[.]', localIP)  #切割ip地址並生成列表
14     addr = a[3]
15     ps = "Subuy2017@"+addr
16     return ps
17 
18 new_passwd = crypt.crypt(password(),"ab")  #將生成的密碼加密
19 change_passwd = "usermod -p %s test" %(new_passwd)     #采用usermod方式修改密碼,必須使用密文密碼來修改
20 os.system(change_passwd)
21 print("Congratulation!,password for %s is %s " %(localIP,password()))
22 sys.exit()

 

結合以上腳本配合saltstack 命令來批量修改服務器密碼:

 1 [root@testd files]# salt -N 'test' state.highstate
 2 rac2:
 3 ----------
 4           ID: file
 5     Function: file.managed
 6         Name: /usr/local/shell/change_passwd.py
 7       Result: True
 8      Comment: File /usr/local/shell/change_passwd.py updated
 9      Started: 10:57:38.489545
10     Duration: 568.87 ms
11      Changes:   
12               ----------
13               diff:
14                   ---  
15                   +++  
16                   @@ -17,7 +17,7 @@
17                        return ps
18                    
19                    new_passwd = crypt.crypt(password(),"ab")
20                   -change_passwd = "usermod -p %s test" %(new_passwd)
21                   +change_passwd = "usermod -p %s root" %(new_passwd)
22                    os.system(change_passwd)
23                    print("Congratulation!,password for %s is %s " %(localIP,password()))
24                    sys.exit()
25 ----------
26           ID: pkg_install
27     Function: cmd.run
28         Name: python /usr/local/shell/change_passwd.py
29       Result: True
30      Comment: Command "python /usr/local/shell/change_passwd.py" run
31      Started: 10:57:39.059976
32     Duration: 114.552 ms
33      Changes:   
34               ----------
35               pid:
36                   19860
37               retcode:
38                   0
39               stderr:
40               stdout:
41                   Congratulation!,password for IP地址 is Subuy2017@162
42 
43 Summary
44 ------------
45 Succeeded: 2 (changed=2)
46 Failed:    0
47 ------------
48 Total states run:     2
49 rac1:
50 ----------
51           ID: file
52     Function: file.managed
53         Name: /usr/local/shell/change_passwd.py
54       Result: True
55      Comment: File /usr/local/shell/change_passwd.py updated
56      Started: 10:57:39.149359
57     Duration: 598.117 ms
58      Changes:   
59               ----------
60               diff:
61                   ---  
62                   +++  
63                   @@ -17,7 +17,7 @@
64                        return ps
65                    
66                    new_passwd = crypt.crypt(password(),"ab")
67                   -change_passwd = "usermod -p %s test" %(new_passwd)
68                   +change_passwd = "usermod -p %s root" %(new_passwd)
69                    os.system(change_passwd)
70                    print("Congratulation!,password for %s is %s " %(localIP,password()))
71                    sys.exit()
72 ----------
73           ID: pkg_install
74     Function: cmd.run
75         Name: python /usr/local/shell/change_passwd.py
76       Result: True
77      Comment: Command "python /usr/local/shell/change_passwd.py" run
78      Started: 10:57:39.749030
79     Duration: 109.626 ms
80      Changes:   
81               ----------
82               pid:
83                   720
84               retcode:
85                   0
86               stderr:
87               stdout:
88                   Congratulation!,password for IP地址 is Subuy2017@161
89 
90 Summary
91 ------------
92 Succeeded: 2 (changed=2)
93 Failed:    0
94 ------------
95 Total states run:     2

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM