通过snmptrap日志转发消息

2024-06-12 03:44:15 发布

您现在位置:Python中文网/ 问答频道 /正文

我尝试侦听新的文件行并在snmptraps中重新发送:

#!/usr/bin/env python
import subprocess

sreader = "tail -f /root/zsv/log"
ssreader = subprocess.Popen(sreader,shell=True,stdout=subprocess.PIPE,stderr=subprocess.STDOUT)

strap = 'snmptrap -c iptvinfo -v 2c 192.168.10.10:163 "" 1.3.3.3.3.3.3.3 1.2.2.2.2.2.2 s '
subprocess.Popen([strap + ssreader.communicate()[0]],shell=True,stdout=subprocess.PIPE,stderr=subprocess.STDOUT)

我什么也得不到。。 这是我的第一个程序,请帮我修一下


Tags: 文件truebinusrstderrstdoutshellsubprocess
1条回答
网友
1楼 · 发布于 2024-06-12 03:44:15
import shlex
from subprocess import Popen, PIPE, check_call

snmptrap = shlex.split('snmptrap -c iptvinfo -v 2c 192.168.10.10:163 "" '
                       '1.3.3.3.3.3.3.3 1.2.2.2.2.2.2 s')
tail = Popen(["tail", "-f", filename], stdout=PIPE, bufsize=1)
for line in iter(tail.stdout.readline, b''):
    check_call(snmptrap + [line]) # send line
tail.stdout.close()
rc = tail.wait()

注意:shell=False默认情况下,stderr不会被重定向。在

您可以将代码更改为一次发送所有可用行,或者每分钟只发送一次。在

根据您的需要,您可以选择适合您的案例tail -f在Python中的实现。见How can I tail a log file in Python?Get last n lines of a file with Python, similar to tail。在

相关问题 更多 >