获取局域网内所有电脑信息包括主机名、IP、操作系统、电脑型号等并存储到桌面文本文件内然后打开文件python可执行程序
以下是Python代码实现:
import os
import platform
def get_local_computers_info():
computers = []
# 获取本机ip
local_ip = os.popen("ipconfig | findstr IPv4 | findstr 192").read().split(":")[1].strip()
# 获取本机主机名
host_name = platform.node()
computers.append({"主机名": host_name, "IP": local_ip, "操作系统": platform.system()})
# 获取局域网内所有电脑信息
ip_prefix = local_ip.rsplit(".", 1)[0] + "."
for i in range(1, 255):
ip = ip_prefix + str(i)
if ip != local_ip:
res = os.system("ping -n 1 -w 1000 " + ip)
if res == 0: # ping成功,说明该ip对应的电脑在线
try:
host_name = os.popen("nslookup " + ip + " | findstr Name").read().split("=")[1].strip()
except:
host_name = "未知"
computers.append({"主机名": host_name, "IP": ip, "操作系统": get_remote_computer_os(ip)})
return computers
def get_remote_computer_os(ip):
os_info = os.popen("wmic /node:" + ip + " os get Caption").read().split("\n")[1].strip()
if not os_info:
os_info = "未知"
return os_info
def save_to_file(computers):
with open("局域网电脑信息.txt", "w") as file:
for computer in computers:
file.write(str(computer) + "\n")
if __name__ == '__main__':
computers = get_local_computers_info()
save_to_file(computers)
os.system("start 局域网电脑信息.txt")
该程序通过ipconfig命令获取本机IP和主机名,然后通过ping扫描局域网内所有IP,再通过nslookup命令获取主机名和wmic命令获取操作系统信息,最后将所有信息存储到文件中。最后使用os.system命令打开文本文件
原文地址: https://www.cveoy.top/t/topic/dxGX 著作权归作者所有。请勿转载和采集!