-
Notifications
You must be signed in to change notification settings - Fork 0
/
remote.py
executable file
·149 lines (120 loc) · 4.56 KB
/
remote.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#!/usr/bin/env python2
## Load the basic modules we want.
import re, os, sys, time, commands, getopt, types
## This is the Expect module.
import pexpect
import getpass
#-------------------------------------------
## Initialize the module.
class Ssh:
def __init__(self):
## Three responses we might expect.
self.Initial_Responses = ['Are you sure',
'password:', pexpect.EOF]
#-------------------------------------------
def Ssh_Method1(self, User="", Password="",
Host="", Timeout=120, Response='', Command="",
Opposite_Match = 0):
if Command == '': return (-1, '')
elif Response == '': return (-2, '')
## Execute the command.
child = pexpect.spawn(Command)
## Get the first response.
i = child.expect (self.Initial_Responses, Timeout)
## The first reponse is to accept the key.
if i==0:
T = child.read(100)
child.sendline("yes")
child.expect('password:', Timeout)
child.sendline(Password)
## The second response sends the password.
elif i == 1:
child.sendline(Password)
## Otherwise, there is an error.
else:
Str1 = str(child.before)
return (-3, 'ERROR: Unknown1: ' + Str1)
## Get the next response.
Possible_Responses = ['password:', pexpect.EOF]
i = child.expect (Possible_Responses, Timeout)
## If it asks for a password, error.
if i == 0:
return (-4, 'ERROR: Incorrect password.')
## Otherwise we are okay.
else:
## Get the output before the match.
Output = str(child.before)
## Compile a regular expression.
RE1 = re.compile(Response)
## If OM = 1, it means "don't match".
OM = Opposite_Match
## If we detecting a positive match and
## it works, return good.
if (OM == 0) and (RE1.search(Output)):
return (0, Output)
## If we are trying not to detect a match
## and it doesn't match, return good.
elif (OM != 0) and (not RE1.search(Output)):
return (0, Output)
else :
## Otherwise, there is an error.
return (-6, str(child.after))
#--------------------------------------------
## This method detects if a file exists.
def File_Test_Exist(self,Destination_File="",
User="", Password="", Host="", Timeout=120):
Command = """ssh -l %s %s 'if test -e %s ;\
then echo "GOOD: FILE EXISTS" ; fi'""" \
%(User, Host, Destination_File)
Response = 'GOOD: FILE EXISTS'
(Error, Message) = self.Ssh_Method1(User=User,
Password=Password, Host=Host, Timeout=Timeout
, Response=Response, Command=Command)
if Error == -5:
Message ="ERROR: file doesn't exist. "+Message
return (Error, Message)
#-------------------------------------------
## This transfers a file using rsync with ssh.
def Script_Transfer(self, Destination_Script="",
User="", Password="", Host="", Source_Script=""
, Timeout=120):
Command = """rsync -e ssh -av %s %s@%s:%s"""\
%(Source_Script, User, Host,
Destination_Script)
Response = 'rsync error:'
(Error, Message) = self.Ssh_Method1(User=User,
Password=Password, Host=Host, Timeout=Timeout
, Response=Response, Command=Command,
Opposite_Match=1)
return (Error, Message)
#---------------------------------------------
def Script_Execute(self, Destination_Script="",
User="", Password="", Host="", Timeout=120):
## Use local python if available.
Python_Command = "/usr/local/python/bin/python"
(Test1,Output) = self.File_Test_Exist(
Destination_File=Python_Command, User=User,
Password=Password, Host=Host)
if Test1 != 0: Python_Command = "python"
Command = """ssh -l %s %s '%s %s'""" %(User,
Host, Python_Command, Destination_Script)
Response = 'GOOD:'
(Error, Message) = self.Ssh_Method1(User=User,
Password=Password, Host=Host, Timeout=Timeout
, Response=Response, Command=Command)
return (Error, Message)
#-------------------------------------------
def Download_File(self, File_Client="",
File_Destination="", User="", Password="",
Host="", Timeout=3600):
Command = """rsync -av -e ssh %s@%s:%s %s""" %(
User, Host, File_Client, File_Destination)
Response = 'rsync error:'
## We set Opposite_Match to 1 because
## we don't want to match the reposnse.
(Error, Message) = self.Ssh_Method1( User=User,
Password=Password, Host=Host, Timeout=Timeout
, Response=Response, Command=Command,
Opposite_Match=1 )
return (Error, Message)
#-------------------------------------------