1
1
#!/usr/bin/env python
3
"""Change passwords on the named machines. passmass host1 host2 host3 . . .
4
Note that login shell prompt on remote machine must end in # or $. """
3
'''Change passwords on the named machines. passmass host1 host2 host3 . . .
4
Note that login shell prompt on remote machine must end in # or $.
8
This license is approved by the OSI and FSF as GPL-compatible.
9
http://opensource.org/licenses/isc-license.txt
11
Copyright (c) 2012, Noah Spurrier <noah@noah.org>
12
PERMISSION TO USE, COPY, MODIFY, AND/OR DISTRIBUTE THIS SOFTWARE FOR ANY
13
PURPOSE WITH OR WITHOUT FEE IS HEREBY GRANTED, PROVIDED THAT THE ABOVE
14
COPYRIGHT NOTICE AND THIS PERMISSION NOTICE APPEAR IN ALL COPIES.
15
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
16
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
17
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
18
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
20
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
21
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
25
from __future__ import print_function
27
from __future__ import absolute_import
9
39
USAGE = '''passmass host1 host2 host3 . . .'''
10
40
COMMAND_PROMPT = '[$#] '
11
41
TERMINAL_PROMPT = r'Terminal type\?'
21
51
i = child.expect([pexpect.TIMEOUT, SSH_NEWKEY, '[Pp]assword: '])
22
52
if i == 0: # Timeout
24
print 'SSH could not login. Here is what SSH said:'
25
print child.before, child.after
54
print('SSH could not login. Here is what SSH said:')
55
print(child.before, child.after)
27
57
if i == 1: # SSH does not have the public key. Just accept it.
28
58
child.sendline ('yes')
32
62
# the login process is asking for our terminal type.
33
63
i = child.expect (['Permission denied', TERMINAL_PROMPT, COMMAND_PROMPT])
35
print 'Permission denied on host:', host
65
print('Permission denied on host:', host)
38
68
child.sendline (TERMINAL_TYPE)
42
72
# (current) UNIX password:
43
73
def change_password(child, user, oldpassword, newpassword):
45
child.sendline('passwd')
75
child.sendline('passwd')
46
76
i = child.expect(['[Oo]ld [Pp]assword', '.current.*password', '[Nn]ew [Pp]assword'])
47
77
# Root does not require old password, so it gets to bypass the next step.
48
78
if i == 0 or i == 1:
51
81
child.sendline(newpassword)
52
82
i = child.expect(['[Nn]ew [Pp]assword', '[Rr]etype', '[Rr]e-enter'])
54
print 'Host did not like new password. Here is what it said...'
56
child.send (chr(3)) # Ctrl-C
84
print('Host did not like new password. Here is what it said...')
86
child.send (chr(3)) # Ctrl-C
57
87
child.sendline('') # This should tell remote passwd command to quit.
59
89
child.sendline(newpassword)
69
99
newpassword = getpass.getpass('New Password: ')
70
100
newpasswordconfirm = getpass.getpass('Confirm New Password: ')
71
101
if newpassword != newpasswordconfirm:
72
print 'New Passwords do not match.'
102
print('New Passwords do not match.')
75
105
for host in sys.argv[1:]:
76
106
child = login(host, user, password)
78
print 'Could not login to host:', host
108
print('Could not login to host:', host)
80
print 'Changing password on host:', host
110
print('Changing password on host:', host)
81
111
change_password(child, user, password, newpassword)
82
112
child.expect(COMMAND_PROMPT)
83
113
child.sendline('exit')