使用Scapy和Pyinstaller的Python CDP客户端

时间:2018-08-13 21:42:38

标签: python python-3.x pyinstaller scapy

我正在尝试编写一个基于Python的CDP客户端,类似于WinCDP。出于故障排除编译的目的,我有一个明显缩短的版本:

# Suppress Scapy IPv6 warning
import logging 
logging.getLogger("scapy.runtime").setLevel(logging.ERROR)

import re from scapy.all import *

load_contrib("cdp") class main:
    def __init__(self):
        self.filter = "ether host 01:00:0c:cc:cc:cc"
        self.interfaces = str(ifaces)
        print(self.interfaces)
        try:
            self.p = sniff(count=1,iface='Intel(R) 82578DM Gigabit Network Connection',filter=self.filter,timeout=60)
        except:
            print('OUCH!  Something went wrong.')
        else:
            print('Finished processing...')
            print("DeviceID: {}".format(self.p[0].payload["CDPMsgDeviceID"].val.decode('utf-8')))
            print("PortID: {}".format(self.p[0].payload["CDPMsgPortID"].iface.decode('utf-8')))
            print("NativeVLAN: {}".format(self.p[0].payload["CDPMsgNativeVLAN"].vlan))
            print("IPv4addr: {}".format(self.p[0].payload["CDPAddrRecordIPv4"].addr))
            print("Model: {}".format(self.p[0].payload["CDPMsgPlatform"].val.decode('utf-8')))
            print("Duplex: {}".format(self.p[0].payload["CDPMsgDuplex"].duplex))
            print("VTP Domain: {}".format(self.p[0].payload["CDPMsgVTPMgmtDomain"].val.decode('utf-8')))

if __name__ == "__main__": main()

在所有版本的Python上,我都尝试过产生类似于以下内容的结果:

INDEX  IFACE                                            IP              MAC
10     Hyper-V Virtual Ethernet Adapter                 172.31.253.241  78:15:27:01:22:F1
11     Intel(R) 82578DM Gigabit Network Connection      10.X.Y.Z     78:2B:CB:00:11:22
21     Hyper-V Virtual Ethernet Adapter #2              10.0.75.1       00:15:5D:CD:BE:03
26     Npcap Loopback Adapter                           127.0.0.1       00:00:00:00:00:00
4      VirtualBox Host-Only Ethernet Adapter #2         192.168.56.1    0A:00:27:00:00:04
9      NETGEAR WNA3100M N300 Wireless Mini USB Adapter                  2C:B0:5D:00:11:22
Finished processing...
DeviceID: switch01
PortID: GigabitEthernet5/0/29
NativeVLAN: 120
IPv4addr: 10.X.Y.Z
Model: cisco WS-C2960X-48FPD-L
Duplex: 1
VTP Domain: XYZ

我设法使脚本得以编译,它将以以下代码运行:

pyinstaller -F cdpscript.py --hidden-import=queue

但是,当可执行文件运行时,输出为:

INDEX  IFACE  IP  MAC
OUCH!  Something went wrong.

这使我相信,足够多的Scapy正在运行以打印ifaces命令的标题,但没有其他效果。这是在Python 3.6.6上完成的,因为Pyinstaller文档指示这是Scapy 2.4.0和PyInstaller 3.3.1支持的最新版本。

我希望能够给我的技术人员一个USB驱动器上的可执行文件,使他们可以运行并获取CDP信息,而不必在本地安装任何东西,这与我们使用WinCDP的方式类似。除此之外,WinCDP最近在Windows 10上无法很好地运行。我希望创建自己的版本,这样我们就可以在数据库中做一些更棒的事情,例如粘贴信息来跟踪我们的机器。无论如何,如果我无法将脚本编译成可以正常运行的exe,就不会发生这种乐趣。另外,任何熟悉Scapy的人都知道是否必须在目标计算机上安装NPcap吗?任何帮助将不胜感激。

0 个答案:

没有答案