OLD | NEW |
(Empty) | |
| 1 #!/usr/bin/env python |
| 2 |
| 3 '''Change passwords on the named machines. passmass host1 host2 host3 . . . |
| 4 Note that login shell prompt on remote machine must end in # or $. |
| 5 |
| 6 PEXPECT LICENSE |
| 7 |
| 8 This license is approved by the OSI and FSF as GPL-compatible. |
| 9 http://opensource.org/licenses/isc-license.txt |
| 10 |
| 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. |
| 22 |
| 23 ''' |
| 24 |
| 25 from __future__ import print_function |
| 26 |
| 27 from __future__ import absolute_import |
| 28 |
| 29 import pexpect |
| 30 import sys, getpass |
| 31 |
| 32 |
| 33 try: |
| 34 raw_input |
| 35 except NameError: |
| 36 raw_input = input |
| 37 |
| 38 |
| 39 USAGE = '''passmass host1 host2 host3 . . .''' |
| 40 COMMAND_PROMPT = '[$#] ' |
| 41 TERMINAL_PROMPT = r'Terminal type\?' |
| 42 TERMINAL_TYPE = 'vt100' |
| 43 SSH_NEWKEY = r'Are you sure you want to continue connecting \(yes/no\)\?' |
| 44 |
| 45 def login(host, user, password): |
| 46 |
| 47 child = pexpect.spawn('ssh -l %s %s'%(user, host)) |
| 48 fout = file ("LOG.TXT","wb") |
| 49 child.setlog (fout) |
| 50 |
| 51 i = child.expect([pexpect.TIMEOUT, SSH_NEWKEY, '[Pp]assword: ']) |
| 52 if i == 0: # Timeout |
| 53 print('ERROR!') |
| 54 print('SSH could not login. Here is what SSH said:') |
| 55 print(child.before, child.after) |
| 56 sys.exit (1) |
| 57 if i == 1: # SSH does not have the public key. Just accept it. |
| 58 child.sendline ('yes') |
| 59 child.expect ('[Pp]assword: ') |
| 60 child.sendline(password) |
| 61 # Now we are either at the command prompt or |
| 62 # the login process is asking for our terminal type. |
| 63 i = child.expect (['Permission denied', TERMINAL_PROMPT, COMMAND_PROMPT]) |
| 64 if i == 0: |
| 65 print('Permission denied on host:', host) |
| 66 sys.exit (1) |
| 67 if i == 1: |
| 68 child.sendline (TERMINAL_TYPE) |
| 69 child.expect (COMMAND_PROMPT) |
| 70 return child |
| 71 |
| 72 # (current) UNIX password: |
| 73 def change_password(child, user, oldpassword, newpassword): |
| 74 |
| 75 child.sendline('passwd') |
| 76 i = child.expect(['[Oo]ld [Pp]assword', '.current.*password', '[Nn]ew [Pp]as
sword']) |
| 77 # Root does not require old password, so it gets to bypass the next step. |
| 78 if i == 0 or i == 1: |
| 79 child.sendline(oldpassword) |
| 80 child.expect('[Nn]ew [Pp]assword') |
| 81 child.sendline(newpassword) |
| 82 i = child.expect(['[Nn]ew [Pp]assword', '[Rr]etype', '[Rr]e-enter']) |
| 83 if i == 0: |
| 84 print('Host did not like new password. Here is what it said...') |
| 85 print(child.before) |
| 86 child.send (chr(3)) # Ctrl-C |
| 87 child.sendline('') # This should tell remote passwd command to quit. |
| 88 return |
| 89 child.sendline(newpassword) |
| 90 |
| 91 def main(): |
| 92 |
| 93 if len(sys.argv) <= 1: |
| 94 print(USAGE) |
| 95 return 1 |
| 96 |
| 97 user = raw_input('Username: ') |
| 98 password = getpass.getpass('Current Password: ') |
| 99 newpassword = getpass.getpass('New Password: ') |
| 100 newpasswordconfirm = getpass.getpass('Confirm New Password: ') |
| 101 if newpassword != newpasswordconfirm: |
| 102 print('New Passwords do not match.') |
| 103 return 1 |
| 104 |
| 105 for host in sys.argv[1:]: |
| 106 child = login(host, user, password) |
| 107 if child == None: |
| 108 print('Could not login to host:', host) |
| 109 continue |
| 110 print('Changing password on host:', host) |
| 111 change_password(child, user, password, newpassword) |
| 112 child.expect(COMMAND_PROMPT) |
| 113 child.sendline('exit') |
| 114 |
| 115 if __name__ == '__main__': |
| 116 main() |
OLD | NEW |