批量改ssh密码脚本,初始IP;端口,用户名,密码在txt文档中
时间: 2024-04-12 19:27:57 浏览: 5
你可以使用以下Python脚本来批量修改SSH密码,其中初始IP、端口、用户名和密码需要以文本文件的形式提供:
```python
import paramiko
def change_ssh_password(ip, port, username, password, new_password):
try:
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(ip, port=port, username=username, password=password)
# 修改密码命令
command = f"echo '{username}:{new_password}' | sudo chpasswd"
# 执行命令
stdin, stdout, stderr = ssh.exec_command(command)
# 检查命令执行结果
if stderr.channel.recv_exit_status() == 0:
print(f"Password changed successfully for {ip}")
else:
print(f"Failed to change password for {ip}")
ssh.close()
except paramiko.AuthenticationException:
print(f"Authentication failed for {ip}")
# 从文本文件中读取初始IP、端口、用户名和密码
with open('credentials.txt', 'r') as file:
lines = file.readlines()
for line in lines:
ip, port, username, password, new_password = line.strip().split(',')
change_ssh_password(ip, int(port), username, password, new_password)
```
你需要将初始IP、端口、用户名、密码和新密码按照以下格式保存在名为`credentials.txt`的文本文件中:
```
ip1,port1,username1,password1,new_password1
ip2,port2,username2,password2,new_password2
ip3,port3,username3,password3,new_password3
```
每行代表一个SSH连接的相关信息。脚本将依次读取每行并连接到相应的IP上,然后执行修改密码的操作。修改成功后,将会打印相应的提示信息。请确保你已经安装了Python和`paramiko`库。
注意:这个脚本使用了`paramiko`库来进行SSH连接和执行命令,请在使用之前确保已经安装了该库。另外,请注意在使用脚本时要小心验证连接信息的准确性,以免对系统造成意外影响。
希望这能帮到你!如果有任何问题,请随时问我。