简体   繁体   中英

how to increment mac address in python

I am trying to send a request with name, id.no and mac address. This process i need to do for 100 times. i mean 100 different names and id.no's and mac address. I tried doing this. I want output as name1, 1234567679 and aa:bb:cc:dd:11:22. can someone please help me. I have pasted my code and output below

for i in range (1,10):
    name = 'name'+str(i)
    print name
    id = 121333445500000
    id += 1
    print id
    mac = "aa:bb:cc:dd:"
    ee = 0
    for j in range(0,9):
        ee1 = '0'+str(ee + j)+':'
        ff = 0

        for k in range(0,10):
            ff1 = ff+k
            if ff1 < 10:
               mac1 = mac + ee1 + '0' + str(ff1)
               print mac1

            else:
                mac1 = mac + ee1 + str(ff1)
                print mac1

Output:

name1
121333445500001
aa:bb:cc:dd:00:00
aa:bb:cc:dd:00:01
aa:bb:cc:dd:00:02
aa:bb:cc:dd:00:03
aa:bb:cc:dd:00:04
aa:bb:cc:dd:00:05
aa:bb:cc:dd:00:06
aa:bb:cc:dd:00:07
aa:bb:cc:dd:00:08
aa:bb:cc:dd:00:09
aa:bb:cc:dd:01:00
aa:bb:cc:dd:01:01
aa:bb:cc:dd:01:02
aa:bb:cc:dd:01:03
aa:bb:cc:dd:01:04
aa:bb:cc:dd:01:05
aa:bb:cc:dd:01:06
aa:bb:cc:dd:01:07
aa:bb:cc:dd:01:08
aa:bb:cc:dd:01:09
aa:bb:cc:dd:02:00
aa:bb:cc:dd:02:01
aa:bb:cc:dd:02:02
aa:bb:cc:dd:02:03
aa:bb:cc:dd:02:04
aa:bb:cc:dd:02:05
aa:bb:cc:dd:02:06
aa:bb:cc:dd:02:07
aa:bb:cc:dd:02:08
aa:bb:cc:dd:02:09
aa:bb:cc:dd:03:00
aa:bb:cc:dd:03:01
aa:bb:cc:dd:03:02
aa:bb:cc:dd:03:03
aa:bb:cc:dd:03:04
aa:bb:cc:dd:03:05
aa:bb:cc:dd:03:06
aa:bb:cc:dd:03:07
aa:bb:cc:dd:03:08
aa:bb:cc:dd:03:09
aa:bb:cc:dd:04:00
aa:bb:cc:dd:04:01
aa:bb:cc:dd:04:02
aa:bb:cc:dd:04:03
aa:bb:cc:dd:04:04
aa:bb:cc:dd:04:05
aa:bb:cc:dd:04:06
aa:bb:cc:dd:04:07
aa:bb:cc:dd:04:08
aa:bb:cc:dd:04:09
aa:bb:cc:dd:05:00
aa:bb:cc:dd:05:01
aa:bb:cc:dd:05:02
aa:bb:cc:dd:05:03
aa:bb:cc:dd:05:04
aa:bb:cc:dd:05:05
aa:bb:cc:dd:05:06
aa:bb:cc:dd:05:07
aa:bb:cc:dd:05:08
aa:bb:cc:dd:05:09
aa:bb:cc:dd:06:00
aa:bb:cc:dd:06:01
aa:bb:cc:dd:06:02
aa:bb:cc:dd:06:03
aa:bb:cc:dd:06:04
aa:bb:cc:dd:06:05
aa:bb:cc:dd:06:06
aa:bb:cc:dd:06:07
aa:bb:cc:dd:06:08
aa:bb:cc:dd:06:09
aa:bb:cc:dd:07:00
aa:bb:cc:dd:07:01
aa:bb:cc:dd:07:02
aa:bb:cc:dd:07:03
aa:bb:cc:dd:07:04
aa:bb:cc:dd:07:05
aa:bb:cc:dd:07:06
aa:bb:cc:dd:07:07
aa:bb:cc:dd:07:08
aa:bb:cc:dd:07:09
aa:bb:cc:dd:08:00
aa:bb:cc:dd:08:01
aa:bb:cc:dd:08:02
aa:bb:cc:dd:08:03
aa:bb:cc:dd:08:04
aa:bb:cc:dd:08:05
aa:bb:cc:dd:08:06
aa:bb:cc:dd:08:07
aa:bb:cc:dd:08:08
aa:bb:cc:dd:08:09
name2
121333445500001
aa:bb:cc:dd:00:00
aa:bb:cc:dd:00:01
aa:bb:cc:dd:00:02
aa:bb:cc:dd:00:03
aa:bb:cc:dd:00:04
aa:bb:cc:dd:00:05
aa:bb:cc:dd:00:06
aa:bb:cc:dd:00:07
aa:bb:cc:dd:00:08
aa:bb:cc:dd:00:09
aa:bb:cc:dd:01:00
aa:bb:cc:dd:01:01
aa:bb:cc:dd:01:02
aa:bb:cc:dd:01:03
aa:bb:cc:dd:01:04
aa:bb:cc:dd:01:05
aa:bb:cc:dd:01:06
aa:bb:cc:dd:01:07
aa:bb:cc:dd:01:08
aa:bb:cc:dd:01:09
aa:bb:cc:dd:02:00
aa:bb:cc:dd:02:01
aa:bb:cc:dd:02:02
aa:bb:cc:dd:02:03
aa:bb:cc:dd:02:04
aa:bb:cc:dd:02:05
aa:bb:cc:dd:02:06
aa:bb:cc:dd:02:07
aa:bb:cc:dd:02:08
aa:bb:cc:dd:02:09
aa:bb:cc:dd:03:00
aa:bb:cc:dd:03:01
aa:bb:cc:dd:03:02
aa:bb:cc:dd:03:03
aa:bb:cc:dd:03:04
aa:bb:cc:dd:03:05
aa:bb:cc:dd:03:06
aa:bb:cc:dd:03:07
aa:bb:cc:dd:03:08
aa:bb:cc:dd:03:09
aa:bb:cc:dd:04:00
aa:bb:cc:dd:04:01
aa:bb:cc:dd:04:02
aa:bb:cc:dd:04:03
aa:bb:cc:dd:04:04
aa:bb:cc:dd:04:05
aa:bb:cc:dd:04:06
aa:bb:cc:dd:04:07
aa:bb:cc:dd:04:08
aa:bb:cc:dd:04:09
aa:bb:cc:dd:05:00
aa:bb:cc:dd:05:01
aa:bb:cc:dd:05:02
aa:bb:cc:dd:05:03
aa:bb:cc:dd:05:04
aa:bb:cc:dd:05:05
aa:bb:cc:dd:05:06
aa:bb:cc:dd:05:07
aa:bb:cc:dd:05:08
aa:bb:cc:dd:05:09
aa:bb:cc:dd:06:00
aa:bb:cc:dd:06:01
aa:bb:cc:dd:06:02
aa:bb:cc:dd:06:03
aa:bb:cc:dd:06:04
aa:bb:cc:dd:06:05
aa:bb:cc:dd:06:06
aa:bb:cc:dd:06:07
aa:bb:cc:dd:06:08
aa:bb:cc:dd:06:09
aa:bb:cc:dd:07:00
aa:bb:cc:dd:07:01
aa:bb:cc:dd:07:02
aa:bb:cc:dd:07:03
aa:bb:cc:dd:07:04
aa:bb:cc:dd:07:05
aa:bb:cc:dd:07:06
aa:bb:cc:dd:07:07
aa:bb:cc:dd:07:08
aa:bb:cc:dd:07:09
aa:bb:cc:dd:08:00
aa:bb:cc:dd:08:01
aa:bb:cc:dd:08:02
aa:bb:cc:dd:08:03
aa:bb:cc:dd:08:04
aa:bb:cc:dd:08:05
aa:bb:cc:dd:08:06
aa:bb:cc:dd:08:07
aa:bb:cc:dd:08:08
aa:bb:cc:dd:08:09
name3
121333445500001

