This repository has been archived by the owner on Nov 26, 2020. It is now read-only.
/
sshpasschanger.py
executable file
·120 lines (105 loc) · 3.73 KB
/
sshpasschanger.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#!/usr/bin/env python
import paramiko
import threading
import re
import argparse
import socket
import thread
arg_parser = argparse.ArgumentParser(description='command line tool for \
changing mutliple passwords over ssh',
usage='%(prog)s -c filename')
arg_parser.add_argument('-c', nargs=1, help='-c followed by filename \
containing hosts')
args = arg_parser.parse_args()
host_re = re.compile("(^HOST [\d\w])")
ip_re = re.compile("^IP: (\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})")
user_re = re.compile("^username: ([\w+\d+]+)")
password_re = re.compile("^password: (.+)")
newpass_re = re.compile("^newpass: (.+)")
filename = args.c[0]
# Host class for creating threaded host objects
class Hosts(threading.Thread):
def __init__(self, host, ip_addr, username, password, newpass):
threading.Thread.__init__(self)
self.host = host
self.ip_addr = ip_addr
self.username = username
self.password = password
self.newpass = newpass
def HostInfo(self):
"prints host info"
print self.host
print self.ip_addr
print self.username
print self.password
print self.newpass
def run(self):
"use paramiko sshclient to change passwords through ssh"
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
try:
ssh.connect(self.ip_addr, username=self.username,
password=self.password)
except socket.error:
print "Connection refused on %s." % self.ip_addr
thread.exit()
except paramiko.AuthenticationException:
print "wrong username and/or password %s." % self.ip_addr
thread.exit()
stdin, stdout, stderr = ssh.exec_command('passwd')
if self.username == 'root':
stdin.write('%s\n' % self.newpass)
stdin.flush()
stdin.write('%s\n' % self.newpass)
stdin.flush()
print stdout.readlines()
print "password change on %s for user %s successfull" % \
(self.ip_addr, self.username)
else:
stdin.write('%s\n' % self.password)
stdin.flush()
stdin.write('%s\n' % self.newpass)
stdin.flush()
stdin.write('%s\n' % self.newpass)
stdin.flush()
print stdout.readlines()
print "password change on %s for user %s successfull" % \
(self.ip_addr, self.username)
ssh.close()
def GetHosts(filename):
"Parse config file to get host information and create host objecs"
hosts = []
try:
hostfile = open(filename)
except IOError:
print "file %s not found" % filename
for line in hostfile:
line = line.strip("\r\n")
if host_re.match(line):
hostnum = host_re.match(line).groups()[0]
elif ip_re.match(line):
ip = ip_re.match(line).groups()[0]
elif user_re.match(line):
user = user_re.match(line).groups()[0]
elif password_re.match(line):
password = password_re.match(line).groups()[0]
elif newpass_re.match(line):
newpass = newpass_re.match(line).groups()[0]
host = Hosts(hostnum, ip, user, password, newpass)
hosts.append(host)
else:
pass
return hosts
def main():
threads = []
hosts = GetHosts(filename)
for host in hosts:
# host.HostInfo()
threads.append(host)
for i in xrange(len(hosts)):
threads[i].start()
for i in xrange(len(hosts)):
threads[i].join()
print 'all Done'
if __name__ == '__main__':
main()