~ubuntu-branches/ubuntu/trusty/pexpect/trusty-proposed

« back to all changes in this revision

Viewing changes to examples/passmass.py

  • Committer: Package Import Robot
  • Author(s): Andrew Starr-Bochicchio, Thomas Kluyver, Jakub Wilk, Jackson Doak, Andrew Starr-Bochicchio
  • Date: 2013-12-06 20:20:26 UTC
  • mfrom: (1.1.4)
  • Revision ID: package-import@ubuntu.com-20131206202026-9k9oixbv7e8ke30q
Tags: 3.0-1
* Team upload.

[ Thomas Kluyver ]
* New upstream release. Closes: #729518
* Add packaging for Python 3.
* Use pybuild for packaging.

[ Jakub Wilk ]
* Use canonical URIs for Vcs-* fields.

[ Jackson Doak ]
* Create debian/python3-pexpect.docs

[ Andrew Starr-Bochicchio ]
* Remove empty debian/patches dir.
* Move documentation and examples into a new python-pexpect-doc
  package. They are shared between the Python 2 and Python 3 packages,
  so there is no need to install them with both.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
#!/usr/bin/env python
2
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 $. """
 
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
5
28
 
6
29
import pexpect
7
30
import sys, getpass
8
31
 
 
32
 
 
33
try:
 
34
    raw_input
 
35
except NameError:
 
36
    raw_input = input
 
37
 
 
38
 
9
39
USAGE = '''passmass host1 host2 host3 . . .'''
10
40
COMMAND_PROMPT = '[$#] '
11
41
TERMINAL_PROMPT = r'Terminal type\?'
20
50
 
21
51
    i = child.expect([pexpect.TIMEOUT, SSH_NEWKEY, '[Pp]assword: '])
22
52
    if i == 0: # Timeout
23
 
        print 'ERROR!'
24
 
        print 'SSH could not login. Here is what SSH said:'
25
 
        print child.before, child.after
 
53
        print('ERROR!')
 
54
        print('SSH could not login. Here is what SSH said:')
 
55
        print(child.before, child.after)
26
56
        sys.exit (1)
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])
34
64
    if i == 0:
35
 
        print 'Permission denied on host:', host
 
65
        print('Permission denied on host:', host)
36
66
        sys.exit (1)
37
67
    if i == 1:
38
68
        child.sendline (TERMINAL_TYPE)
42
72
# (current) UNIX password:
43
73
def change_password(child, user, oldpassword, newpassword):
44
74
 
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'])
53
83
    if i == 0:
54
 
        print 'Host did not like new password. Here is what it said...'
55
 
        print child.before
56
 
        child.send (chr(3)) # Ctrl-C
 
84
        print('Host did not like new password. Here is what it said...')
 
85
        print(child.before)
 
86
        child.send (chr(3)) # Ctrl-C
57
87
        child.sendline('') # This should tell remote passwd command to quit.
58
88
        return
59
89
    child.sendline(newpassword)
61
91
def main():
62
92
 
63
93
    if len(sys.argv) <= 1:
64
 
        print USAGE
 
94
        print(USAGE)
65
95
        return 1
66
96
 
67
97
    user = raw_input('Username: ')
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.')
73
103
        return 1
74
104
 
75
105
    for host in sys.argv[1:]:
76
106
        child = login(host, user, password)
77
107
        if child == None:
78
 
            print 'Could not login to host:', host
 
108
            print('Could not login to host:', host)
79
109
            continue
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')
85
115
if __name__ == '__main__':
86
116
    try:
87
117
        main()
88
 
    except pexpect.ExceptionPexpect, e:
89
 
        print str(e)
90
 
 
 
118
    except pexpect.ExceptionPexpect as e:
 
119
        print(str(e))