zoukankan      html  css  js  c++  java
  • mtu探测

    # -*- coding: UTF-8 -*-
    from scapy.layers.inet import IP, ICMP, TCP
    from scapy.layers.l2 import Ether
    import sys
    import logging
    logging.getLogger("scapy.runtime").setLevel(logging.ERROR)  # 清除报错
    from scapy.all import *  # 导入scapy模块
    
    
    def mtu_one(mtu, dst):
        data = b'a' * (mtu - 28)
    
        # result = sr1(Ether(dst='48-bd-3d-22-77-e0') / IP(flags='DF', dst=dst) / ICMP() / data,
        #              timeout=0.2, iface='Intel(R) Dual Band Wireless-AC 3168')
    
        # result = sr1(IP(flags='DF',dst=dst) / ICMP() / data, timeout=0.2,
        #             iface='Intel(R) Dual Band Wireless-AC 3168')  # 发送第三层数据包
    
        result = sr1(IP(flags='DF', dst=dst) / ICMP() / data, timeout=0.2)  # 发送第三层数据包
        # print(result.show())
        try:
    
            # 如果返回数据包ICMP层的type==3,code==4则表明数据包已经超过了MTU
            if result.getlayer(ICMP).type == 3 and result.getlayer(ICMP).code == 4:
                return 1
    
            # 如果返回数据包ICMP层的type==0,code==0则表明数据包可以成功发送,大小没有超过MTU
            elif result.getlayer(ICMP).type == 0 and result.getlayer(ICMP).code == 0:
                return 2
        except:
            return None
    # -*- coding: UTF-8 -*-
    from scapy.layers.inet import IP, ICMP, TCP
    from scapy.layers.l2 import Ether
    import sys
    import logging
    logging.getLogger("scapy.runtime").setLevel(logging.ERROR)  # 清除报错
    from scapy.all import *  # 导入scapy模块
    
    
    def mtu_one(mtu, dst):
        data = b'a' * (mtu - 28)
    
        # result = sr1(Ether(dst='48-bd-3d-22-77-e0') / IP(flags='DF', dst=dst) / ICMP() / data,
        #              timeout=0.2, iface='Intel(R) Dual Band Wireless-AC 3168')
    
        # result = sr1(IP(flags='DF',dst=dst) / ICMP() / data, timeout=0.2,
        #             iface='Intel(R) Dual Band Wireless-AC 3168')  # 发送第三层数据包
    
        result = sr1(IP(flags='DF', dst=dst) / ICMP() / data, timeout=0.2)  # 发送第三层数据包
        # print(result.show())
        try:
    
            # 如果返回数据包ICMP层的type==3,code==4则表明数据包已经超过了MTU
            if result.getlayer(ICMP).type == 3 and result.getlayer(ICMP).code == 4:
                return 1
    
            # 如果返回数据包ICMP层的type==0,code==0则表明数据包可以成功发送,大小没有超过MTU
            elif result.getlayer(ICMP).type == 0 and result.getlayer(ICMP).code == 0:
                return 2
        except:
            return None
    
    
    if __name__ == "__main__":
        ipaddr = "8.8.8.8"
        mtu = 1500
        while mtu > 0:
            res = mtu_one(mtu, ipaddr)
            if res == 1 or res is None:
                mtu -= 1
            elif res == 2:
                print('mtu: %d' % mtu)
                break
    
    if __name__ == "__main__":
        ipaddr = "8.8.8.8"
        mtu = 1500
        while mtu > 0:
            res = mtu_one(mtu, ipaddr)
            if res == 1 or res is None:
                mtu -= 1
            elif res == 2:
                print('mtu: %d' % mtu)
                break
    [root@bogon ~]# python icmp_pro.py 
    Begin emission:
    ...........................................Finished sending 1 packets.
    ...........................................
    Received 86 packets, got 0 answers, remaining 1 packets
    Begin emission:
    Finished sending 1 packets.
    .*
    Received 2 packets, got 1 answers, remaining 0 packets
    mtu: 1499
  • 相关阅读:
    office365离线安装
    c#使用emit方法DB,实体相互转换
    c#采用emit将DataTable转List
    c#将List转换成DataTable
    c#将List转换成DataTable(采用Emit)
    聊聊编程开发的数据库批量插入(sql)
    c#随便聊聊数据库操作
    c#聊聊文件数据库kv
    WPF几个样式
    c#(IronPython)调用Python方法
  • 原文地址:https://www.cnblogs.com/dream397/p/14808750.html
Copyright © 2011-2022 走看看