【问题标题】:Forward new log messages via snmptrap通过 snmptrap 转发新的日志消息
【发布时间】:2014-03-28 12:46:50
【问题描述】:

我尝试监听新的文件行并在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)

我什么也没得到.. 这是我的第一个程序,请帮助修复它

【问题讨论】:

  • 您能否提供一系列 在终端中为您工作的 shell 命令,您希望将其转换为 Python?
  • 当然.. tail -f "file-name" -f 选项导致在到达文件末尾时不停止,而是等待附加数据附加到输入。以及发送 snmp 陷阱的示例命令: snmptrap -c iptvinfo -v 2c 10.144.216.91:163 "" 1.3.3.3.3.3.3.3 1.2.2.2.2.2.2 s same_text_from_tail
  • edit 提出您的问题,而不是在评论中发布(它可以格式化并且对其他用户更可见)
  • tail -f /root/zsv/log | snmptrap -c iptvinfo -v 2c 10.144.216.91:163 "" 1.3.3.3.3.3.3.3 1.2.2.2.2.2.2 s - 工作吗?
  • 不,我试过了。会很棒

标签: python python-2.7 subprocess net-snmp


【解决方案1】:
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.

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-09-06
    • 2015-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多