也许您可以尝试 正则表达式来解析输出。这是我用的。请查看并发表评论。
import subprocess, re
def read_status(service):
p = subprocess.Popen(["systemctl", "status", service], stdout=subprocess.PIPE)
(output, err) = p.communicate()
output = output.decode('utf-8')
service_regx = r"Loaded:.*\/(.*service);"
status_regx= r"Active:(.*) since (.*);(.*)"
service_status = {}
for line in output.splitlines():
service_search = re.search(service_regx, line)
status_search = re.search(status_regx, line)
if service_search:
service_status['service'] = service_search.group(1)
#print("service:", service)
elif status_search:
service_status['status'] = status_search.group(1).strip()
#print("status:", status.strip())
service_status['since'] = status_search.group(2).strip()
#print("since:", since.strip())
service_status['uptime'] = status_search.group(3).strip()
#print("uptime:", uptime.strip())
return service_status
def main():
service = 'mysql'
reponse = read_status(service)
for key in reponse:
print('{}:{}'.format(key, reponse[key]))
if __name__ == '__main__':
main()
输出:
service:mysql.service
status:active (running)
since:Fri 2018-03-16 09:17:57 CET
uptime:6h ago
我只是用它来检查我的正则表达式。