4 # Greatly modified by Nigel W. Moriarty
14 "Session with extra state including the password to be used."
16 def __init__(self, user, host, password=None, verbose=0):
20 self.verbose = verbose
21 self.password = password
30 self.f = open('ssh.out','w')
34 outl = 'class :'+self.__class__.__name__
35 for attr in self.__dict__:
36 if attr == 'password':
37 outl += '\n\t'+attr+' : '+'*'*len(self.password)
39 outl += '\n\t'+attr+' : '+str(getattr(self, attr))
42 def __exec(self, command):
44 "Execute a command on the remote host. Return the output."
45 child = spawn(command,
49 sys.stderr.write("-> " + command + "\n")
50 seen = child.expect(self.keys)
51 self.f.write(str(child.before) + str(child.after)+'\n')
54 seen = child.expect(self.keys)
57 self.password = getpass.getpass('Remote password: ')
58 child.sendline(self.password)
61 # Added to allow the background running of remote process
62 if not child.isalive():
63 seen = child.expect(self.keys)
65 lines = child.readlines()
68 sys.stderr.write("<- " + child.before + "|\n")
70 self.f.write(str(child.before) + str(child.after)+'\n')
76 def ssh(self, command):
78 return self.__exec("ssh -l %s %s \"%s\"" \
79 % (self.user,self.host,command))
81 def scp(self, src, dst):
83 return self.__exec("scp %s %s@%s:%s" \
84 % (src, session.user, session.host, dst))
86 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.