首页 > 解决方案 > USB端口监控

问题描述

我需要监控 Windows 和 Linux 计算机的 USB 端口,以了解是否连接了笔式驱动器。

我怎么做?

我需要它通过 Linux 脚本或 Windows。

标签: linuxbashpowershellusb

解决方案


您可以使用 Windows 上的 WMI 事件(仅在 Windows上,因为 WMI,Windows Management Instrumentation 仅在 Windows 上可用)在设备状态更改时执行操作。

假设您在 Windows 上,并使用 PowerShell,以下是您在DeviceChangeEvent期间触发事件的方式,例如添加 USB 驱动器时。

Register-WMIEvent -query "Select * From Win32_DeviceChangeEvent where EventType = '2'" `
-action {
   #Do Something when a device is added
   Write-host "Device added at $(Get-date)"
} 

当我插入一个连接了很多端口和设备的 USB 集线器时,我看到的是:

Device added at 06/10/2021 09:27:59
Device added at 06/10/2021 09:27:59
Device added at 06/10/2021 09:28:00
Device added at 06/10/2021 09:28:00
Device added at 06/10/2021 09:28:00

推荐阅读