其他分享
首页 > 其他分享> > 位运算生成ip地址和mac地址

位运算生成ip地址和mac地址

作者:互联网

位运算生成ip地址和mac地址

def IntToMac(macnum):
    return '{:02X}:{:02X}:{:02X}:{:02X}:{:02X}:{:02X}'.format(
            (macnum & 0xFF0000000000) >> 40,
            (macnum & 0xFF00000000) >> 32,
            (macnum & 0xFF000000) >> 24,
            (macnum & 0xFF0000) >> 16,
            (macnum & 0xFF00) >> 8,
            (macnum & 0xFF))
print(IntToMac(2))
00:00:00:00:00:02
def getstamaclist(numb, index, oui = 'F0:99:BF'):
    """
    :param numb: 起始IP
    :param index:     IP数量
    :return:  mac列表
    传入mac数量和索引,根据oui产生mac,返回一个list
    index:1,2,3, index*200+1 是起始mac,导致每个可以200个不重复mac
    """
    stal = []
    index *= 200
    for i in range(numb):
        index += 1
        stamac =  '{}:{:02X}:{:02X}:{:02X}'.format(oui, (index & 0xFF0000) >> 16, (index & 0xFF00) >> 8, (index & 0xFF))
        stal.append(stamac)
    return stal

print(getstamaclist(2, 3))

['F0:99:BF:00:02:59', 'F0:99:BF:00:02:5A']
def getiplist(startip, num):
    """
    :param startip: 起始IP
    :param num:     IP数量
    :return:   IP列表
    """
    ip = map(int, startip.split('.'))
    ip = list(ip)
    iplist = []
    for i in range(num):
        ipst = ip[0] << 24 | ip[1] << 16 | ip[2] << 8 | ip[3]
        ipst += i
        ips = '%s.%s.%s.%s' % ((ipst & 0xff000000) >> 24,
                            (ipst & 0x00ff0000) >> 16,
                            (ipst & 0x0000ff00) >> 8,
                            (ipst & 0x000000ff))
        iplist.append(ips)
    return iplist

print(getiplist('192.168.0.1', 10))

['192.168.0.1', '192.168.0.2', '192.168.0.3', '192.168.0.4', '192.168.0.5', '192.168.0.6', '192.168.0.7', '192.168.0.8', '192.168.0.9', '192.168.0.10']

标签:index,00,ip,mac,192.168,macnum,地址,02X
来源: https://blog.csdn.net/qq_42180416/article/details/119065911