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

« back to all changes in this revision

Viewing changes to examples/sshls.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
 
"""This runs 'ls -l' on a remote host using SSH. At the prompts enter hostname,
4
 
user, and password.
5
 
 
6
 
$Id: sshls.py 489 2007-11-28 23:40:34Z noah $
7
 
"""
 
3
'''This runs 'ls -l' on a remote host using SSH.
 
4
At the prompts enter hostname, username, and password.
 
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
8
28
 
9
29
import pexpect
10
30
import getpass, os
11
31
 
 
32
 
 
33
try:
 
34
    raw_input
 
35
except NameError:
 
36
    raw_input = input
 
37
 
 
38
 
12
39
def ssh_command (user, host, password, command):
13
40
 
14
 
    """This runs a command on the remote host. This could also be done with the
15
 
pxssh class, but this demonstrates what that class does at a simpler level.
16
 
This returns a pexpect.spawn object. This handles the case when you try to
17
 
connect to a new host and ssh asks you if you want to accept the public key
18
 
fingerprint and continue connecting. """
 
41
    '''This runs a command on the remote host. This could also be done with the
 
42
    pxssh class, but this demonstrates what that class does at a simpler level.
 
43
    This returns a pexpect.spawn object. This handles the case when you try to
 
44
    connect to a new host and ssh asks you if you want to accept the public key
 
45
    fingerprint and continue connecting. '''
19
46
 
20
47
    ssh_newkey = 'Are you sure you want to continue connecting'
21
48
    child = pexpect.spawn('ssh -l %s %s %s'%(user, host, command))
22
49
    i = child.expect([pexpect.TIMEOUT, ssh_newkey, 'password: '])
23
50
    if i == 0: # Timeout
24
 
        print 'ERROR!'
25
 
        print 'SSH could not login. Here is what SSH said:'
26
 
        print child.before, child.after
 
51
        print('ERROR!')
 
52
        print('SSH could not login. Here is what SSH said:')
 
53
        print(child.before, child.after)
27
54
        return None
28
55
    if i == 1: # SSH does not have the public key. Just accept it.
29
56
        child.sendline ('yes')
30
57
        child.expect ('password: ')
31
58
        i = child.expect([pexpect.TIMEOUT, 'password: '])
32
59
        if i == 0: # Timeout
33
 
            print 'ERROR!'
34
 
            print 'SSH could not login. Here is what SSH said:'
35
 
            print child.before, child.after
36
 
            return None       
 
60
            print('ERROR!')
 
61
            print('SSH could not login. Here is what SSH said:')
 
62
            print(child.before, child.after)
 
63
            return None
37
64
    child.sendline(password)
38
65
    return child
39
66
 
44
71
    password = getpass.getpass('Password: ')
45
72
    child = ssh_command (user, host, password, '/bin/ls -l')
46
73
    child.expect(pexpect.EOF)
47
 
    print child.before
 
74
    print(child.before)
48
75
 
49
76
if __name__ == '__main__':
 
77
 
50
78
    try:
51
79
        main()
52
 
    except Exception, e:
53
 
        print str(e)
 
80
    except Exception as e:
 
81
        print(str(e))
54
82
        traceback.print_exc()
55
83
        os._exit(1)
56
84