golden hour
/opt/saltstack/salt/lib/python3.10/site-packages/salt/utils
⬆️ Go Up
Upload
File/Folder
Size
Actions
__init__.py
237 B
Del
OK
__pycache__
-
Del
OK
aggregation.py
5.17 KB
Del
OK
ansible.py
1.48 KB
Del
OK
args.py
18.33 KB
Del
OK
asynchronous.py
4.06 KB
Del
OK
atomicfile.py
5.33 KB
Del
OK
aws.py
20.37 KB
Del
OK
azurearm.py
11.42 KB
Del
OK
beacons.py
517 B
Del
OK
boto3mod.py
8.36 KB
Del
OK
boto_elb_tag.py
3.02 KB
Del
OK
botomod.py
7.98 KB
Del
OK
cache.py
11.49 KB
Del
OK
channel.py
489 B
Del
OK
cloud.py
116.3 KB
Del
OK
color.py
2.72 KB
Del
OK
compat.py
1.89 KB
Del
OK
configcomparer.py
3.88 KB
Del
OK
configparser.py
10.82 KB
Del
OK
context.py
6.8 KB
Del
OK
crypt.py
5 KB
Del
OK
ctx.py
1.42 KB
Del
OK
data.py
53.03 KB
Del
OK
dateutils.py
2.3 KB
Del
OK
debug.py
4.19 KB
Del
OK
decorators
-
Del
OK
dictdiffer.py
16.48 KB
Del
OK
dicttrim.py
3.9 KB
Del
OK
dictupdate.py
11.33 KB
Del
OK
dns.py
35.21 KB
Del
OK
doc.py
2.25 KB
Del
OK
dockermod
-
Del
OK
entrypoints.py
1.83 KB
Del
OK
environment.py
2.2 KB
Del
OK
error.py
1.18 KB
Del
OK
etcd_util.py
33.25 KB
Del
OK
event.py
52.45 KB
Del
OK
extend.py
8.87 KB
Del
OK
extmods.py
6.04 KB
Del
OK
filebuffer.py
3.15 KB
Del
OK
files.py
27.94 KB
Del
OK
find.py
22.08 KB
Del
OK
fsutils.py
3.29 KB
Del
OK
functools.py
6.02 KB
Del
OK
gitfs.py
130.41 KB
Del
OK
github.py
1.52 KB
Del
OK
gzip_util.py
2.86 KB
Del
OK
hashutils.py
5.91 KB
Del
OK
http.py
33.9 KB
Del
OK
iam.py
1.22 KB
Del
OK
icinga2.py
754 B
Del
OK
idem.py
1.22 KB
Del
OK
immutabletypes.py
2.46 KB
Del
OK
itertools.py
2.36 KB
Del
OK
jid.py
3 KB
Del
OK
jinja.py
33.92 KB
Del
OK
job.py
6.89 KB
Del
OK
json.py
3.78 KB
Del
OK
kickstart.py
41.04 KB
Del
OK
kinds.py
493 B
Del
OK
lazy.py
3.06 KB
Del
OK
listdiffer.py
10.9 KB
Del
OK
locales.py
2.06 KB
Del
OK
mac_utils.py
14.01 KB
Del
OK
mako.py
3.97 KB
Del
OK
master.py
29.7 KB
Del
OK
mattermost.py
1.77 KB
Del
OK
memcached.py
3.56 KB
Del
OK
migrations.py
1.46 KB
Del
OK
mine.py
3.68 KB
Del
OK
minion.py
4.13 KB
Del
OK
minions.py
43.38 KB
Del
OK
mount.py
1.15 KB
Del
OK
msazure.py
5.36 KB
Del
OK
msgpack.py
4.69 KB
Del
OK
nacl.py
13.65 KB
Del
OK
namecheap.py
4.32 KB
Del
OK
napalm.py
23.22 KB
Del
OK
nb_popen.py
7.24 KB
Del
OK
network.py
73.86 KB
Del
OK
nxos.py
12.94 KB
Del
OK
nxos_api.py
4 KB
Del
OK
odict.py
13.21 KB
Del
OK
openstack
-
Del
OK
oset.py
6.41 KB
Del
OK
pagerduty.py
3.03 KB
Del
OK
parsers.py
122.69 KB
Del
OK
path.py
11.24 KB
Del
OK
pbm.py
9.81 KB
Del
OK
pkg
-
Del
OK
platform.py
5.84 KB
Del
OK
powershell.py
4.15 KB
Del
OK
preseed.py
2.64 KB
Del
OK
process.py
40.76 KB
Del
OK
profile.py
3.21 KB
Del
OK
proxy.py
331 B
Del
OK
psutil_compat.py
3.63 KB
Del
OK
pushover.py
4.51 KB
Del
OK
pycrypto.py
5.41 KB
Del
OK
pydsl.py
13.74 KB
Del
OK
pyobjects.py
10.75 KB
Del
OK
reactor.py
18.99 KB
Del
OK
reclass.py
752 B
Del
OK
roster_matcher.py
3.55 KB
Del
OK
rsax931.py
8.42 KB
Del
OK
s3.py
8.78 KB
Del
OK
saltclass.py
14.27 KB
Del
OK
sanitizers.py
2.51 KB
Del
OK
schedule.py
71.81 KB
Del
OK
schema.py
54.26 KB
Del
OK
sdb.py
4.04 KB
Del
OK
slack.py
3.58 KB
Del
OK
smb.py
11.16 KB
Del
OK
smtp.py
3.27 KB
Del
OK
ssdp.py
14.75 KB
Del
OK
ssh.py
769 B
Del
OK
state.py
8.43 KB
Del
OK
stringio.py
355 B
Del
OK
stringutils.py
16.95 KB
Del
OK
systemd.py
5.51 KB
Del
OK
templates.py
24.03 KB
Del
OK
textformat.py
5.03 KB
Del
OK
thin.py
31.91 KB
Del
OK
timed_subprocess.py
4.06 KB
Del
OK
timeout.py
1.53 KB
Del
OK
timeutil.py
2.4 KB
Del
OK
url.py
5 KB
Del
OK
user.py
11.86 KB
Del
OK
validate
-
Del
OK
value.py
247 B
Del
OK
vault.py
21.74 KB
Del
OK
verify.py
25.34 KB
Del
OK
versions.py
17.17 KB
Del
OK
virt.py
3.24 KB
Del
OK
virtualbox.py
22.43 KB
Del
OK
vmware.py
129.74 KB
Del
OK
vsan.py
17.18 KB
Del
OK
vt.py
31.47 KB
Del
OK
vt_helper.py
4.4 KB
Del
OK
win_chcp.py
3.7 KB
Del
OK
win_dacl.py
95.49 KB
Del
OK
win_dotnet.py
4.74 KB
Del
OK
win_functions.py
12.69 KB
Del
OK
win_lgpo_auditpol.py
8.48 KB
Del
OK
win_lgpo_netsh.py
17.87 KB
Del
OK
win_lgpo_reg.py
16.98 KB
Del
OK
win_network.py
16.35 KB
Del
OK
win_osinfo.py
2.83 KB
Del
OK
win_pdh.py
13.85 KB
Del
OK
win_reg.py
30.82 KB
Del
OK
win_runas.py
10.53 KB
Del
OK
win_service.py
5.2 KB
Del
OK
win_system.py
14.47 KB
Del
OK
win_update.py
40.36 KB
Del
OK
winapi.py
818 B
Del
OK
x509.py
73.19 KB
Del
OK
xdg.py
316 B
Del
OK
xmlutil.py
13.91 KB
Del
OK
yaml.py
349 B
Del
OK
yamldumper.py
3.37 KB
Del
OK
yamlencoding.py
1.55 KB
Del
OK
yamllint.py
1.61 KB
Del
OK
yamlloader.py
6.04 KB
Del
OK
yamlloader_old.py
8.15 KB
Del
OK
yast.py
619 B
Del
OK
zeromq.py
1.74 KB
Del
OK
zfs.py
19.15 KB
Del
OK
Edit: win_network.py
""" This salt util uses WMI to gather network information on Windows 7 and .NET 4.0+ on newer systems. The reason for this is that calls to WMI tend to be slower. Especially if the query has not been optimized. For example, timing to gather NIC info from WMI and .NET were as follows in testing: WMI: 3.4169998168945312 seconds NET: 1.0390000343322754 seconds Since this is used to generate grain information we want to avoid using WMI as much as possible. There are 3 functions in this salt util. - get_interface_info_dot_net - get_interface_info_wmi - get_interface_info The ``get_interface_info`` function will call one of the other two functions depending on the version of Windows this is run on. Once support for Windows 7 is dropped we can remove the WMI stuff and just use .NET. :depends: - pythonnet - wmi """ # https://docs.microsoft.com/en-us/dotnet/api/system.net.networkinformation.networkinterface.getallnetworkinterfaces?view=netframework-4.7.2 import logging import platform import salt.utils.win_reg from salt._compat import ipaddress IS_WINDOWS = platform.system() == "Windows" log = logging.getLogger(__name__) __virtualname__ = "win_network" if IS_WINDOWS: # pythonnet 3.0.1 requires .NET 4.7.2 (461808). This isn't installed by # default until Windows Server 2019 / Windows 10 1809 (10.1.17763). But, it # can be installed on older versions of Windows. So, instead of checking # platform here, let's check the version of .NET net_release = salt.utils.win_reg.read_value( hive="HKLM", key=r"SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full", vname="Release", ) # If the registry key is not found, or the value is less than 461808, we # need to use WMI if not net_release["success"] or net_release["vdata"] < 461808: USE_WMI = True else: USE_WMI = False if USE_WMI: # This is supported by all versions of Windows, but the database we're # using hasn't really been optimized, so it is much slower import wmi import salt.utils.winapi else: # This uses .NET to get network settings and is faster than WMI try: # pylint: disable=unused-import import clr # pylint: enable=unused-import from System.Net import NetworkInformation except RuntimeError: # In some environments, using the Relenv OneDir package, we can't # load pythonnet. Uninstalling and reinstalling pythonnet fixes the # issue, but it is a manual step. Until we figure it out, we are # just going to fall back to WMI. I was able to reproduce a failing # system using Windows 10 Home Edition log.debug("Failed to load pythonnet. Falling back to WMI") USE_WMI = True import wmi import salt.utils.winapi # TODO: Should we deprecate support for pythonnet 2.5.2, these enumerations can # TODO: be deleted enum_adapter_types = { 1: "Unknown", 6: "Ethernet", 9: "TokenRing", 15: "FDDI", 20: "BasicISDN", 21: "PrimaryISDN", 23: "PPP", 24: "Loopback", 26: "Ethernet3Megabit", 28: "Slip", 37: "ATM", 48: "GenericModem", 53: "TAPAdapter", # Not in MSDN Defined enumeration 62: "FastEthernetT", 63: "ISDN", 69: "FastEthernetFx", 71: "Wireless802.11", 94: "AsymmetricDSL", 95: "RateAdaptDSL", 96: "SymmetricDSL", 97: "VeryHighSpeedDSL", 114: "IPOverATM", 117: "GigabitEthernet", 131: "Tunnel", 143: "MultiRateSymmetricDSL", 144: "HighPerformanceSerialBus", 237: "WMAN", 243: "WWANPP", 244: "WWANPP2", } enum_operational_status = { 1: "Up", 2: "Down", 3: "Testing", 4: "Unknown", 5: "Dormant", 6: "NotPresent", 7: "LayerDown", } enum_prefix_suffix = { 0: "Other", 1: "Manual", 2: "WellKnown", 3: "DHCP", 4: "Router", 5: "Random", } af_inet = 2 af_inet6 = 23 def __virtual__(): """ Only load if windows """ if not IS_WINDOWS: return False, "This utility will only run on Windows" return __virtualname__ def _get_base_properties(i_face): raw_mac = i_face.GetPhysicalAddress().ToString() try: # pythonnet 3.0.1 i_face_type = i_face.NetworkInterfaceType.ToString() except AttributeError: # pythonnet 2.5.2 try: i_face_type = enum_adapter_types[i_face.NetworkInterfaceType] except KeyError: i_face_type = i_face.Description ret = { "alias": i_face.Name, "description": i_face.Description, "id": i_face.Id, "receive_only": i_face.IsReceiveOnly, "type": i_face_type, "physical_address": ":".join(raw_mac[i : i + 2] for i in range(0, 12, 2)), } try: # pythonnet 3.0.1 ret["status"] = i_face.OperationalStatus.ToString() except AttributeError: # pythonnet 2.5.2 ret["status"] = enum_operational_status[i_face.OperationalStatus] return ret def _get_ip_base_properties(i_face): ip_properties = i_face.GetIPProperties() return { "dns_suffix": ip_properties.DnsSuffix, "dns_enabled": ip_properties.IsDnsEnabled, "dynamic_dns_enabled": ip_properties.IsDynamicDnsEnabled, } def _get_ip_unicast_info(i_face): ip_properties = i_face.GetIPProperties() int_dict = {} if ip_properties.UnicastAddresses.Count > 0: names = {af_inet: "ip_addresses", af_inet6: "ipv6_addresses"} for addrs in ip_properties.UnicastAddresses: try: # pythonnet 3.0.1 if addrs.Address.AddressFamily.ToString() == "InterNetwork": family = 2 else: family = 23 except AttributeError: # pythonnet 2.5.2 family = addrs.Address.AddressFamily if family == af_inet: ip = addrs.Address.ToString() mask = addrs.IPv4Mask.ToString() net = ipaddress.IPv4Network(ip + "/" + mask, False) ip_info = { "address": ip, "netmask": mask, "broadcast": net.broadcast_address.compressed, "loopback": addrs.Address.Loopback.ToString(), } else: ip_info = { "address": addrs.Address.ToString().split("%")[0], # ScopeID is a suffix affixed to the end of an IPv6 # address it denotes the adapter. This is different from # ScopeLevel. Need to figure out how to get ScopeLevel # for feature parity with Linux "interface_index": int(addrs.Address.ScopeId), } ip_info.update({"prefix_length": addrs.PrefixLength}) try: # pythonnet 3.0.1 ip_info.update( {"prefix_origin": addrs.PrefixOrigin.ToString()}, ) except AttributeError: # pythonnet 2.5.2 ip_info.update( {"prefix_origin": enum_prefix_suffix[addrs.PrefixOrigin]}, ) try: # pythonnet 3.0.1 ip_info.update( {"suffix_origin": addrs.SuffixOrigin.ToString()}, ) except AttributeError: # pythonnet 2.5.2 ip_info.update( {"suffix_origin": enum_prefix_suffix[addrs.SuffixOrigin]}, ) int_dict.setdefault(names[family], []).append(ip_info) return int_dict def _get_ip_gateway_info(i_face): ip_properties = i_face.GetIPProperties() int_dict = {} if ip_properties.GatewayAddresses.Count > 0: names = {af_inet: "ip_gateways", af_inet6: "ipv6_gateways"} for addrs in ip_properties.GatewayAddresses: try: # pythonnet 3.0.1 if addrs.Address.AddressFamily.ToString() == "InterNetwork": family = 2 else: family = 23 except AttributeError: # pythonnet 2.5.2 family = addrs.Address.AddressFamily int_dict.setdefault(names[family], []).append( addrs.Address.ToString().split("%")[0] ) return int_dict def _get_ip_dns_info(i_face): ip_properties = i_face.GetIPProperties() int_dict = {} if ip_properties.DnsAddresses.Count > 0: names = {af_inet: "ip_dns", af_inet6: "ipv6_dns"} for addrs in ip_properties.DnsAddresses: try: # pythonnet 3.0.1 if addrs.AddressFamily.ToString() == "InterNetwork": family = 2 else: family = 23 except AttributeError: # pythonnet 2.5.2 family = addrs.AddressFamily int_dict.setdefault(names[family], []).append( addrs.ToString().split("%")[0] ) return int_dict def _get_ip_multicast_info(i_face): ip_properties = i_face.GetIPProperties() int_dict = {} if ip_properties.MulticastAddresses.Count > 0: names = {af_inet: "ip_multicast", af_inet6: "ipv6_multicast"} for addrs in ip_properties.MulticastAddresses: try: # pythonnet 3.0.1 if addrs.Address.AddressFamily.ToString() == "InterNetwork": family = 2 else: family = 23 except AttributeError: # pythonnet 2.5.2 family = addrs.Address.AddressFamily int_dict.setdefault(names[family], []).append( addrs.Address.ToString().split("%")[0] ) return int_dict def _get_ip_anycast_info(i_face): ip_properties = i_face.GetIPProperties() int_dict = {} if ip_properties.AnycastAddresses.Count > 0: names = {af_inet: "ip_anycast", af_inet6: "ipv6_anycast"} for addrs in ip_properties.AnycastAddresses: try: # pythonnet 3.0.1 if addrs.Address.AddressFamily.ToString() == "InterNetwork": family = 2 else: family = 23 except AttributeError: # pythonnet 2.5.2 family = addrs.Address.AddressFamily int_dict.setdefault(names[family], []).append(addrs.Address.ToString()) return int_dict def _get_ip_wins_info(i_face): ip_properties = i_face.GetIPProperties() int_dict = {} if ip_properties.WinsServersAddresses.Count > 0: for addrs in ip_properties.WinsServersAddresses: int_dict.setdefault("ip_wins", []).append(addrs.ToString()) return int_dict def _get_network_interfaces(): return NetworkInformation.NetworkInterface.GetAllNetworkInterfaces() def get_interface_info_dot_net_formatted(): """ Returns data gathered via ``get_interface_info_dot_net`` and returns the info in the same manner as ``get_interface_info_wmi`` Returns: dict: A dictionary of information about all interfaces on the system """ # Massage the data returned by dotnet to mirror that returned by wmi interfaces = get_interface_info_dot_net() i_faces = {} for i_face in interfaces: if interfaces[i_face]["status"] == "Up": name = interfaces[i_face]["description"] i_faces.setdefault(name, {}).update( {"hwaddr": interfaces[i_face]["physical_address"], "up": True} ) for ip in interfaces[i_face].get("ip_addresses", []): i_faces[name].setdefault("inet", []).append( { "address": ip["address"], "broadcast": ip["broadcast"], "netmask": ip["netmask"], "gateway": interfaces[i_face].get("ip_gateways", [""])[0], "label": name, } ) for ip in interfaces[i_face].get("ipv6_addresses", []): i_faces[name].setdefault("inet6", []).append( { "address": ip["address"], "gateway": interfaces[i_face].get("ipv6_gateways", [""])[0], "prefixlen": ip["prefix_length"], } ) return i_faces def get_interface_info_dot_net(): """ Uses .NET 4.0+ to gather Network Interface information. Should only run on Windows systems newer than Windows 7/Server 2008R2 Returns: dict: A dictionary of information about all interfaces on the system """ interfaces = {} for i_face in _get_network_interfaces(): temp_dict = _get_base_properties(i_face) temp_dict.update(_get_ip_base_properties(i_face)) temp_dict.update(_get_ip_unicast_info(i_face)) temp_dict.update(_get_ip_gateway_info(i_face)) temp_dict.update(_get_ip_dns_info(i_face)) temp_dict.update(_get_ip_multicast_info(i_face)) temp_dict.update(_get_ip_anycast_info(i_face)) temp_dict.update(_get_ip_wins_info(i_face)) interfaces[i_face.Name] = temp_dict return interfaces def get_interface_info_wmi(): """ Uses WMI to gather Network Interface information. Should only run on Windows 7/2008 R2 and lower systems. This code was pulled from the ``win_interfaces`` function in ``salt.utils.network`` unchanged. Returns: dict: A dictionary of information about all interfaces on the system """ with salt.utils.winapi.Com(): c = wmi.WMI() i_faces = {} for i_face in c.Win32_NetworkAdapterConfiguration(IPEnabled=1): i_faces[i_face.Description] = {} if i_face.MACAddress: i_faces[i_face.Description]["hwaddr"] = i_face.MACAddress if i_face.IPEnabled: i_faces[i_face.Description]["up"] = True for ip in i_face.IPAddress: if "." in ip: if "inet" not in i_faces[i_face.Description]: i_faces[i_face.Description]["inet"] = [] item = {"address": ip, "label": i_face.Description} if i_face.DefaultIPGateway: broadcast = next( (i for i in i_face.DefaultIPGateway if "." in i), "" ) if broadcast: item["broadcast"] = broadcast if i_face.IPSubnet: netmask = next((i for i in i_face.IPSubnet if "." in i), "") if netmask: item["netmask"] = netmask i_faces[i_face.Description]["inet"].append(item) if ":" in ip: if "inet6" not in i_faces[i_face.Description]: i_faces[i_face.Description]["inet6"] = [] item = {"address": ip} if i_face.DefaultIPGateway: broadcast = next( (i for i in i_face.DefaultIPGateway if ":" in i), "" ) if broadcast: item["broadcast"] = broadcast if i_face.IPSubnet: prefixlen = next( (int(i) for i in i_face.IPSubnet if "." not in i), None ) if prefixlen: item["prefixlen"] = prefixlen i_faces[i_face.Description]["inet6"].append(item) else: i_faces[i_face.Description]["up"] = False return i_faces def get_interface_info(): """ This function will return network interface information for the system and will use the best method to retrieve that information. Windows 7/2008R2 and below will use WMI. Newer systems will use .NET. Returns: dict: A dictionary of information about the Network interfaces """ # On Windows 7 machines, use WMI as dotnet 4.0 is not available by default if USE_WMI: return get_interface_info_wmi() else: return get_interface_info_dot_net_formatted()
Save