批量生成IPv6 range地址

def get_ip(count='10', start='2001:2:3:4:5:6:7:8'):
    starts = start.split(':')
    A = int(starts[0], 16) # int('a',16) 将字符串'a'进行16进制转换 base 存在时,视 x 为 base 类型数字,并将其转换为 10 进制数字。
    # A2 = int('2001', 16)
    # print(A2)
    B = int(starts[1], 16)
    # print(B)
    C = int(starts[2], 16)
    D = int(starts[3], 16)
    E = int(starts[4], 16)
    F = int(starts[5], 16)
    G = int(starts[6], 16)
    H = int(starts[7], 16)
    file = open(str(count)+'_ipv6_range_list.txt', 'w')
    for A in range(A, 65536):
        A = hex(A).replace('0x', '')
        for B in range(B, 65536):
            B = hex(B).replace('0x', '')
            for C in range(C, 65536):
                C = hex(C).replace('0x', '')
                for D in range(D, 65536):
                    D = hex(D).replace('0x', '')
                    for E in range(E, 65536):
                        E = hex(E).replace('0x', '')
                        for F in range(F, 65536):
                            F = hex(F).replace('0x', '')
                            for G in range(G, 65536):
                                G = hex(G).replace('0x', '')
                                for H in range(H, 65536):
                                    # print "H=",H
                                    H1 = hex(H).replace('0x', '')
                                    H2 = hex(H+10).replace('0x', '')
                                    # ipv6 = "%s:%s:%s:%s:%s:%s:%s:%s" % (A, B, C, D, E, F, G, H)
                                    ipv6_start = "%s:%s:%s:%s:%s:%s:%s:%s" % (A, B, C, D, E, F, G, H1)
                                    ipv6_end = "%s:%s:%s:%s:%s:%s:%s:%s" % (A, B, C, D, E, F, G, H2)
                                    if count > 1:
                                        ipv6 = ipv6_start + '-' + ipv6_end
                                        file.write(ipv6 + "\n")
                                        count -= 1
                                        print(ipv6)
                                    elif count == 1:
                                        file.write(ipv6)
                                        count -= 1
                                    else:
                                        file.close()
                                        return

                                H = 0
                            G = 0
                        F = 0
                    E = 0
                D = 0
            C = 0
        B = 0

if __name__ == '__main__':
    get_ip(100, '2001:2:3:4:5:6:FFFE:FFFa')

 

posted @ 2021-06-17 20:51  dmwc  阅读(253)  评论(0编辑  收藏  举报