目录
add_random_pwd.py
# -*- coding: utf-8 -*-
from __future__ import print_function
import random
import sys
def generate_password(code_len=16):
all_lowercase = 'abcdefghijklmnopqrstuvwxyz'
all_uppercase = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
all_numbers = '0123456789'
all_punctuations = r'!@#$%^&*'
all_password = all_lowercase + all_uppercase + all_numbers + all_punctuations
code = ''
for _ in range(code_len):
index = random.randint(0, len(all_password) - 1)
code += all_password[index]
return code
# code = generate_password()
# print(code)
# https://blog.csdn.net/sunbocong/java/article/details/103627884
new_file = open('./newhosts.txt', 'w') # 打开一个文件,可写模式
# with open('./hosts.txt', 'r') as f: # 打开一个文件只读模式
with open(sys.argv[1], 'r') as f: # 打开一个文件只读模式
line_list = f.readlines() # 读取文件中的每一行,放入line列表中
# print(line)
# for i in line:
# print(i)
for line in line_list:
line_new = line.replace('\n', '') # 将换行符替换为空('')
code = generate_password()
# line_new = line_new + r'|' + '\n' # 行末尾加上"|",同时加上"\n"换行符
line_new = line_new + ' ' + code + '\n' # 行末尾加上"|",同时加上"\n"换行符
print(line_new)
new_file.write(line_new) # 写入一个新文件中
modify_server_pwd.py
# -*- coding: utf-8 -*-
from __future__ import print_function
import paramiko
import sys
# 原脚本是一个老哥发的,瞎胡改了改...
def run_change():
s = paramiko.SSHClient()
s.set_missing_host_key_policy(paramiko.AutoAddPolicy()) # 自动添加服务器到 know_hosts 文件
# newhosts.txt 文件格式:
# ip, 旧密码, 新密码 # 只修改root,需要指定端口或用户稍加修改即可,类似脚本 https://blog.51cto.com/weixiaoxin/2063323 (未测试)
"""
10.0.0.202 123456 centos
10.0.0.41 centos 123456
"""
# with open('/server/scripts/paramiko/hosts.txt', 'r') as f:
with open(sys.argv[1], 'r') as f:
for ip1 in f:
# print(ip1.strip())
hostip = ip1.split()[0]
# name =ip1.split()[1]
oldpass = ip1.split()[1]
newpass = ip1.split()[2]
# print('----------------')
# print(hostip)
# print(oldpass)
# print(newpass)
# print('++++++++++++++++')
# print('\n')
try:
s.connect(hostname=hostip, username='root', password=oldpass) # connect 方法 现远程服务器连接与认证 ip,端口,用户,密码
stdin, stdout, stderr = s.exec_command('echo %s |passwd --stdin root' % str(newpass)) # 在远程机执行命令的方法
# print(stdin,stdout,stderr)
r_message = stdout.read()
# print('----------------')
# print(type(r_message))
# print('++++++++++++++++')
# print(bytes.decode(r_message))
if "成功" in bytes.decode(r_message):
print('%s密码更新成功' % hostip)
print("\n")
elif "successfully" in bytes.decode(r_message):
print('%s密码更新成功' % hostip)
print("\n")
else:
print('%s密码更新失败' % hostip)
print(stderr.read()) # bytes.decode
print("\n")
s.close()
except Exception as e:
print('-----------------------------------error info----------------------------------------')
print(e)
print('+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++')
# print("\n")
print("%s密码错误,连接失败" % hostip)
print("\n")
# except Exception:
# 不能正常连接的server打印信息到控制台
if __name__ == '__main__':
run_change()
先建好hosts.txt文件
# 格式: ip地址 root密码
python@ubuntu20:/server/scripts/paramiko$ cat hosts.txt
10.0.0.202 123456
10.0.0.41 123456
10.0.0.51 123456
10.0.0.7 123456
先执行添加随机密码脚本, 生成新文件 newhosts.txt
python@ubuntu20:/server/scripts/paramiko$ python3 add_random_pwd.py ./hosts.txt
10.0.0.202 123456 *#RtV$Ua4Gj83CKW
10.0.0.41 123456 jKYg8Ee7rJS&dU*U
10.0.0.51 123456 Y34KGk4*KzD0XfLg
10.0.0.7 123456 ESrRxDO&asvv#Aub
python@ubuntu20:/server/scripts/paramiko$ cat newhosts.txt
10.0.0.202 123456 *#RtV$Ua4Gj83CKW
10.0.0.41 123456 jKYg8Ee7rJS&dU*U
10.0.0.51 123456 Y34KGk4*KzD0XfLg
10.0.0.7 123456 ESrRxDO&asvv#Aub
执行修改密码脚本
# 为了之后虚拟机方便还原密码,手动修改了一下newhosts.txt做测试,只修改一台机器
python@ubuntu20:/server/scripts/paramiko$ cat newhosts.txt
10.0.0.41 123456 centos
10.0.0.202 123456 *#RtV$Ua4Gj83CKW
10.0.0.41 centos *#rtr$ua4gj83ckw
python@ubuntu20:/server/scripts/paramiko$ python3 modify_server_pwd.py ./newhosts.txt
10.0.0.41密码更新成功
-----------------------------------error info----------------------------------------
[Errno None] Unable to connect to port 22 on 10.0.0.202
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
10.0.0.202密码错误,连接失败
10.0.0.41密码更新成功
发表评论