Python generates random MAC addresses

Source: Internet
Author: User
This article describes how to generate random MAC addresses in Python. For more information, see use python code to generate a random MAC address, if you use the scapy module, you can directly use the RandMAC () function to generate a MAC.

Python

The code is as follows:


Import random
Maclist = []
For I in range (1, 7 ):
RANDSTR = "". join (random. sample ("0123456789 abcdef", 2 ))
Maclist. append (RANDSTR)
RANDMAC = ":". join (Maclist)
Print RANDMAC
-------------------------------- Running result -----------------------------------
E4: 13: 0e: 1a: 73: f5

The following Fake_HW is a mac address packaged in binary format using struct.

The code is as follows:


Import random
Import struct
Mac_bin_list = []
Mac_hex_list = []
For I in range (1, 7 ):
I = random. randint (0x00, 0xff)
Mac_bin_list.append (I)
Fake_HW = struct. pack ("BBBBBB", mac_bin_list [0], mac_bin_list [1], mac_bin_list [2], mac_bin_list [3], mac_bin_list [4], mac_bin_list [5]
For j in mac_bin_list:
Mac_hex_list.append (hex (j ))
Hardware = ":". join (mac_hex_list). replace ("0x ","")
Print Hardware
-------------------- Result -----------------------------
24: c7: 6f: 92: 2c: 42

The above is all the content of this article. I hope you will like it.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.