Blame


1 84302400 2020-10-03 op These days, for various reason, I'm using USB tethering very often. Enabling it it's not difficult at all, it as simple as
2 84302400 2020-10-03 op
3 84302400 2020-10-03 op ```
4 84302400 2020-10-03 op doas dhclient urndis0
5 84302400 2020-10-03 op ```
6 84302400 2020-10-03 op
7 84302400 2020-10-03 op 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.
8 84302400 2020-10-03 op
9 84302400 2020-10-03 op => https://man.openbsd.org/hotplugd Enter hotplugd(8)!
10 84302400 2020-10-03 op
11 84302400 2020-10-03 op hotplugd is a daemon that will listen for the attach/detach of various
12 84302400 2020-10-03 op devices and execute a script. It's that simple, and at the same time
13 84302400 2020-10-03 op really useful.
14 84302400 2020-10-03 op
15 84302400 2020-10-03 op 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.
16 84302400 2020-10-03 op
17 84302400 2020-10-03 op => https://man.openbsd.org/hotplugd hotplugd(8) manpage
18 84302400 2020-10-03 op
19 84302400 2020-10-03 op 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.
20 84302400 2020-10-03 op
21 84302400 2020-10-03 op 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:
22 84302400 2020-10-03 op
23 84302400 2020-10-03 op ```
24 84302400 2020-10-03 op #!/bin/sh
25 84302400 2020-10-03 op # /etc/hotplug/attach
26 84302400 2020-10-03 op
27 84302400 2020-10-03 op DEVCLASS=$1
28 84302400 2020-10-03 op DEVNAME=$2
29 84302400 2020-10-03 op
30 84302400 2020-10-03 op case $DEVCLASS in
31 84302400 2020-10-03 op # network devices
32 84302400 2020-10-03 op 3)
33 7785fefb 2020-10-03 op case $DEVNAME in
34 84302400 2020-10-03 op # USB tethering
35 84302400 2020-10-03 op urndis*) dhclient $DEVNAME ;;
36 84302400 2020-10-03 op esac
37 84302400 2020-10-03 op esac
38 84302400 2020-10-03 op ```
39 84302400 2020-10-03 op
40 84302400 2020-10-03 op Remember to make the script executable and to enable hotplugd(8):
41 84302400 2020-10-03 op
42 84302400 2020-10-03 op ```
43 84302400 2020-10-03 op # chmod +x /etc/hotplug/attach
44 84302400 2020-10-03 op # rcctl enable hotplugd
45 84302400 2020-10-03 op # rcctl start hotplugd
46 84302400 2020-10-03 op ```
47 84302400 2020-10-03 op
48 84302400 2020-10-03 op 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.
49 84302400 2020-10-03 op
50 84302400 2020-10-03 op NB: I'm not paranoid enough to worry about accidentally connect to a stranger's phone. You have been warned.