<< Click to Display Table of Contents >> Raynet One > 1.1 > User Guide > Technical overview > Custom inventory scripting > Python inventory scripting Use case: fetching local hostnames |
Hostnames known to devices are critical resources. They define the IP network destination for navigation to actual resources. Keep good watch over local hostname registrations of each single device. Maintain and verify established organization network hostnames mitigating the impact of DNS server downtimes. Properly extend your corporate IT services by assigning and registering strong hostnames.
Supported OS types: Windows, Linux
def main():
osf = PythonConnector.os_family()
lines = None
if osf == "Windows":
lines = PythonConnector.run("type C:\\Windows\\System32\\drivers\\etc\\hosts")
elif osf == "Linux":
lines = PythonConnector.run("cat /etc/hosts")
else:
print( "unable to get local hostname resolutions for unsupported platform" )
return
if lines:
for L in lines:
if len(L) > 0 and L[0] != '#':
TKS = L.split()
if len(TKS) >= 2:
for HN in TKS[1:]:
HR = PythonConnector.create_item("LocalHostname", HN)
PythonConnector.add_property("IPAddress", TKS[0], HR)
PythonConnector.add_item(HR)
print( "collected local hostname resolution inventory" )
else:
print( "failed to read the hosts file" )
main()