Python code to scan network to obtain the computer name [closed]
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this questionI need to run a network scan to obtain the computer name o开发者_开发知识库f the PCs connected to the network. This is to run a software compliance scan on these systems.
We require the name of the system in order to connect to their registry to obtain information.
How do we obtain computer name or is their any other way to connect to the computers inthe network ?
You can probably just use an IP address rather than a name. System calls in Windows that requires a machine name will generally also accept a numeric address just the same.
e.g. these both work:
>>> import _winreg
>>> c = _winreg.ConnectRegistry("SOMEMACHINE", _winreg.HKEY_CLASSES_ROOT)
>>> c = _winreg.ConnectRegistry("10.10.40.9", _winreg.HKEY_CLASSES_ROOT)
Very windows specific hack that works okay.
import subprocess
def get_host(target):
pro = subprocess.Popen(['ping','-a', '-n', '1', target], stdout = subprocess.PIPE, shell = True)
return pro.communicate()[0]
def parse_hostname(result):
return result.split()[1]
def get_range(network, start, finish):
while start != finish:
target = network + start
yield parse_hostname(get_host(target))
start += 1
精彩评论