added validation checks/conversion for valid values of mac addresses and ip addresses to addaddr and sethwaddr functions
This commit is contained in:
parent
de493c810a
commit
6042c6fed9
4 changed files with 51 additions and 6 deletions
|
@ -18,7 +18,7 @@ from subprocess import PIPE, STDOUT, Popen
|
|||
|
||||
import netaddr
|
||||
|
||||
from core.errors import CoreCommandError
|
||||
from core.errors import CoreCommandError, CoreError
|
||||
|
||||
DEVNULL = open(os.devnull, "wb")
|
||||
|
||||
|
@ -425,3 +425,34 @@ def random_mac():
|
|||
mac = netaddr.EUI(value)
|
||||
mac.dialect = netaddr.mac_unix
|
||||
return str(mac)
|
||||
|
||||
|
||||
def validate_mac(value):
|
||||
"""
|
||||
Validate mac and return unix formatted version.
|
||||
|
||||
:param str value: address to validate
|
||||
:return: unix formatted mac
|
||||
:rtype: str
|
||||
"""
|
||||
try:
|
||||
mac = netaddr.EUI(value)
|
||||
mac.dialect = netaddr.mac_unix
|
||||
return str(mac)
|
||||
except netaddr.AddrFormatError as e:
|
||||
raise CoreError(f"invalid mac address {value}: {e}")
|
||||
|
||||
|
||||
def validate_ip(value):
|
||||
"""
|
||||
Validate ip address with prefix and return formatted version.
|
||||
|
||||
:param str value: address to validate
|
||||
:return: formatted ip address
|
||||
:rtype: str
|
||||
"""
|
||||
try:
|
||||
ip = netaddr.IPNetwork(value)
|
||||
return str(ip)
|
||||
except (ValueError, netaddr.AddrFormatError) as e:
|
||||
raise CoreError(f"invalid ip address {value}: {e}")
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue