This repository has been archived on 2025-09-14. You can view files and clone it, but cannot push or open issues or pull requests.
Files
lijia-tsg-oam/py_cmd/tsg_hardware_reboot.py

61 lines
2.0 KiB
Python
Raw Normal View History

#coding=utf-8
2019-07-23 11:56:52 +08:00
import sys
2019-08-06 18:54:58 +08:00
import re
2019-07-23 11:56:52 +08:00
import syslog
import subprocess
2019-07-23 11:56:52 +08:00
##define KERN_EMERG "<0>" /* system is unusable */
##define KERN_ALERT "<1>" /* action must be taken immediately */
##define KERN_CRIT "<2>" /* critical conditions */
##define KERN_ERR "<3>" /* error conditions */
##define KERN_WARNING "<4>" /* warning conditions */
##define KERN_NOTICE "<5>" /* normal but significant condition */
##define KERN_INFO "<6>" /* informational */
##define KERN_DEBUG "<7>" /* debug-level messages */
MSG_PREFIX = ['EMERG', 'ALERT', 'CRIT', 'ERR', 'WARNING', 'NOTICE', 'INFO', 'DEBUG']
2019-08-06 18:54:58 +08:00
#return exitcode value + output message:
# 0: succ
# 1: error
def system_cmd_run(cmd_str):
dangerous_cmd = {"rm", "mv", "poweroff", "shutdown"}
for cmd in dangerous_cmd:
pattern = "\s*%s" %(cmd)
match_str = re.match(pattern, cmd_str)
if not match_str is None:
2019-08-06 18:54:58 +08:00
print("can't run this cmd:%s" %(cmd_str))
sys.exit(1)
try:
exitcode, output = subprocess.getstatusoutput(cmd_str)
except Exception as e:
#if exitcode != 0:
# output = ""
print(e)
return 1, e
2019-08-06 18:54:58 +08:00
return exitcode, output
2019-07-23 11:56:52 +08:00
def tsg_hardware_reboot():
2019-08-06 18:54:58 +08:00
#G_LOCAL_NODE_NAME = get_local_node_name()
G_LOCAL_NODE_NAME = "TSG_MCN1"
2019-07-23 11:56:52 +08:00
log_handle = syslog.openlog(G_LOCAL_NODE_NAME)
msg = "[%s] %s" %(MSG_PREFIX[syslog.LOG_NOTICE], "call tsg_hardware_reboot...")
syslog.syslog(syslog.LOG_NOTICE, msg)
ret = system_cmd_run("reboot")
if ret != 0:
log_handle = syslog.openlog(G_LOCAL_NODE_NAME)
msg = "[%s] %s" %(MSG_PREFIX[syslog.LOG_NOTICE], "tsg_hardware_reboot error!")
syslog.syslog(syslog.LOG_NOTICE, msg)
print (msg)
return 1
return 0
2019-07-23 11:56:52 +08:00
if __name__ == '__main__':
fun_res_code = tsg_hardware_reboot()
sys.exit(fun_res_code)