Python / scapy:按CTRL-C时,sniff()函数不会中断

时间:2017-03-21 16:49:40

标签: python multithreading networking wifi scapy

我正在编写一个显示所有可用WiFi网络的脚本。我使用scapy这样做了。

我想在按CTRL-C时结束脚本。但在按下之后没有任何反应,脚本继续搜索WiFi网络。

我读过它可能是由threading引起的。但我没有开始一个线程。即使在按下CTRL-BREAK后,程序仍会继续。

def PcktHandler(pckt):
    global APs
    try:
        if pckt.haslayer(Dot11):
            if pckt.type == 0 and pckt.subtype == 8:
                if pckt.addr2 not in APs:
                    APs[pckt.addr2] = pckt.info
                    output_aps(pckt.addr2, pckt.info, on_channel)
    except KeyboardInterrupt:
        os._exit

def output_aps(bssid, essid, channel):

        ch_space = 3
        if len(str(channel)) == 1:
            ch_space = 4

        print(str(bssid).upper()+' '*3+'| '+str(channel)+' '*ch_space+'| '+str(essid))

# scanning for networks   
def scan():
    global on_channel #-> channel currently on

    print('[*] Scanning for WiFi-Networks...\n')
    print('BSSID' + ' '*17 + 'CH' + ' '*5 + 'ESSID')

    while on_channel < 15:
        # channel hopping and sniffing
        try:
            os.system('iwconfig ' + iface + ' channel ' + str(on_channel))
        except (OSError):
            print('[-] Invalid interface.')
        try:
            sniff(iface='wlan0mon', prn=PcktHandler, count=10, timeout=3)
        except KeyboardInterrupt:
            os._exit

        on_channel += 1
        if on_channel >= 14:
            on_channel = 1


if __name__ == '__main__':
    args = argument_parser()
    APs = {}
    on_channel = 1

    if args.interface:
        iface = args.interface
        conf.iface = iface

        if args.scan:
            scan()


    else:
        print('[!] Select an interface.')

希望有一个解决方案! 谢谢你的帮忙!

0 个答案:

没有答案