summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authornoah <noah@656d521f-e311-0410-88e0-e7920216d269>2002-11-27 19:59:49 +0000
committernoah <noah@656d521f-e311-0410-88e0-e7920216d269>2002-11-27 19:59:49 +0000
commitd206f9e77f26f275350dd7732441fe853368622a (patch)
tree847b644d614ed4cb89f5b3935dcffd7c03f12b1a
parent23dcd1db08b1512368df9b85c42b23ab4d99e057 (diff)
downloadpexpect-d206f9e77f26f275350dd7732441fe853368622a.tar.gz
An example by Eric S. Raymond... Not working yet.
git-svn-id: http://pexpect.svn.sourceforge.net/svnroot/pexpect/trunk@121 656d521f-e311-0410-88e0-e7920216d269
-rw-r--r--pexpect/examples/ssh_session.py37
1 files changed, 37 insertions, 0 deletions
diff --git a/pexpect/examples/ssh_session.py b/pexpect/examples/ssh_session.py
new file mode 100644
index 0000000..5e05298
--- /dev/null
+++ b/pexpect/examples/ssh_session.py
@@ -0,0 +1,37 @@
+#
+# Eric S. Raymond
+#
+from pexpect import *
+
+class ssh_session:
+ "Session with extra state including the password to be used."
+ def __init__(self, user, host, password=None, verbose=0):
+ self.user = user
+ self.host = host
+ self.verbose = verbose
+ self.password = password
+ def __exec(self, command):
+ "Execute a command on the remote host. Return the output."
+ child = spawn(command)
+ if self.verbose:
+ sys.stderr.write("-> " + command + "\n")
+ seen = child.expect(['assword:', EOF])
+ if seen == 0:
+ if not self.password:
+ self.password = getpass.getpass('Remote password: ')
+ child.sendline(self.password)
+ seen = child.expect(EOF)
+ if self.verbose:
+ sys.stderr.write("<- " + child.before + "\n")
+ return child.before
+ def ssh(self, command):
+ return self.__exec("ssh -l %s %s \"%s\""%(self.user,self.host,command))
+ def scp(self, src, dst):
+ return self.__exec("scp %s %s@%s:%s" % (src, session.user, session.host, dst))
+ def exists(self, file):
+ "Retrieve file permissions of specified remote file."
+ seen = self.ssh("/bin/ls -ld %s" % file)
+ if string.find(seen, "No such file") > -1:
+ return None # File doesn't exist
+ else:
+ return seen.split()[0] # Return permission field of listing.