-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathosshell.py
More file actions
49 lines (43 loc) · 1.63 KB
/
osshell.py
File metadata and controls
49 lines (43 loc) · 1.63 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
#!/usr/bin/env python3
import logging
class OSshell:
"""
This Class envelopes the object and methods for calling the OS
with a command shell, this Class execute the command
and returns
"""
def __init__(self, logname="log"):
"""
Initialize
"""
# Initialize the logging
self.logname = logname+".OSshell"
self.logger = logging.getLogger(self.logname)
self.logger.info("Logging initialization of OSshell. Done.")
def runoscommand(self, command_shell):
"""
Run the OS command in <command_shell>, a LIST containing the
command to run for subprocess
Return [status,
output,
[[msg],[errno],[strerror]]
]
status:
1 -> OK, command successfully executed
20 -> NOK, problem calling the operating system
"""
import subprocess
import sys
# Attach to LOCAL SYSLOG
logger = self.logger
errno = 0
strerror = ""
try:
p1 = subprocess.run(command_shell, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
except OSError as error:
errno, strerror = error.args
logger.critical("Couldn't execute command <" + command_shell + ">")
logger.critical("Error number: " + str(errno))
logger.critical("Error msg: " + strerror)
return [20, p1, ["Couldn't execute command <" + str(command_shell) + ">", errno, strerror]]
return [1, p1, ["Command successfully executed", errno, strerror]]