-2

我想将我的Scapy代码与 Nagios 监控工具集成,并希望结果返回 0、1、2、3。

该程序工作正常,充当广播流量的被动侦听器,并以时间间隔显示 IP 地址。

任何人都可以帮助我使用我可以用来返回这些值的代码,请在下面找到代码。

抱歉,我没有包含下面的代码:

    import sys
    import string
    import datetime
    import socket
    from datetime import datetime
    from scapy.all import *
    m_iface = "eth0"
    default_gw = "192.168.26.2"
    COUNTER_SLOTS = 5
    TIMEOUT = 20
    SCREEN_REFRESH = 15
    circular_counter = [0]*COUNTER_SLOTS
    session_start = {}
    session_stop = {}
    host_names = {}
    last_printed = 0

    host_names ["196.168.26.254"]=u'macbook'
    host_names ["192.167.26.237"]=u'testlocal'
    host_names ["192.168.26.238"]=u'xp1'
    host_names ["192.168.26.239"]=u'xp2'
    host_names ["192.168.26.2"]=u'default gateway'


    print "passive ping start"


    def arp_monitor_callback(pkt): 
    if ARP in pkt and pkt[ARP].op in (1,2): #who-has or is-at
    addr = pkt[ARP].psrc
    arp_counter(addr) 
return
    # circular buffer for statistics, 1 slot for 
    if UDP in pkt and IP in pkt:
    ipdata = pkt[IP]
    addr = ipdata.getlayer(IP).src
    arp_counter(addr)
    return

    def arp_counter(src):
    global last_printed
    tm = int(time.time())

    #print (pkt.psrc)
    #all_stats[src] = tm

    pos = src.find("192.168.26")
    if pos == -1:
    # print "wrong address"
    return

    if src in session_stop.keys():
    sess_stop = session_stop[src]
    if (tm - sess_stop)/60 > TIMEOUT:
    session_start[src] = tm   # start a new session
    session_stop[src] = tm   # start a new session
    else:
    session_stop[src] = tm   # start a new session

    else: # never saw the host
    session_start[src] = tm   # start a new session
    session_stop[src] = tm   # start a new session


    # print 
    if (tm - last_printed > SCREEN_REFRESH):
    print "-----------------------------------"
    last_printed = tm
    i = 1
    for k in sorted(session_start.keys(), cmp=lambda x, y: cmp(socket.inet_aton(x),                     socket.inet_aton(y))):
    if k in host_names.keys():
    hn = string.ljust(host_names[k], 40)
    else:
    hn = u'local_host_machine_ip'.ljust(40)

    last_hours = (tm - session_stop[k])/3600
    last_mins = ((tm - session_stop[k])/60) % 60

    s_start = (datetime.fromtimestamp(int(session_start[k])).strftime('%d/%m %H:%M'))
    s_stop = (datetime.fromtimestamp(int(session_stop[k])).strftime('%d/%m %H:%M'))

    pos = k.find("192.168.26")
    if pos != -1:
            print i, k,"\t", hn,"\t",last_hours,":",last_mins,"\t","                (",s_start,"==",s_stop,")",(session_stop[k] - session_start[k])/60
            i = i + 1

    #sys.stdout.flush()


    p = sniff(prn=arp_monitor_callback, store = 0)
4

1 回答 1

1

根据您提供的有限解释,有两种可能性。

Python 的return语句可用于返回 Python 对象,或者在本例中为整数。

http://docs.python.org/2/reference/simple_stmts.html#return

您必须在函数中使用它。由于您尚未发布任何代码,因此我不确定这是否正是您想要的。

sys当您运行完 Scapy 代码后,您还可以使用该模块返回退出状态。

http://docs.python.org/2/library/sys.html#sys.exit

使用示例sys-

import sys
from scapy.all import *

# Scapy magic

sys.exit(0) # exit with a code of 0
于 2013-08-14T00:20:38.913 回答