Python generates random MAC address _python

Source: Internet
Author: User

Using Python code to generate a random MAC address, use the Python network programming or available, and use the Scapy module to generate the Mac directly using the Randmac () function.

Python

Copy Code code as follows:

Import Random
Maclist = []
For I in Range (1,7):
Randstr = "". Join (Random.sample ("0123456789abcdef", 2)
Maclist.append (RANDSTR)
Randmac = ":". Join (Maclist)
Print Randmac
--------------------------------Run Results-----------------------------------
E4:13:0e:1a:73:f5

The following FAKE_HW are MAC addresses packaged in binary format with struct

Copy Code code 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
--------------------Results-----------------------------
24:c7:6f:92:2c:42

The above is the entire content of this article, I hope you can enjoy.

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.