Skip to content

Commit

Permalink
0.2 work
Browse files Browse the repository at this point in the history
  • Loading branch information
joshschmelzle committed May 25, 2023
1 parent 46d5394 commit 994ac95
Show file tree
Hide file tree
Showing 14 changed files with 178 additions and 71 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ TODO.md
dist
.pytest_cache/
/.mypy_cache/
venv/
3 changes: 3 additions & 0 deletions .pypirc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[distutils]
index-servers =
pypi
13 changes: 13 additions & 0 deletions DEV.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# DEV.md

## Useful commands

### bumpver

```bash
bumpver show
bumpver show -vv
bumpver update --dry --minor --no-fetch
bumpver update --dry --minor
bumpver update --minor
```
7 changes: 7 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
Release History
===============

0.0.2 (2023-05-23)
------------------

- Minor bug fixes and fix tests.
- Switch from setup.py to pyproject.toml.
- License change from MIT to BSD-3-Clause.

0.0.1 (2019-02-27)
------------------

Expand Down
2 changes: 2 additions & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
# MANIFEST.in

include README.md LICENSE HISTORY.md
59 changes: 36 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,44 +1,57 @@
randmac.py
==========

a utility that generates 12-digit mac addresses; either the NIC portion or full 12-digit MAC.
randmac is a utility that generates 12-digit mac addresses; either the NIC portion or full 12-digit MAC.

the optional `-f` argument will return a random 12-digit MAC address that can be identified by the locally administrated address (LAA) format. This means you will always see `x2`, `x6`, `xA`, or `xE` at the beginning of a MAC address generated by randmac.
The optional `-f` argument will return a random 12-digit MAC address that can be identified by the locally administrated address (LAA) format. This means you will always see `x2`, `x6`, `xA`, or `xE` at the beginning of a MAC address generated by randmac.

![](randmac20200913.gif)

# installation
# Installation

to install with pip:
To install with pip:

`pip install randmac`
`python3 -m pip install randmac`

# requirements
# Requirements

Python >3.2 required.
Python 3.2 or greater is required.

# mac address formats
# Supported MAC address formats

Supported MAC address formats:
- MM:MM:MM:SS:SS:SS
- MM-MM-MM-SS-SS-SS
- MM.MM.MM.SS.SS.SS
- MMMM.MMSS.SSSS
- MMMMMMSSSSSS
Supported formats:

- MM:MM:MM:SS:SS:SS (colons)
- MM-MM-MM-SS-SS-SS (hyphens/dashes)
- MM.MM.MM.SS.SS.SS (dots/periods)
- MMMM.MMSS.SSSS (cisco style)
- MMMMMMSSSSSS (no separator)

where `M` stands for the manufacturer or vendor, and `S` stands for the NIC specific portion.

# usage
# Usage

```
usage: randmac [-h] [-V] [-p] [mac]
Generates MAC addresses. By default randmac will generate a 12-digit MAC address following the Locally Administrated Address (LAA) format. randmac only supports 12-digit (48-bit) MAC addresses.
positional arguments:
mac mac address required for output format
you can `from randmac import RandMac` and use it like `RandMac()`.
options:
-h, --help show this help message and exit
-V, --version show program's version number and exit
-p, --partial randomizes the NIC portion of a MAC address
```

if you wish to change the mac address format. provide a sample mac so `randmac` knows what the output format should be.
In your own code, you can `from randmac import RandMac` and use it like `RandMac()`. If you wish to change the mac address format, pass in a sample MAC address with the target format. This means you can `from randmac import RandMac` and use it like `RandMac("0000.0000.0000")` or `RandMac("ff-ff-ff-dd-dd-dd")` to specify the delimiter format.

you can `from randmac import RandMac` and use it like `RandMac("0000.0000.0000")`.
From a terminal (if the the console scripts entry point `randmac` is in your path and executable) you can type `randmac` and press enter to get a generate a new 12-digit LAA address, or `randmac 00:00:00:00:00:00 -p` to generate a MAC with the same OUI, but a different NIC portion. The result will look something like `00:00:00:60:cf:6e`.

from a terminal (if the the console scripts entry point `randmac` is in your path and executable) you can use `randmac` to get a generate a new 12-digit LAA address, or `randmac 00:00:00:00:00:00 -p` to generate a MAC with the same OUI, but a different NIC portion.
# Example usage

