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
12 devices and execute a script. It's that simple, and at the same time
13 really useful.
15 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.
17 => https://man.openbsd.org/hotplugd hotplugd(8) manpage
19 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.
21 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:
23 ```
24 #!/bin/sh
25 # /etc/hotplug/attach
27 DEVCLASS=$1
28 DEVNAME=$2
30 case $DEVCLASS in
31 # network devices
32 3)
33 case $DEVNAME in
34 # USB tethering
35 urndis*) dhclient $DEVNAME ;;
36 esac
37 esac
38 ```
40 Remember to make the script executable and to enable hotplugd(8):
42 ```
43 # chmod +x /etc/hotplug/attach
44 # rcctl enable hotplugd
45 # rcctl start hotplugd
46 ```
48 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.
50 NB: I'm not paranoid enough to worry about accidentally connect to a stranger's phone. You have been warned.