updated _modules state

This commit is contained in:
Paul 2023-03-10 00:09:04 +01:00
parent c779a0e4e8
commit 9271de25fb
2 changed files with 47 additions and 2 deletions

View File

@ -1,3 +1,48 @@
#!/usr/bin/python3 #!/usr/bin/python3
ip_cidr_regex="\d{0,3}\.\d{0,3}\.\d{0,3}\.\d{0,3}\/(\d+)" import re
import json
import urllib
from urllib.request import urlopen, Request
from urllib.parse import urljoin
from urllib.error import HTTPError
MIN_CIDR=32
CIDR_REGEX = re.compile(r"(.*)/(.*)")
POSTFIX_FORMAT = lambda ip: "[{}]/{}".format(CIDR_REGEX.findall(ip)[0][0],
CIDR_REGEX.findall(ip)[0][1])
def __virtual__():
return True
def get_public_ip(url="https://ipinfo.io"):
req = Request(url=url,)
req.add_header("Accept", "*/*")
res = urlopen(req)
ret = json.loads(res.read())
if res.status == 200:
return "{}/{}".format(ret["ip"],MIN_CIDR)
return None
def get_internal_networks(tgt="*", fmt="std"):
res = []
res.extend([net for k, net in __salt__["mine.get"](tgt=tgt, fun='public_ip', tgt_type='compound').items()])
res.extend([net for k, nets in __salt__["mine.get"](tgt=tgt, fun='ip_networks', tgt_type='compound').items() for net in nets if __salt__['network.is_private'](net.split("/")[0])])
if fmt == "std":
res.extend([net for k, nets in __salt__["mine.get"](tgt=tgt, fun='ip_networks6', tgt_type='compound').items() for net in nets])
elif fmt == "postfix":
res.extend([POSTFIX_FORMAT(net) for k, nets in __salt__["mine.get"](tgt=tgt, fun='ip_networks6', tgt_type='compound').items() for net in nets])
res = list(set(res))
res.sort()
return res
def get_nat_networks(tgt="*"):
res = []
res.extend([net for k, nets in __salt__["mine.get"](tgt=tgt, fun='ip_networks', tgt_type='compound').items() for net in nets if __salt__['network.is_private'](net.split("/")[0])])
res = list(set(res))
res.sort()
return res

View File

@ -11,4 +11,4 @@ def get_version(configfile=None):
res = regex.match(l.strip("\n")) res = regex.match(l.strip("\n"))
if res: if res:
return ".".join(res.groups()) return ".".join(res.groups())
return "" return None