You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过USB驱动器序列号定位设备并实现自动化文件保存?

通过USB序列号定位驱动器并自动保存文件

核心问题分析

你之前的错误是把U盘序列号当成了文件路径,实际上需要先通过序列号映射到对应的驱动器盘符/挂载点,再用这个路径来保存文件。下面分系统给出具体实现方案,以Python为例(适合自动化脚本开发)。


Windows 平台实现

1. 获取U盘序列号

先执行命令查看所有可移动磁盘的序列号:

wmic diskdrive get serialnumber

注意序列号可能包含空格,要完整复制目标U盘的序列号(比如1234567890 )。

2. 代码实现(通过序列号找盘符+保存IP+快捷键触发)

先安装依赖库:

pip install wmi keyboard

完整代码:

import wmi
import socket
import keyboard

def get_usb_drive_by_serial(target_serial):
    c = wmi.WMI()
    # 遍历所有可移动磁盘(DriveType=2)
    for disk in c.Win32_LogicalDisk(DriveType=2):
        # 关联到磁盘分区
        for partition in disk.associators("Win32_LogicalDiskToPartition"):
            # 关联到物理磁盘驱动器
            for disk_drive in partition.associators("Win32_DiskDriveToDiskPartition"):
                # 匹配序列号(去除首尾空格避免误差)
                if disk_drive.SerialNumber and target_serial.strip() == disk_drive.SerialNumber.strip():
                    return disk.DeviceID  # 返回盘符,如E:\
    return None

def get_local_ip():
    # 获取本地联网IP(根据需求调整,比如取特定网卡IP)
    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    try:
        # 连接外部地址触发网卡绑定
        s.connect(("8.8.8.8", 80))
        ip = s.getsockname()[0]
    finally:
        s.close()
    return ip

def save_ip_to_usb():
    # 替换为你的U盘序列号
    TARGET_SERIAL = "1234567890"
    drive_path = get_usb_drive_by_serial(TARGET_SERIAL)
    
    if drive_path:
        ip_addr = get_local_ip()
        save_path = f"{drive_path}\\ip_record.txt"
        with open(save_path, "w", encoding="utf-8") as f:
            f.write(ip_addr)
        print(f"IP已保存到: {save_path}")
    else:
        print("未找到目标序列号的U盘")

# 绑定快捷键:Ctrl+Shift+S 触发保存
keyboard.add_hotkey('ctrl+shift+s', save_ip_to_usb)
# 保持脚本运行监听按键
keyboard.wait()

注意事项

  • 脚本需要管理员权限运行(否则WMI查询可能失败)
  • 序列号要完全匹配,注意原输出中的空格和大小写

Linux/macOS 平台实现

1. 获取U盘序列号

  • Linux:执行 ls /dev/disk/by-id/,找到带usb前缀且包含目标U盘标识的条目,提取序列号部分
  • macOS:执行 diskutil info /dev/disk2s1(替换为你的U盘设备名),找到Serial Number字段

2. 代码示例(Linux)

import subprocess
import socket
import keyboard

def get_usb_mount_point(target_serial):
    # 查找对应序列号的设备路径
    result = subprocess.run(['ls', '/dev/disk/by-id/'], capture_output=True, text=True)
    for line in result.stdout.splitlines():
        if target_serial in line and 'usb' in line:
            # 获取真实设备路径
            device = subprocess.run(['readlink', '-f', line], capture_output=True, text=True).stdout.strip()
            # 查找挂载点
            df_result = subprocess.run(['df', device], capture_output=True, text=True)
            return df_result.stdout.splitlines()[1].split()[5]
    return None

def get_local_ip():
    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    try:
        s.connect(("8.8.8.8", 80))
        ip = s.getsockname()[0]
    finally:
        s.close()
    return ip

def save_ip_to_usb():
    TARGET_SERIAL = "usb-XXXXXX-0:0"  # 替换为你的U盘序列号标识
    mount_point = get_usb_mount_point(TARGET_SERIAL)
    
    if mount_point:
        ip_addr = get_local_ip()
        save_path = f"{mount_point}/ip_record.txt"
        with open(save_path, "w", encoding="utf-8") as f:
            f.write(ip_addr)
        print(f"IP已保存到: {save_path}")
    else:
        print("未找到目标U盘")

keyboard.add_hotkey('ctrl+shift+s', save_ip_to_usb)
keyboard.wait()

内容的提问来源于stack exchange,提问作者JerryTHP

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.11 13:55:36