6
Greatly modified by Nigel W. Moriarty
11
This license is approved by the OSI and FSF as GPL-compatible.
12
http://opensource.org/licenses/isc-license.txt
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.
28
from __future__ import absolute_import
38
'''Session with extra state including the password to be used.'''
40
def __init__(self, user, host, password=None, verbose=0):
44
self.verbose = verbose
45
self.password = password
54
self.f = open('ssh.out','w')
58
outl = 'class :'+self.__class__.__name__
59
for attr in self.__dict__:
60
if attr == 'password':
61
outl += '\n\t'+attr+' : '+'*'*len(self.password)
63
outl += '\n\t'+attr+' : '+str(getattr(self, attr))
66
def __exec(self, command):
68
'''Execute a command on the remote host. Return the output.'''
70
child = spawn(command,
74
sys.stderr.write("-> " + command + "\n")
75
seen = child.expect(self.keys)
76
self.f.write(str(child.before) + str(child.after)+'\n')
79
seen = child.expect(self.keys)
82
self.password = getpass.getpass('Remote password: ')
83
child.sendline(self.password)
86
# Added to allow the background running of remote process
87
if not child.isalive():
88
seen = child.expect(self.keys)
90
lines = child.readlines()
93
sys.stderr.write("<- " + child.before + "|\n")
95
self.f.write(str(child.before) + str(child.after)+'\n')
101
def ssh(self, command):
103
return self.__exec("ssh -l %s %s \"%s\"" \
104
% (self.user,self.host,command))
106
def scp(self, src, dst):
108
return self.__exec("scp %s %s@%s:%s" \
109
% (src, session.user, session.host, dst))
111
def exists(self, file):
113
'''Retrieve file permissions of specified remote file.'''
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
119
return seen.split()[0] # Return permission field of listing.