2022-12-09 11:19:21 +01:00
|
|
|
# This class offers easy logik to execute cli commands
|
|
|
|
# @author Kevin Veen-Birkenbach [kevin@veen.world]
|
|
|
|
|
|
|
|
import subprocess
|
|
|
|
|
2022-12-09 13:08:07 +01:00
|
|
|
class Cli(object):
|
2022-12-09 11:19:21 +01:00
|
|
|
|
|
|
|
def __init__(self):
|
2022-12-09 12:03:45 +01:00
|
|
|
self.command = ''
|
|
|
|
self.output = []
|
2022-12-09 11:19:21 +01:00
|
|
|
pass
|
|
|
|
|
|
|
|
def executeCommand(self,command):
|
|
|
|
self.command = command
|
|
|
|
process = subprocess.Popen([command], stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
|
|
|
|
out, err = process.communicate()
|
|
|
|
stdout = out.splitlines()
|
|
|
|
self.output = []
|
|
|
|
for line in stdout:
|
|
|
|
self.output.append(line.decode("utf-8"))
|
|
|
|
if process.wait() > bool(0):
|
2022-12-10 17:30:27 +01:00
|
|
|
raise Exception("Error for: \nCommand:<<" + str(command) + ">>\nOutput:<<" + str(out) + ">>\nExitcode:<<" + str(err) + ">>")
|
2022-12-09 11:19:21 +01:00
|
|
|
return self.output
|
|
|
|
|
|
|
|
def getOutputString(self):
|
2022-12-09 13:22:57 +01:00
|
|
|
return str('\n'.join(self.output))
|
2022-12-09 11:19:21 +01:00
|
|
|
|
|
|
|
def getCommandString(self):
|
|
|
|
return self.command
|