-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbashUtils.py
More file actions
130 lines (106 loc) · 3.69 KB
/
bashUtils.py
File metadata and controls
130 lines (106 loc) · 3.69 KB
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
from optparse import OptionParser
from signal import alarm, signal, SIGALRM, SIGKILL
from subprocess import PIPE, Popen
import logging
import os
import paramiko
import select
class wget(object):
def __init__(self, filename, url, path=None):
pass
def background(self, handler):
pass
class remoteSSHClient(object):
def __init__(self, host, port, user, passwd):
self.host = host
self.port = port
self.user = user
self.passwd = passwd
self.ssh = paramiko.SSHClient()
self.ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
try:
self.ssh.connect(str(host),int(port), user, passwd)
except paramiko.SSHException, sshex:
logging.debug(repr(sshex))
def execute(self, command):
stdin, stdout, stderr = self.ssh.exec_command(command)
output = stdout.readlines()
errors = stderr.readlines()
results = []
if output is not None and len(output) == 0:
if errors is not None and len(errors) > 0:
for error in errors:
results.append(error.rstrip())
else:
for strOut in output:
results.append(strOut.rstrip())
return results
def execute_buffered(self, command, bufsize=512):
transport = self.ssh.get_transport()
channel = transport.open_session()
try:
stdin, stdout, sterr = channel.exec_command(command)
while True:
rl, wl, xl = select.select([channel],[],[],0.0)
if len(rl) > 0:
logging.debug(channel.recv(bufsize))
except paramiko.SSHException, e:
logging.debug(repr(e))
def scp(self, srcFile, destPath):
transport = paramiko.Transport((self.host, int(self.port)))
transport.connect(username = self.user, password=self.passwd)
sftp = paramiko.SFTPClient.from_transport(transport)
try:
sftp.put(srcFile, destPath)
except IOError, e:
raise e
class bash:
def __init__(self, args, timeout=600):
self.args = args
logging.debug("execute:%s"%args)
self.timeout = timeout
self.process = None
self.success = False
self.run()
def run(self):
class Alarm(Exception):
pass
def alarm_handler(signum, frame):
raise Alarm
try:
self.process = Popen(self.args, shell=True, stdout=PIPE, stderr=PIPE)
if self.timeout != -1:
signal(SIGALRM, alarm_handler)
alarm(self.timeout)
try:
self.stdout, self.stderr = self.process.communicate()
if self.timeout != -1:
alarm(0)
except Alarm:
os.kill(self.process.pid, SIGKILL)
self.success = self.process.returncode == 0
except:
pass
if not self.success:
logging.debug("Failed to execute:" + self.getErrMsg())
def isSuccess(self):
return self.success
def getStdout(self):
try:
return self.stdout.strip("\n")
except AttributeError:
return ""
def getLines(self):
return self.stdout.split("\n")
def getStderr(self):
try:
return self.stderr.strip("\n")
except AttributeError:
return ""
def getErrMsg(self):
if self.isSuccess():
return ""
if self.getStderr() is None or self.getStderr() == "":
return self.getStdout()
else:
return self.getStderr()