2011-03-12 04:13:06 +08:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
|
|
|
"""Change passwords on the named machines. passmass host1 host2 host3 . . .
|
|
|
|
Note that login shell prompt on remote machine must end in # or $. """
|
|
|
|
|
|
|
|
import pexpect
|
2016-09-07 04:57:50 +08:00
|
|
|
import sys
|
|
|
|
import getpass
|
2011-03-12 04:13:06 +08:00
|
|
|
|
|
|
|
USAGE = '''passmass host1 host2 host3 . . .'''
|
|
|
|
COMMAND_PROMPT = '[$#] '
|
|
|
|
TERMINAL_PROMPT = r'Terminal type\?'
|
|
|
|
TERMINAL_TYPE = 'vt100'
|
|
|
|
SSH_NEWKEY = r'Are you sure you want to continue connecting \(yes/no\)\?'
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2011-03-12 04:13:06 +08:00
|
|
|
def login(host, user, password):
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
child = pexpect.spawn('ssh -l %s %s' % (user, host))
|
|
|
|
fout = file("LOG.TXT", "wb")
|
|
|
|
child.setlog(fout)
|
2011-03-12 04:13:06 +08:00
|
|
|
|
|
|
|
i = child.expect([pexpect.TIMEOUT, SSH_NEWKEY, '[Pp]assword: '])
|
2016-09-07 04:57:50 +08:00
|
|
|
if i == 0: # Timeout
|
2011-03-12 04:13:06 +08:00
|
|
|
print 'ERROR!'
|
|
|
|
print 'SSH could not login. Here is what SSH said:'
|
|
|
|
print child.before, child.after
|
2016-09-07 04:57:50 +08:00
|
|
|
sys.exit(1)
|
|
|
|
if i == 1: # SSH does not have the public key. Just accept it.
|
|
|
|
child.sendline('yes')
|
|
|
|
child.expect('[Pp]assword: ')
|
2011-03-12 04:13:06 +08:00
|
|
|
child.sendline(password)
|
|
|
|
# Now we are either at the command prompt or
|
|
|
|
# the login process is asking for our terminal type.
|
2016-09-07 04:57:50 +08:00
|
|
|
i = child.expect(['Permission denied', TERMINAL_PROMPT, COMMAND_PROMPT])
|
2011-03-12 04:13:06 +08:00
|
|
|
if i == 0:
|
|
|
|
print 'Permission denied on host:', host
|
2016-09-07 04:57:50 +08:00
|
|
|
sys.exit(1)
|
2011-03-12 04:13:06 +08:00
|
|
|
if i == 1:
|
2016-09-07 04:57:50 +08:00
|
|
|
child.sendline(TERMINAL_TYPE)
|
|
|
|
child.expect(COMMAND_PROMPT)
|
2011-03-12 04:13:06 +08:00
|
|
|
return child
|
|
|
|
|
|
|
|
# (current) UNIX password:
|
2016-09-07 04:57:50 +08:00
|
|
|
|
|
|
|
|
2011-03-12 04:13:06 +08:00
|
|
|
def change_password(child, user, oldpassword, newpassword):
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
child.sendline('passwd')
|
|
|
|
i = child.expect(
|
|
|
|
['[Oo]ld [Pp]assword', '.current.*password', '[Nn]ew [Pp]assword'])
|
2011-03-12 04:13:06 +08:00
|
|
|
# Root does not require old password, so it gets to bypass the next step.
|
|
|
|
if i == 0 or i == 1:
|
|
|
|
child.sendline(oldpassword)
|
|
|
|
child.expect('[Nn]ew [Pp]assword')
|
|
|
|
child.sendline(newpassword)
|
|
|
|
i = child.expect(['[Nn]ew [Pp]assword', '[Rr]etype', '[Rr]e-enter'])
|
|
|
|
if i == 0:
|
|
|
|
print 'Host did not like new password. Here is what it said...'
|
|
|
|
print child.before
|
2016-09-07 04:57:50 +08:00
|
|
|
child.send(chr(3)) # Ctrl-C
|
|
|
|
child.sendline('') # This should tell remote passwd command to quit.
|
2011-03-12 04:13:06 +08:00
|
|
|
return
|
|
|
|
child.sendline(newpassword)
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2011-03-12 04:13:06 +08:00
|
|
|
def main():
|
|
|
|
|
|
|
|
if len(sys.argv) <= 1:
|
|
|
|
print USAGE
|
|
|
|
return 1
|
|
|
|
|
|
|
|
user = raw_input('Username: ')
|
|
|
|
password = getpass.getpass('Current Password: ')
|
|
|
|
newpassword = getpass.getpass('New Password: ')
|
|
|
|
newpasswordconfirm = getpass.getpass('Confirm New Password: ')
|
|
|
|
if newpassword != newpasswordconfirm:
|
|
|
|
print 'New Passwords do not match.'
|
|
|
|
return 1
|
|
|
|
|
|
|
|
for host in sys.argv[1:]:
|
|
|
|
child = login(host, user, password)
|
2016-09-07 04:57:50 +08:00
|
|
|
if child is None:
|
2011-03-12 04:13:06 +08:00
|
|
|
print 'Could not login to host:', host
|
|
|
|
continue
|
|
|
|
print 'Changing password on host:', host
|
|
|
|
change_password(child, user, password, newpassword)
|
|
|
|
child.expect(COMMAND_PROMPT)
|
|
|
|
child.sendline('exit')
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
try:
|
|
|
|
main()
|
2016-09-07 04:57:50 +08:00
|
|
|
except pexpect.ExceptionPexpect as e:
|
2011-03-12 04:13:06 +08:00
|
|
|
print str(e)
|