import json import subprocess with open('/var/log/suricata/fast.log', '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} 失败!') 代码运行后报错:Traceback (most recent call last): File "do-Suricata.py", line 5, in <module> events = json.load(f) File "/usr/local/python3.7/lib/python3.7/json/__init__.py", line 296, in load parse_constant=parse_constant, object_pairs_hook=object_pairs_hook, **kw) File "/usr/local/python3.7/lib/python3.7/json/__init__.py", line 348, in loads return _default_decoder.decode(s) File "/usr/local/python3.7/lib/python3.7/json/decoder.py", line 340, in decode raise JSONDecodeError("Extra data", s, end) json.decoder.JSONDecodeError: Extra data: line 1 column 2 (char 1)如何解决
时间: 2023-12-03 10:47:12 浏览: 152
这个错误是因为 fast.log 文件中包含了多个 JSON 对象,而 json.load() 方法只能读取一个 JSON 对象。可以考虑逐行读取文件,然后对每行进行解析。
可以使用以下代码进行修改:
```
import json
import subprocess
with open('/var/log/suricata/fast.log', 'r') as f:
for line in f:
event = json.loads(line)
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} 失败!')
```
这样,每次读取一行并解析,就可以避免上述错误。
相关问题
import json import subprocess with open('/var/log/suricata/fast.log', '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} 失败!')修改优化一下这段代码
可以考虑以下几点进行优化:
1. 将代码封装成函数,方便复用。
2. 使用 try-except 语句处理 subprocess.call() 函数返回的异常,可以更好地处理命令执行失败的情况。
3. 使用 f-string 格式化字符串,使代码更加简洁明了。
4. 将常量提取出来,避免硬编码。
下面是优化后的代码:
```python
import json
import subprocess
LOG_FILE = '/var/log/suricata/fast.log'
IPTABLES_CMD = ['iptables', '-A', 'INPUT', '-s']
def ban_ip(ip):
try:
result = subprocess.call(IPTABLES_CMD + [ip, '-j', 'DROP'])
if result == 0:
print(f'封禁IP {ip} 成功!')
else:
print(f'封禁IP {ip} 失败!')
except subprocess.CalledProcessError as e:
print(f'命令执行失败:{e}')
with open(LOG_FILE, 'r') as f:
events = json.load(f)
for event in events:
if event['alert']['severity'] <= 2:
ip = event['src_ip']
ban_ip(ip)
```
这样优化后的代码更具可读性和可维护性,也更加健壮。
import json import subprocess with open('/var/log/suricata/fast.log', 'r') as f: for line in f: try: event = json.loads(line) except json.JSONDecodeError: # 如果解析失败,则跳过这一行 continue 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} 失败!')修改优化一下,fast.log中内容并不是json格式
好的,那我们可以使用正则表达式来解析fast.log中的内容,而不是使用json。代码如下:
```python
import re
import subprocess
with open('/var/log/suricata/fast.log', 'r') as f:
for line in f:
# 使用正则表达式匹配IP地址
match = re.search(r'\d+\.\d+\.\d+\.\d+', line)
if not match:
continue
ip = match.group()
# 调用iptables命令封禁IP
result = subprocess.call(['iptables', '-A', 'INPUT', '-s', ip, '-j', 'DROP'])
if result == 0:
print(f'封禁IP {ip} 成功!')
else:
print(f'封禁IP {ip} 失败!')
```
这段代码使用了正则表达式来匹配IP地址,在匹配成功后调用iptables命令封禁该IP地址。
阅读全文