3 """Change passwords on the named machines. passmass host1 host2 host3 . . .
4 Note that login shell prompt on remote machine must end in # or $. """
9 USAGE = '''passmass host1 host2 host3 . . .'''
10 COMMAND_PROMPT = '[$#] '
11 TERMINAL_PROMPT = r'Terminal type\?'
12 TERMINAL_TYPE = 'vt100'
13 SSH_NEWKEY = r'Are you sure you want to continue connecting \(yes/no\)\?'
15 def login(host, user, password):
17 child = pexpect.spawn('ssh -l %s %s'%(user, host))
18 fout = file ("LOG.TXT","wb")
21 i = child.expect([pexpect.TIMEOUT, SSH_NEWKEY, '[Pp]assword: '])
24 print 'SSH could not login. Here is what SSH said:'
25 print child.before, child.after
27 if i == 1: # SSH does not have the public key. Just accept it.
28 child.sendline ('yes')
29 child.expect ('[Pp]assword: ')
30 child.sendline(password)
31 # Now we are either at the command prompt or
32 # the login process is asking for our terminal type.
33 i = child.expect (['Permission denied', TERMINAL_PROMPT, COMMAND_PROMPT])
35 print 'Permission denied on host:', host
38 child.sendline (TERMINAL_TYPE)
39 child.expect (COMMAND_PROMPT)
42 # (current) UNIX password:
43 def change_password(child, user, oldpassword, newpassword):
45 child.sendline('passwd')
46 i = child.expect(['[Oo]ld [Pp]assword', '.current.*password', '[Nn]ew [Pp]assword'])
47 # Root does not require old password, so it gets to bypass the next step.
49 child.sendline(oldpassword)
50 child.expect('[Nn]ew [Pp]assword')
51 child.sendline(newpassword)
52 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
57 child.sendline('') # This should tell remote passwd command to quit.
59 child.sendline(newpassword)
63 if len(sys.argv) <= 1:
67 user = raw_input('Username: ')
68 password = getpass.getpass('Current Password: ')
69 newpassword = getpass.getpass('New Password: ')
70 newpasswordconfirm = getpass.getpass('Confirm New Password: ')
71 if newpassword != newpasswordconfirm:
72 print 'New Passwords do not match.'
75 for host in sys.argv[1:]:
76 child = login(host, user, password)
78 print 'Could not login to host:', host
80 print 'Changing password on host:', host
81 change_password(child, user, password, newpassword)
82 child.expect(COMMAND_PROMPT)
83 child.sendline('exit')
85 if __name__ == '__main__':
88 except pexpect.ExceptionPexpect, e: