import json import subprocess with open('eve.json', 'r') as f: events = json.load(f) for event in events: if event['alert']['severity'] <= 2: ip = event['src_ip'] # 调用iptables命令封禁IP subprocess.call(['iptables', '-A', 'INPUT', '-s', ip, '-j', 'DROP'])输出一个封禁成功的结果,完善代码
时间: 2023-07-31 09:11:22 浏览: 151
subprocess.h::ant:适用于C和C ++的单头进程启动解决方案
可以在调用 `subprocess.call` 命令后,再进行一个判断,看是否执行成功,完善代码如下:
```python
import json
import subprocess
with open('eve.json', 'r') as f:
events = json.load(f)
for event in events:
if event['alert']['severity'] <= 2:
ip = event['src_ip']
# 调用iptables命令封禁IP
result = subprocess.call(['iptables', '-A', 'INPUT', '-s', ip, '-j', 'DROP'])
if result == 0:
print(f'封禁IP {ip} 成功!')
else:
print(f'封禁IP {ip} 失败!')
```
这样,当 `subprocess.call` 命令执行成功时,会输出一个封禁成功的结果,否则输出一个封禁失败的结果。
阅读全文