]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - third_party/Python/module/pexpect-2.4/examples/ssh_session.py
Vendor import of lldb trunk r290819:
[FreeBSD/FreeBSD.git] / third_party / Python / module / pexpect-2.4 / examples / ssh_session.py
1 #
2 # Eric S. Raymond
3 #
4 # Greatly modified by Nigel W. Moriarty
5 # April 2003
6 #
7 from pexpect import *
8 import os
9 import sys
10 import getpass
11 import time
12
13
14 class ssh_session:
15
16     "Session with extra state including the password to be used."
17
18     def __init__(self, user, host, password=None, verbose=0):
19
20         self.user = user
21         self.host = host
22         self.verbose = verbose
23         self.password = password
24         self.keys = [
25             'authenticity',
26             'assword:',
27             '@@@@@@@@@@@@',
28             'Command not found.',
29             EOF,
30         ]
31
32         self.f = open('ssh.out', 'w')
33
34     def __repr__(self):
35
36         outl = 'class :' + self.__class__.__name__
37         for attr in self.__dict__:
38             if attr == 'password':
39                 outl += '\n\t' + attr + ' : ' + '*' * len(self.password)
40             else:
41                 outl += '\n\t' + attr + ' : ' + str(getattr(self, attr))
42         return outl
43
44     def __exec(self, command):
45         "Execute a command on the remote host.    Return the output."
46         child = spawn(command,
47                       # timeout=10,
48                       )
49         if self.verbose:
50             sys.stderr.write("-> " + command + "\n")
51         seen = child.expect(self.keys)
52         self.f.write(str(child.before) + str(child.after) + '\n')
53         if seen == 0:
54             child.sendline('yes')
55             seen = child.expect(self.keys)
56         if seen == 1:
57             if not self.password:
58                 self.password = getpass.getpass('Remote password: ')
59             child.sendline(self.password)
60             child.readline()
61             time.sleep(5)
62             # Added to allow the background running of remote process
63             if not child.isalive():
64                 seen = child.expect(self.keys)
65         if seen == 2:
66             lines = child.readlines()
67             self.f.write(lines)
68         if self.verbose:
69             sys.stderr.write("<- " + child.before + "|\n")
70         try:
71             self.f.write(str(child.before) + str(child.after) + '\n')
72         except:
73             pass
74         self.f.close()
75         return child.before
76
77     def ssh(self, command):
78
79         return self.__exec("ssh -l %s %s \"%s\""
80                            % (self.user, self.host, command))
81
82     def scp(self, src, dst):
83
84         return self.__exec("scp %s %s@%s:%s"
85                            % (src, session.user, session.host, dst))
86
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
92         else:
93             return seen.split()[0]  # Return permission field of listing.