用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字
云代码 - python代码库

用python实现ping的功能

2023-09-04 作者: FN举报

[python]代码库

from scapy.layers.inet import *
from scapy.all import *
import random
import time
print("""
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
    ++++++++++++++       +      +         ++++++++++++++          ++            +++++++++++++            +++++++++++++++   
    ++          ++         +  +           ++          ++          ++            ++         ++            ++           ++    
    ++++++++++++++          ++            ++++++++++++++          ++            ++         ++            ++           ++
    ++                      ++            ++                      ++            ++         ++            +++++++++++++++
    ++                      ++            ++                      ++            ++         ++                         ++
    ++                      ++            ++                      ++            ++         ++            ++++++       ++
    ++                      ++            ++                      ++            ++         ++            ++++++       ++
    ++                      ++            ++                      ++            ++         ++            +++++++++++++++
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
""") ; time.sleep(2)
print("[                            ]   已加载:0%")   ; time.sleep(2)
print("[***                         ]   已加载:5%")   ; time.sleep(3)
print("[*******                     ]   已加载:15%")  ; time.sleep(4)
print("[*************               ]   已加载:35%")  ; time.sleep(3)
print("[*******************         ]   已加载:80%")  ; time.sleep(2)
print("[*************************   ]   已加载:99%")  ; time.sleep(5)
print("[****************************]   已加载:100%") ; time.sleep(2) ; print("加载完毕!启动程序...") ; time.sleep(2)
ip_dict = {}
stop_ip_range = 0
def py_ping(link_ip):
    for i in range(5):
        time.sleep(1)
        random_IP_id    =   random.randint(1, 9999)
        random_IP_ttl   =   random.randint(1, 64)
        random_ICMP_id  =   random.randint(1, 9999)
        random_ICMP_seq =   random.randint(1, 9999)
        ping_data = IP(dst=link_ip, id=random_IP_id, ttl=random_IP_ttl) / ICMP(id=random_ICMP_id,seq=random_ICMP_seq, ) / b'hello'
        print(ping_data)
        ping = sr1(ping_data,timeout=5)
        try:
            request_ip    =   (ping.getlayer(IP)).fields['src']
            recev_bytes   =   len(ping.getlayer(Padding).fields['load'])
            IP_ttl        =   ping.getlayer(IP).fields['ttl']
            ICMP_seq      =   ping.getlayer(ICMP).fields['seq']
        except:
            print("----------------------------------------------------------------------------------- ")
            print(f"[-]请求时产生异常,可能是目标IP:{link_ip}没有存活所导致,请检查信息后重新尝试 !")
            print("-----------------------------------------------------------------------------------")
            global stop_ip_range
            stop_ip_range+=1
            ip_dict['STOP_IP_%d'%stop_ip_range] = link_ip
            break
        print("---------------------------------------------------------------------------------------")
        print(f"[+]in {request_ip}  bytes is {recev_bytes} ttl is {IP_ttl} seq is {ICMP_seq}")
        print("---------------------------------------------------------------------------------------")

if __name__ == '__main__':
    # ping_ip = input("请输入ping的IP:")
    import ipaddress
    ip_list = []
    while True:
        scan_ip       = input("请输入要扫描的IP网络号地址(系统会自动扫描该网络下主机):")
        scan_ip_range = input("请输入扫描主机数:")
        try:
            ip = ipaddress.ip_network(f'{scan_ip}.0/24')
            break
        except:
            print("[-]您输入的不是正确的IP网络号地址!")
            pass

    for addr_ip in ip:
        ip_list.append(str(addr_ip))
    a = 0
    for i in range(int(scan_ip_range)):
        a+=1
        py_ping(ip_list[a])
    print("=====================================")
    for stop_ip_,stop_ip in ip_dict.items():
        print(f"[-]death ip is ---> {stop_ip}")
    print("=====================================")
    yn_write_ping = input("是否保存死亡IP记录(Y/N):")
    if (yn_write_ping == 'y') or (yn_write_ping == 'y'):
        with open("pyping_death_IP.txt",'wb') as op:
            op.write(str(ip_dict.items()).encode("UTF-8"))
            print("保存成功")
            time.sleep(3)
    else:
        pass

[源代码打包下载]




网友评论    (发表评论)


发表评论:

评论须知:

  • 1、评论每次加2分,每天上限为30;
  • 2、请文明用语,共同创建干净的技术交流环境;
  • 3、若被发现提交非法信息,评论将会被删除,并且给予扣分处理,严重者给予封号处理;
  • 4、请勿发布广告信息或其他无关评论,否则将会删除评论并扣分,严重者给予封号处理。


扫码下载

加载中,请稍后...

输入口令后可复制整站源码

加载中,请稍后...