以下是一个示例代码:

import os
import platform

# 获取本机IP地址
def get_local_ip():
    return socket.gethostbyname(socket.gethostname())

# 获取本机主机名
def get_local_hostname():
    return socket.gethostname()

# 获取操作系统信息
def get_os_info():
    os_name = os.name
    if os_name == "posix":
        return platform.system() + " " + platform.release()
    elif os_name == "nt":
        return platform.system() + " " + platform.win32_ver()[0]

# 获取电脑型号
def get_computer_model():
    if platform.system() == "Windows":
        return platform.win32_computername()
    elif platform.system() == "Linux":
        return platform.uname()[1]

# 获取局域网内所有电脑信息
def get_lan_computers():
    computers = []
    ip_prefix = get_local_ip().split(".")[:-1]
    for i in range(1, 255):
        ip = ".".join(ip_prefix) + "." + str(i)
        if ip == get_local_ip():
            continue
        try:
            hostname = socket.gethostbyaddr(ip)[0]
            os_info = get_os_info()
            model = get_computer_model()
            computers.append((hostname, ip, os_info, model))
        except socket.herror:
            pass
    return computers

# 将信息写入文本文件
def write_to_file(computers):
    with open(os.path.join(os.path.expanduser("~"), "Desktop", "lan_computers.txt"), "w") as f:
        for computer in computers:
            f.write("主机名:" + computer[0] + "\n")
            f.write("IP地址:" + computer[1] + "\n")
            f.write("操作系统:" + computer[2] + "\n")
            f.write("电脑型号:" + computer[3] + "\n")
            f.write("\n")

if __name__ == "__main__":
    computers = get_lan_computers()
    write_to_file(computers)

该代码使用了Python内置的socket、os、platform等模块,可以获取本机IP地址、主机名、操作系统信息、电脑型号等,并通过遍历局域网内所有IP地址,获取局域网内所有电脑的信息,并将其写入桌面上的文本文件中。运行该代码需要管理员权限

获取局域网内所有电脑信息包括主机名、IP、操作系统、电脑型号等并存储到桌面文本文件内python语言

原文地址: https://www.cveoy.top/t/topic/dxA1 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录