forked from chaudron/ovs_perf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dut_ssh_shell.py
97 lines (81 loc) · 2.77 KB
/
dut_ssh_shell.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
# Copyright 2017-2019 "OVS Performance" Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# Files name:
# dut_ssh_shell.py
#
# Description:
# Simple DUT SSH shell class
#
# Author:
# Eelco Chaudron
#
# Initial Created:
# 17 January 2017
#
#
# Imports
#
import logging
import shlex
import spur
import sys
from spur import SshShell
#
# DutExecutionResult(object)
#
class DutExecutionResult(object):
def __init__(self, return_code, stdout_output, stderr_output):
self.return_code = return_code
self.stdout_output = stdout_output.decode("utf-8", "ignore")
self.stderr_output = stderr_output.decode("utf-8", "ignore")
@property
def output(self):
return self.stdout_output + self.stderr_output
#
# DutSshShell(SshShell)
#
class DutSshShell(SshShell):
def dut_exec(self, cmd, **kwargs):
die_on_error = kwargs.pop("die_on_error", False)
if 'raw_cmd' in kwargs:
command = kwargs['raw_cmd']
else:
command = shlex.split(cmd)
self.logger.debug("EXEC_I: \"{}\"".format(cmd))
self.logger.debug("EXEC_F: \"{}\"".format(command))
try:
result = self.run(command, allow_error=True)
result = DutExecutionResult(result.return_code,
result.output,
result.stderr_output)
except spur.errors.NoSuchCommandError as e:
result = DutExecutionResult(-100, "", e.message)
pass
self.logger.debug("RETURN: {}".format(result.return_code))
self.logger.debug("STDOUT: >>{}<<END".format(
result.stdout_output.encode('utf-8')))
self.logger.debug("STDERR: >>{}<<END".format(
result.stderr_output.encode('utf-8')))
if result.return_code != 0 and die_on_error is True:
print(("ERROR[%d]: Failed executing command, \"%s\", on DUT \"%s\""
% (result.return_code, " ".join(command), self._hostname)))
sys.exit(-1)
return result
def dut_exec_shell(self, cmd, **kwargs):
return self.dut_exec("", raw_cmd=['sh', '-c', cmd], **kwargs)
@property
def logger(self):
name = '.'.join(['dutSh', self._hostname])
return logging.getLogger(name)