~ubuntu-branches/debian/sid/pexpect/sid

« back to all changes in this revision

Viewing changes to examples/ssh_session.py

  • Committer: Package Import Robot
  • Author(s): Jackson Doak
  • Date: 2014-01-26 11:22:03 UTC
  • mfrom: (1.1.5)
  • Revision ID: package-import@ubuntu.com-20140126112203-g1rvj001b7cxpyat
Tags: 3.1-1
* Team upload.
* New upstream release
* debian/control: 
  - In -doc, add breaks/replaces on python-pexcept <<3. Closes: #732915
  - Drop python-pexcept-doc to Suggests in both other packages. 
    Closes: #732889
  - Bump standards-version to 3.9.5 (no changes)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#!/usr/bin/env python
2
 
 
3
 
'''
4
 
 Eric S. Raymond
5
 
 
6
 
 Greatly modified by Nigel W. Moriarty
7
 
 April 2003
8
 
 
9
 
PEXPECT LICENSE
10
 
 
11
 
    This license is approved by the OSI and FSF as GPL-compatible.
12
 
        http://opensource.org/licenses/isc-license.txt
13
 
 
14
 
    Copyright (c) 2012, Noah Spurrier <noah@noah.org>
15
 
    PERMISSION TO USE, COPY, MODIFY, AND/OR DISTRIBUTE THIS SOFTWARE FOR ANY
16
 
    PURPOSE WITH OR WITHOUT FEE IS HEREBY GRANTED, PROVIDED THAT THE ABOVE
17
 
    COPYRIGHT NOTICE AND THIS PERMISSION NOTICE APPEAR IN ALL COPIES.
18
 
    THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
19
 
    WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
20
 
    MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
21
 
    ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
22
 
    WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
23
 
    ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
24
 
    OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
25
 
 
26
 
'''
27
 
 
28
 
from __future__ import absolute_import
29
 
 
30
 
from pexpect import *
31
 
import os, sys
32
 
import getpass
33
 
import time
34
 
 
35
 
 
36
 
class ssh_session:
37
 
 
38
 
    '''Session with extra state including the password to be used.'''
39
 
 
40
 
    def __init__(self, user, host, password=None, verbose=0):
41
 
 
42
 
        self.user = user
43
 
        self.host = host
44
 
        self.verbose = verbose
45
 
        self.password = password
46
 
        self.keys = [
47
 
            'authenticity',
48
 
            'assword:',
49
 
            '@@@@@@@@@@@@',
50
 
            'Command not found.',
51
 
            EOF,
52
 
            ]
53
 
 
54
 
        self.f = open('ssh.out','w')
55
 
 
56
 
    def __repr__(self):
57
 
 
58
 
        outl = 'class :'+self.__class__.__name__
59
 
        for attr in self.__dict__:
60
 
            if attr == 'password':
61
 
                outl += '\n\t'+attr+' : '+'*'*len(self.password)
62
 
            else:
63
 
                outl += '\n\t'+attr+' : '+str(getattr(self, attr))
64
 
        return outl
65
 
 
66
 
    def __exec(self, command):
67
 
 
68
 
        '''Execute a command on the remote host. Return the output.'''
69
 
 
70
 
        child = spawn(command,
71
 
                                    #timeout=10,
72
 
                                    )
73
 
        if self.verbose:
74
 
            sys.stderr.write("-> " + command + "\n")
75
 
        seen = child.expect(self.keys)
76
 
        self.f.write(str(child.before) + str(child.after)+'\n')
77
 
        if seen == 0:
78
 
            child.sendline('yes')
79
 
            seen = child.expect(self.keys)
80
 
        if seen == 1:
81
 
            if not self.password:
82
 
                self.password = getpass.getpass('Remote password: ')
83
 
            child.sendline(self.password)
84
 
            child.readline()
85
 
            time.sleep(5)
86
 
            # Added to allow the background running of remote process
87
 
            if not child.isalive():
88
 
                seen = child.expect(self.keys)
89
 
        if seen == 2:
90
 
            lines = child.readlines()
91
 
            self.f.write(lines)
92
 
        if self.verbose:
93
 
            sys.stderr.write("<- " + child.before + "|\n")
94
 
        try:
95
 
            self.f.write(str(child.before) + str(child.after)+'\n')
96
 
        except:
97
 
            pass
98
 
        self.f.close()
99
 
        return child.before
100
 
 
101
 
    def ssh(self, command):
102
 
 
103
 
        return self.__exec("ssh -l %s %s \"%s\"" \
104
 
                                             % (self.user,self.host,command))
105
 
 
106
 
    def scp(self, src, dst):
107
 
 
108
 
        return self.__exec("scp %s %s@%s:%s" \
109
 
                                             % (src, session.user, session.host, dst))
110
 
 
111
 
    def exists(self, file):
112
 
 
113
 
        '''Retrieve file permissions of specified remote file.'''
114
 
 
115
 
        seen = self.ssh("/bin/ls -ld %s" % file)
116
 
        if string.find(seen, "No such file") > -1:
117
 
            return None # File doesn't exist
118
 
        else:
119
 
            return seen.split()[0] # Return permission field of listing.
120