saltstack安装:略过
python脚本修改密码:
# -*- coding utf-8 -*-
import socket
import re
import os
import sys
import crypt localIP = socket.gethostbyname(socket.gethostname()) def password():
localIP = socket.gethostbyname(socket.gethostname()) #获取本地ip
a = re.split('[.]', localIP) #切割ip地址并生成列表
addr = a[3]
ps = "Subuy2017@"+addr
return ps new_passwd = crypt.crypt(password(),"ab") #将生成的密码加密
change_passwd = "usermod -p %s test" %(new_passwd) #采用usermod方式修改密码,必须使用密文密码来修改
os.system(change_passwd)
print("Congratulation!,password for %s is %s " %(localIP,password()))
sys.exit()
结合以上脚本配合saltstack 命令来批量修改服务器密码:
[root@testd files]# salt -N 'test' state.highstate
rac2:
----------
ID: file
Function: file.managed
Name: /usr/local/shell/change_passwd.py
Result: True
Comment: File /usr/local/shell/change_passwd.py updated
Started: 10:57:38.489545
Duration: 568.87 ms
Changes:
----------
diff:
---
+++
@@ -17,7 +17,7 @@
return ps new_passwd = crypt.crypt(password(),"ab")
-change_passwd = "usermod -p %s test" %(new_passwd)
+change_passwd = "usermod -p %s root" %(new_passwd)
os.system(change_passwd)
print("Congratulation!,password for %s is %s " %(localIP,password()))
sys.exit()
----------
ID: pkg_install
Function: cmd.run
Name: python /usr/local/shell/change_passwd.py
Result: True
Comment: Command "python /usr/local/shell/change_passwd.py" run
Started: 10:57:39.059976
Duration: 114.552 ms
Changes:
----------
pid:
19860
retcode:
0
stderr:
stdout:
Congratulation!,password for IP地址 is Subuy2017@162 Summary
------------
Succeeded: 2 (changed=2)
Failed: 0
------------
Total states run: 2
rac1:
----------
ID: file
Function: file.managed
Name: /usr/local/shell/change_passwd.py
Result: True
Comment: File /usr/local/shell/change_passwd.py updated
Started: 10:57:39.149359
Duration: 598.117 ms
Changes:
----------
diff:
---
+++
@@ -17,7 +17,7 @@
return ps new_passwd = crypt.crypt(password(),"ab")
-change_passwd = "usermod -p %s test" %(new_passwd)
+change_passwd = "usermod -p %s root" %(new_passwd)
os.system(change_passwd)
print("Congratulation!,password for %s is %s " %(localIP,password()))
sys.exit()
----------
ID: pkg_install
Function: cmd.run
Name: python /usr/local/shell/change_passwd.py
Result: True
Comment: Command "python /usr/local/shell/change_passwd.py" run
Started: 10:57:39.749030
Duration: 109.626 ms
Changes:
----------
pid:
720
retcode:
0
stderr:
stdout:
Congratulation!,password for IP地址 is Subuy2017@161 Summary
------------
Succeeded: 2 (changed=2)
Failed: 0
------------
Total states run: 2