# example usage
From Python:

```
>>> from randmac import RandMac
Expand All @@ -52,7 +65,7 @@ from a terminal (if the the console scripts entry point `randmac` is in your pat
'06eb4584d1e3'
```

or
From CLI:

```
> randmac
Expand All @@ -61,6 +74,6 @@ fa:bf:7c:5d:65:3e
00-00-00-dd-5f-16
```

# license
# License

license can be found [here](https://github.com/joshschmelzle/randmac/blob/master/LICENSE).
The license for this project can be found [here](https://github.com/joshschmelzle/randmac/blob/master/LICENSE).
42 changes: 42 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
[build-system]
requires = ["setuptools==61.0.0", "wheel"]
build-backend = "setuptools.build_meta"

[project]
name = "randmac"
description = ""
readme = "README.md"
authors = [{ name = "Josh Schmelzle", email = "[email protected]"}]
license = { file = "LICENSE" }
classifiers = [
"Natural Language :: English",
"Development Status :: 3 - Alpha",
"Programming Language :: Python :: 3.2",
"Intended Audience :: Developers",
"Intended Audience :: System Administrators",
"Topic :: Utilities",
]
keywords = ["randmac", "random mac", "random mac address"]

[project.scripts]
randmac = "randmac=randmac.__main__:main"

[project.optional-dependencies]
build = ["build", "twine"]
dev = ["black", "bumpver", "isort", "mypy", "pytest"]

[tool.bumpver]
current_version = "0.1.0"
version_pattern = "MAJOR.MINOR.PATCH"
commit_message = "Bump version {old_version} -> {new_version}"
commit = true
tag = true
push = false

[tool.bumpver.file_patterns]
"pyproject.toml" = [
'current_version = "{version}"',
]
"randmac/__version__.py" = [
'__version__ = "{version}"'
]
7 changes: 7 additions & 0 deletions randmac/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,8 @@
# /usr/bin/env python3
# -*- coding: utf-8 -*-
#
# _ _ _ _| _ _ _ _
# | (_|| |(_|| | |(_|(_
#

from randmac.randmac import RandMac
4 changes: 4 additions & 0 deletions randmac/__main__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# /usr/bin/env python3
# -*- coding: utf-8 -*-
#
# _ _ _ _| _ _ _ _
# | (_|| |(_|| | |(_|(_
#

import os
import platform
Expand Down
6 changes: 4 additions & 2 deletions randmac/__version__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# /usr/bin/env python3
# -*- coding: utf-8 -*-
#
# _ _ _ _| _ _ _ _
# | (_|| |(_|| | |(_|(_
Expand All @@ -6,8 +8,8 @@
__title__ = "randmac"
__description__ = "a random 12-digit mac address generator"
__url__ = "https://github.com/joshschmelzle/randmac"
__version__ = "0.1"
__version__ = "0.1.0"
__author__ = "Josh Schmelzle"
__author_email__ = "[email protected]"
__license__ = "MIT"
__license__ = "BSD-3-Clause"
__copyright__ = "Copyright Josh Schmelzle"
4 changes: 4 additions & 0 deletions randmac/helpers.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# /usr/bin/env python3
# -*- coding: utf-8 -*-
#
# _ _ _ _| _ _ _ _
# | (_|| |(_|| | |(_|(_
#

import argparse
import textwrap
Expand Down
25 changes: 17 additions & 8 deletions randmac/randmac.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
# /usr/bin/env python3
# -*- coding: utf-8 -*-
#
# _ _ _ _| _ _ _ _
# | (_|| |(_|| | |(_|(_
#

"""
Randomizes the 6 digit NIC portion of the MAC address required for input.
Generates a 12-digit LAA MAC address
Optional argument will generate a 12-digit LAA.
Optional argument will generate 6 digit NIC portion of the MAC address.
Supported MAC address formats:
MM:MM:MM:SS:SS:SS
Expand All @@ -14,8 +18,6 @@
"""

import random
import sys
import os
from enum import Enum


Expand Down Expand Up @@ -56,6 +58,7 @@ class Format(Enum):
def __init__(self, mac=None, generate_partial=None) -> None:
if mac is None:
mac = "11:11:11:11:11:11"
mac = mac.lower()
trimmed = self._trim_separator(mac)
_chars = []
for _char in trimmed:
Expand All @@ -77,16 +80,22 @@ def __init__(self, mac=None, generate_partial=None) -> None:
)

def __repr__(self):
return repr(self.mac)
return repr(str(self.mac))

def __str__(self):
return str(self.mac)


def __getitem__(self, item):
return str(self.mac)[item]

def __len__(self):
return len(str(self.mac))

@staticmethod
def _trim_separator(mac: str) -> str:
"""removes separator from MAC address"""
return mac.translate(str.maketrans("", "", ":-."))

return mac.replace(":","").replace("-","").replace(".","")
@staticmethod
def _set_lettercase(string: str) -> str:
"""determines lettercase for MAC address"""
Expand Down
17 changes: 12 additions & 5 deletions tests/bench.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
# /usr/bin/env python3
# -*- coding: utf-8 -*-
#
# _ _ _ _| _ _ _ _
# | (_|| |(_|| | |(_|(_
#

import time
import sys

sys.path.insert(0, "../randmac/")
import randmac
from randmac import RandMac


def bench():
Expand Down Expand Up @@ -36,10 +43,10 @@ def test():
"00000000000f",
]:
# print("input {}".format(mac))
mac = randmac.nic_portion(mac)
# print("output nic {}".format(mac))
mac = randmac.twelve_digit_mac(mac)
# print("output mac {}".format(mac))
foo = RandMac(mac, generate_partial=True)
# print("output nic {}".format(foo))
foo = RandMac(mac)
# print("output mac {}".format(foo))
# print("")


