4 # Greatly modified by Nigel W. Moriarty
16 "Session with extra state including the password to be used."
18 def __init__(self, user, host, password=None, verbose=0):
22 self.verbose = verbose
23 self.password = password
32 self.f = open('ssh.out', 'w')
36 outl = 'class :' + self.__class__.__name__
37 for attr in self.__dict__:
38 if attr == 'password':
39 outl += '\n\t' + attr + ' : ' + '*' * len(self.password)
41 outl += '\n\t' + attr + ' : ' + str(getattr(self, attr))
44 def __exec(self, command):
45 "Execute a command on the remote host. Return the output."
46 child = spawn(command,
50 sys.stderr.write("-> " + command + "\n")
51 seen = child.expect(self.keys)
52 self.f.write(str(child.before) + str(child.after) + '\n')
55 seen = child.expect(self.keys)
58 self.password = getpass.getpass('Remote password: ')
59 child.sendline(self.password)
62 # Added to allow the background running of remote process
63 if not child.isalive():
64 seen = child.expect(self.keys)
66 lines = child.readlines()
69 sys.stderr.write("<- " + child.before + "|\n")
71 self.f.write(str(child.before) + str(child.after) + '\n')
77 def ssh(self, command):
79 return self.__exec("ssh -l %s %s \"%s\""
80 % (self.user, self.host, command))
82 def scp(self, src, dst):
84 return self.__exec("scp %s %s@%s:%s"
85 % (src, session.user, session.host, dst))
87 def exists(self, file):
88 "Retrieve file permissions of specified remote file."
89 seen = self.ssh("/bin/ls -ld %s" % file)
90 if string.find(seen, "No such file") > -1:
91 return None # File doesn't exist
93 return seen.split()[0] # Return permission field of listing.