使用python制作ipv6网段扫描脚本(升级版本)

对一个个ipv6地址进行扫描时间需要太久,为了解决这个问题,我们将掩码比较小的ipv6网段进行细分成/72网段地址,并且可以对这些网段同时扫描,极大的减少扫描到存活ipv6地址的时间

具体脚本如下

import ipaddress
from scapy.all import *
import concurrent.futures

def ping_ipv6_address(address):
    """
    Ping an IPv6 address and return True if it is alive, otherwise False.
    """
    icmpv6_packet = IPv6(dst=address)/ICMPv6EchoRequest()
    response = sr1(icmpv6_packet, timeout=2, verbose=0)
    
    if response and ICMPv6EchoReply in response:
        print(f"{address} is alive")
        return True
    return False

def scan_ipv6_network(subnet):
    """
    Scan an IPv6 subnet and return a list of addresses that are alive.
    """
    alive_addresses = []

    for ip in subnet:
        if ping_ipv6_address(str(ip)):
            alive_addresses.append(str(ip))
    
    return alive_addresses

def split_ipv6_network(network, target_prefix_length):
    """
    Split an IPv6 network into smaller subnets with target_prefix_length.
    """
    net = ipaddress.IPv6Network(network)
    subnets = list(net.subnets(new_prefix=target_prefix_length))
    return subnets

def get_ipv6_network():
    """
    Prompt the user to enter an IPv6 network and validate the input.
    """
    while True:
        try:
            network = input("Enter the IPv6 network to scan (e.g., 2449:8C38:80:220::/64): ")
            ipv6_network = ipaddress.IPv6Network(network)  # Validate input
            return ipv6_network
        except ValueError as ve:
            print("Invalid input, please enter a valid IPv6 network.")

def main():
    while True:
        ipv6_network = get_ipv6_network()
        
        # Determine the target prefix length for splitting
        if ipv6_network.prefixlen <= 64:
            target_prefix_length = 72
        else:
            target_prefix_length = 64
        
        # Split the network into subnets with the target prefix length
        subnets = split_ipv6_network(ipv6_network, target_prefix_length)
        print(f"Splitting {ipv6_network} into /{target_prefix_length} subnets:")
        for subnet in subnets:
            print(f"Subnet: {subnet}")
        
        # Perform parallel scanning of each subnet
        with concurrent.futures.ThreadPoolExecutor() as executor:
            future_to_subnet = {executor.submit(scan_ipv6_network, subnet): subnet for subnet in subnets}
            for future in concurrent.futures.as_completed(future_to_subnet):
                subnet = future_to_subnet[future]
                try:
                    alive_addresses = future.result()
                    if alive_addresses:
                        print(f"Alive addresses in {subnet}:")
                        for address in alive_addresses:
                            print(address)
                except Exception as e:
                    print(f"Error scanning {subnet}: {e}")
        
        # Ask if the user wants to scan another network
        response = input("Do you want to scan another IPv6 network? (yes/no): ")
        if response.lower() != "yes":
            break

if __name__ == "__main__":
    main()

  • 7
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值