Expand Down
59 changes: 26 additions & 33 deletions tests/test_randmac.py
Original file line number Diff line number Diff line change
@@ -1,44 +1,37 @@
import pytest
# /usr/bin/env python3
# -*- coding: utf-8 -*-
#
# _ _ _ _| _ _ _ _
# | (_|| |(_|| | |(_|(_
#

import sys

sys.path.insert(0, "../randmac/")
from randmac import randmac
from randmac import RandMac


class TestRandMac(object):
def testrandomnic(self):
assert len(randmac.nic_portion("00.00.00.00.00.00")) == 17
assert len(randmac.nic_portion("0000.0000.0000")) == 14
assert "123456" in randmac.nic_portion("123456AABBCC")
class Test_RandMac(object):
def test_partial(self):
assert len(RandMac("000000000000", generate_partial=True)) == 12
assert len(RandMac("00.00.00.00.00.00", generate_partial=True)) == 17
assert len(RandMac("00-00-00-00-00-00", generate_partial=True)) == 17
assert len(RandMac("00:00:00:00:00:00", generate_partial=True)) == 17
assert len(RandMac("0000.0000.0000", generate_partial=True)) == 14
assert "123456" in str(RandMac("123456AABBCC", generate_partial=True))
assert "12:34:56" in str(RandMac("12:34:56:AA:BB:CC", generate_partial=True))

def testmac(self):
assert randmac.twelve_digit_mac("00:00:00:00:00:00")[1].lower() in [
"2",
"6",
"a",
"e",
]
assert randmac.twelve_digit_mac("00.00.00.00.00.00")[1].lower() in [
"2",
"6",
"a",
"e",
]
assert randmac.twelve_digit_mac("00-00-00-00-00-00")[1].lower() in [
"2",
"6",
"a",
"e",
]
assert randmac.twelve_digit_mac("0000.0000.0000")[1].lower() in [
"2",
"6",
"a",
"e",
]
assert randmac.twelve_digit_mac("000000000000")[1].lower() in [
def test_mac(self):
laa_hex = [
"2",
"6",
"a",
"e",
]
assert "123456" not in RandMac("123456AABBCC")
assert "12:34:56" not in RandMac("12:34:56:AA:BB:CC")
assert RandMac("00:00:00:00:00:00")[1].lower() in laa_hex
assert RandMac("00.00.00.00.00.00")[1].lower() in laa_hex
assert RandMac("00-00-00-00-00-00")[1].lower() in laa_hex
assert RandMac("0000.0000.0000")[1].lower() in laa_hex
assert RandMac("000000000000")[1].lower() in laa_hex

0 comments on commit 994ac95

Please sign in to comment.