Skip to content

Instantly share code, notes, and snippets.

@echoes341
Last active July 18, 2024 02:47
Show Gist options
  • Save echoes341/66c61923660b590f82f663f49a88a76e to your computer and use it in GitHub Desktop.
Save echoes341/66c61923660b590f82f663f49a88a76e to your computer and use it in GitHub Desktop.
USB/IP Auto bind all usb devices (except one) and auto attach them on a windows client every 5 seconds
ACTION=="add", SUBSYSTEMS=="usb", ATTR{idProduct}!="XXXX", ATTR{idVendor}!="YYYY", RUN{program}+="/usr/sbin/usbip bind -b %k"
On the linux server (the one sharing USB):
1. Copy usbip.conf to /etc/modules-load.d/usbip.conf
2. systemctl enable --now usbipd
3. Copy 99-usbip.rules to /etc/udev/rules.d/99-usbip.rules, change the XXXX and YYYY to the product ID and vendor ID you want to exclude from auto binding, then reboot.
Some interesting commands that I will forget:
- udevadm control --log-priority=debug and then journalctl -f
- udevadm info -a -p <device path> (device path can be found using usbip list -l, in the form of /sys/devices/pci0000:00/0000:00:1a.0/usb1/1-1/1-1.4 )
- udevadm monitor --property
- udevadm control --reload-rules && udevadm trigger
- udevadm test $(udevadm info -q path -n <device path>) (a different device path in the form of /dev/bus/usb/002/006)
# Install https://github.com/vadimgrn/usbip-win2
# Check for admin rights
if (-NOT ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) {
# Relaunch the script with admin rights
Start-Process powershell -ArgumentList "-NoProfile -ExecutionPolicy Bypass -File `"$PSCommandPath`"" -Verb RunAs
exit
}
$remoteHost = "XXX"
$sleepTime = 5
while ($true) {
usbip list -r $remoteHost |
Where-Object {
$_ -match "^\s*(\d+-\d+(\.\d+)?)(?=\s*:)"
} |
ForEach-Object {
$device = $matches[1]
Write-Host "Attaching device: $device"
Invoke-Expression "usbip attach -r $remoteHost -b $device"
}
Start-Sleep -Seconds $sleepTime
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment