条件代码在scapy中返回0,1,2,3

时间:2013-08-13 10:51:29

标签: python scapy

我想将我的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)

1 个答案:

答案 0 :(得分:1)

基于您提供的有限解释,有两种可能性。

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

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

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

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

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