1 | #
|
---|
2 | # Eric S. Raymond
|
---|
3 | #
|
---|
4 | # Greatly modified by Nigel W. Moriarty
|
---|
5 | # April 2003
|
---|
6 | #
|
---|
7 | from pexpect import *
|
---|
8 | import os, sys
|
---|
9 | import getpass
|
---|
10 | import time, string
|
---|
11 |
|
---|
12 | class ssh_session:
|
---|
13 | "Session with extra state including the password to be used."
|
---|
14 | def __init__(self, user, host, password=None, verbose=0):
|
---|
15 | self.user = user
|
---|
16 | self.host = host
|
---|
17 | self.verbose = verbose
|
---|
18 | self.password = password
|
---|
19 | self.keys = [
|
---|
20 | 'authenticity',
|
---|
21 | ' password: ',
|
---|
22 | '@@@@@@@@@@@@',
|
---|
23 | 'Command not found.',
|
---|
24 | EOF,
|
---|
25 | ]
|
---|
26 | #self.f = open('ssh.out','w+')
|
---|
27 |
|
---|
28 | def __del__(self):
|
---|
29 | pass
|
---|
30 | #self.f.close()
|
---|
31 |
|
---|
32 | def __repr__(self):
|
---|
33 | outl = 'class :'+self.__class__.__name__
|
---|
34 | for attr in self.__dict__:
|
---|
35 | if attr == 'password':
|
---|
36 | outl += '\n\t'+attr+' : '+'*'*len(self.password)
|
---|
37 | else:
|
---|
38 | outl += '\n\t'+attr+' : '+str(getattr(self, attr))
|
---|
39 | return outl
|
---|
40 |
|
---|
41 | def __exec(self, command):
|
---|
42 | "Execute a command on the remote host. Return the output."
|
---|
43 | child = spawn(command, timeout=5000)
|
---|
44 | #child.timeout = 5000
|
---|
45 | if self.verbose:
|
---|
46 | print "-> " + command + "\n"
|
---|
47 | #sys.stderr.write("-> " + command + "\n")
|
---|
48 | seen = child.expect(self.keys)
|
---|
49 | #self.f.write(str(child.before) + str(child.after)+'\n')
|
---|
50 | if seen == 0:
|
---|
51 | child.sendline('yes')
|
---|
52 | seen = child.expect(self.keys)
|
---|
53 | if seen == 1:
|
---|
54 | if not self.password:
|
---|
55 | self.password = getpass.getpass(self.user+'@'+self.host+"'s password:")
|
---|
56 | child.sendline(self.password)
|
---|
57 | child.readline()
|
---|
58 | for line in child:
|
---|
59 | print string.replace(line, "\n", "")
|
---|
60 | time.sleep(5)
|
---|
61 | # Added to allow the background running of remote process
|
---|
62 | if not child.isalive():
|
---|
63 | seen = child.expect(self.keys)
|
---|
64 | if seen == 2:
|
---|
65 | lines = child.readlines()
|
---|
66 | #self.f.write(lines)
|
---|
67 | if self.verbose:
|
---|
68 | pass
|
---|
69 | print "<- " + child.before +"|\n"
|
---|
70 | try:
|
---|
71 | pass
|
---|
72 | #self.f.write(str(child.before) + str(child.after)+'\n')
|
---|
73 | except:
|
---|
74 | pass
|
---|
75 | return child.before
|
---|
76 |
|
---|
77 | def ssh(self, command):
|
---|
78 | return self.__exec("ssh -l %s %s \"%s\"" \
|
---|
79 | % (self.user,self.host,command))
|
---|
80 |
|
---|
81 | def scp(self, src, dst, option=None):
|
---|
82 | if option:
|
---|
83 | return self.__exec("scp -"+option+" %s %s@%s:%s" \
|
---|
84 | % (src, self.user, self.host, dst))
|
---|
85 |
|
---|
86 | return self.__exec("scp %s %s@%s:%s" \
|
---|
87 | % (src, self.user, self.host, dst))
|
---|
88 |
|
---|
89 | def scpget(self, src, dst, option=None):
|
---|
90 | if option:
|
---|
91 | return self.__exec("scp -"+option+" %s@%s:%s %s " \
|
---|
92 | % (self.user, self.host, src, dst))
|
---|
93 |
|
---|
94 | return self.__exec("scp %s@%s:%s %s " \
|
---|
95 | % (self.user, self.host, src, dst))
|
---|
96 |
|
---|
97 |
|
---|
98 | def exists(self, file):
|
---|
99 | "Retrieve file permissions of specified remote file."
|
---|
100 | seen = self.ssh("/bin/ls -ld %s" % file)
|
---|
101 | if string.find(seen, "No such file") > -1:
|
---|
102 | return None # File doesn't exist
|
---|
103 | else:
|
---|
104 | return seen.split()[0] # Return permission field of listing.
|
---|