Blob


1 These days, for various reason, I'm using USB tethering very often. Enabling it it's not difficult at all, it as simple as
3 ```
4 doas dhclient urndis0
5 ```
7 But it's tedious. Especially if you need to do it multiple times per day. I needed something to save me from filling my shell history of dhclient urndis0.
9 => https://man.openbsd.org/hotplugd Enter hotplugd(8)!
11 hotplugd is a daemon that will listen for the attach/detach of various devices and execute a script. It's that simple, and at the same time really useful.
13 Disclaimer: I don't like to write howtos because the man pages are generally better, and the information in blog like this tends to rot sooner or later. I encourage you to go read the hotplugd(8) man page (it's really short, simple and understandable -- it even has some examples!) and consider this post as a "did you know?"-sort of thing.
15 => https://man.openbsd.org/hotplugd hotplugd(8) manpage
17 With the disclaimer in place, let's continue. The idea is that hotplugd will execute /etc/hotplug/attach and detach script when a device is attached or detached. It doesn't need to be a shell script, any executable file will do, but will stick with a plain old sh script.
19 When you enable your phone USB tethering, a new device called urndisN is created. So, knowing this, we just need to execute dhclient(8) inside the attach script on the correct urndis(4) devices. Easy peasy:
21 ```
22 #!/bin/sh
23 # /etc/hotplug/attach
25 DEVCLASS=$1
26 DEVNAME=$2
28 case $DEVCLASS in
29 # network devices
30 3)
31 case $DEVNAME in
32 # USB tethering
33 urndis*) dhclient $DEVNAME ;;
34 esac
35 esac
36 ```
38 Remember to make the script executable and to enable hotplugd(8):
40 ```
41 # chmod +x /etc/hotplug/attach
42 # rcctl enable hotplugd
43 # rcctl start hotplugd
44 ```
46 Every time you enable the tethering on your phone your computer will automatically connect to it. In theory the same principle can also be used to automatically mount discs when plugged, but I haven't tried yet.
48 NB: I'm not paranoid enough to worry about accidentally connect to a stranger's phone. You have been warned.