Running command when USB/iPhone is plugged in
-
I am succesfully running internet tethering via my iPhone 14. However, everytime I plug it in I have to run these commands to get the network going and give it an ip address:
ifconfig eth1 up
udhcpc -i eth1How can I have a shell script or something like that run these two commands everytime I get my iPhone plugged in.
These are the connection logs:
[ 2672.632679] ipheth 1-1.3:4.2: Apple iPhone USB Ethernet device attached
[ 2672.361490] usb 1-1.3: new high-speed USB device number 14
-
@baby-Onioneer You can create a hotplug script to run when the device is added. Take look at the hotplug docs for the actions supported. In simple terms, you create shell script in /etc/hotplug.d/usb that runs your commands when the device is added.
Here is an example of one I use to run whenever my wifi adapter comes up:
#!/bin/sh # # File: 40-setup # Author: chris # # Created on 25 Dec. 2024, 4:05:00 am # # Place me in /etc/hotplug.d/iface/40-setup WIFI_INTERFACE="wwan" WIFI_ACTION="ifup" SETUP_FILE=/etc/myapp/setup.sh if [ "$INTERFACE" = "$WIFI_INTERFACE" ]; then if [ "$ACTION" = "$WIFI_ACTION" ]; then echo "nameserver $(ip -o -4 a s apcli0 | awk '{ print $4 }' | cut -d/ -f1)" >> /etc/resolv.conf [ -f $SETUP_FILE ] && sh $SETUP_FILE || echo "" fi fi
-
@crispyoz I had this script and sometimes it runs sometimes it does not, could it be a physical issue with the cable? I do also have another label 10 script so not sure if thats what messes it up since they both have the same priority for execution? I am still unfamiliar with that, i understand lower numbers get priority execution but not much more.
#!/bin/sh # Hotplug script for iPhone tethering #/etc/hotplug.d/usb/10-iphone-tethering if [ "$ACTION" = "add" ]; then logger "iPhone detected, PRODUCT=$PRODUCT, bringing up eth1" sleep 2 ifconfig eth1 up udhcpc -i eth1 fi
/etc/hotplug.d/usb$ ls 10-iphone-tethering 20-usb_mode 10-motion 22-qmi_wwan
-
@baby-Onioneer I debug my scripts by adding a log entry before the "if" that logs the ACTION, DEVICENAME, DEVPATH and PRODUCT so you have some visibility as to what hotplug is seeing.
The sequence number is important so your script is executed in the correct sequence.
-
@crispyoz Is the logger I have not a good enough logging?
How can I determine when the script shoudl be ran?
-
@baby-Onioneer I would add an additional line of logging like this:
logger -t MYUSBHOTPLUG "ACTION: $ACTION , DEVICENAME: $DEVICENAME , blah blah"
if [ "$ACTION" = "add" ]; then
logger -t MYUSBHOTPLUG "iPhone detected, PRODUCT=$PRODUCT, bringing up eth1"
sleep 2
ifconfig eth1 up
udhcpc -i eth1
fiSo you can see the sequence of what is going on with the script, not just inside your "if" statement. I'm not sure if it's add or bind that you want to respond to.