Hope this Helps

mac="0xaabbccdd0000"
for i in range(101):
    mac = "{:012X}".format(int(mac, 16) + 1)
    print(':'.join(mac[i]+mac[i+1] for i in range(0, len(mac), 2)))

Try this code

import time, random

mac = "aa:bb:cc:dd:"

for i in range(1,100):
    name = 'name'+ str(i)
    # getting random number for id
    id = int(time.time()) + random.randrange(0, 100, 1)

    # quotient in part 1
    mac_end1 = "0"  + str(int(i/10))
    # remainder in part 2
    mac_end2 = "0" + str(int(i%10))

    mac1 = mac + mac_end1 + ":" + mac_end2         
    print name, id, mac1

Based on @tcpip, here is a more generic way

Script filename: mac_generator.py

#%%
import argparse 
import sys

# sys.argv = ['']

parser = argparse.ArgumentParser()
parser.add_argument('-t',
                    '--total',
                    type=int,
                    default=100,
                    help="The total number of mac address to be generated")
parser.add_argument('-f',
                    '--file',
                    action="store_true",
                    help="Generate in filename mac_address.txt")
args = parser.parse_args()

print("Total number of MAC address to be generated is: {}".format(args.total))

#%%

mac="0x000000000001"

def print_mac():
    for i in range(args.total - 1):
        global mac
        mac = "{:012X}".format(int(mac, 16) + 1)
        print(':'.join(mac[i]+mac[i+1] for i in range(0, len(mac), 2)))

original_stdout = sys.stdout # Save a reference to the original standard output

if(args.file):
    with open('mac_address.txt', 'w') as f:
        sys.stdout = f # Change the standard output to the file we created.
        print_mac()
        sys.stdout = original_stdout # Reset the standard output to its origin
else:
    print_mac()

Usage

usage: mac_generator.py [-h] [-t TOTAL] [-f]

optional arguments:
  -h, --help            show this help message and exit
  -t TOTAL, --total TOTAL
                        The total number of mac address to be generated
  -f, --file            Generate in filename mac_address.txt

So, if you want to generate 100 MAC address, run ./mac_generator -t 100

I used the netaddr library, it makes it quite neat:

   base = netaddr.EUI(emac2)
   for m in range(1, count2):
        value = base.value
        value += 1
        base._set_value(value)
        macs.append(str(base))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM