python3执行复杂脚本命令 推荐模块为subprocess.run()
因此我们执行脚本就采用此函数,可以判断shell执行状态,可以获取到shell的执行结果。在日常使用中应该足矣
待执行命令
netstat -antlp | awk '{print $4,$5}'
shell的命令(获取到文本中.com结尾的字符)
p = subprocess.run(["grep -rn '.com$' %s | awk '{print $1, $3}'"%(path)],shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE,check=False)
print(p)
命令执行完成返回CompletedProcess实例
参数: shell=True 通过操作系统的shell执行命令
stdout=subprocess.PIPE shell命令执行的输出结果
stderr=subprocess.PIPE shell命令执行错误时的输出结果,
returncode=0 表示shell命令执行成功
通过python3获取shell命令执行的结果,对结果进行处理并存入数据库(实测有效)