-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
69 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
#!/usr/bin/env bash | ||
|
||
SUDO=${SUDO:-sudo} | ||
|
||
USB_CDC_ECM_DIR="$(dirname "$(readlink -f "$0")")" | ||
|
||
INTERFACE_CHECK_COUNTER=5 # 5 attempts to find usb interface | ||
|
||
find_interface() { | ||
INTERFACE=$(ls -A /sys/bus/usb/drivers/cdc_ether/*/net/ 2>/dev/null) | ||
INTERFACE_CHECK=$(echo -n "${INTERFACE}" | head -c1 | wc -c) | ||
if [ "${INTERFACE_CHECK}" -eq 0 ] && [ ${INTERFACE_CHECK_COUNTER} != 0 ]; then | ||
# We want to have multiple opportunities to find the USB interface | ||
# as sometimes it can take a few seconds for it to enumerate after | ||
# the device has been flashed. | ||
sleep 1 | ||
((INTERFACE_CHECK_COUNTER=INTERFACE_CHECK_COUNTER-1)) | ||
find_interface | ||
fi | ||
INTERFACE=${INTERFACE%/} | ||
} | ||
|
||
echo "Waiting for network interface." | ||
find_interface | ||
|
||
if [ "${INTERFACE_CHECK}" -eq 0 ]; then | ||
echo "Unable to find network interface" | ||
exit 1 | ||
else | ||
echo "Found interface: ${INTERFACE}" | ||
fi | ||
|
||
setup_interface() { | ||
${SUDO} sysctl -w net.ipv6.conf."${INTERFACE}".forwarding=1 | ||
${SUDO} sysctl -w net.ipv6.conf."${INTERFACE}".accept_ra=0 | ||
${SUDO} ip link set "${INTERFACE}" up | ||
${SUDO} ip a a 2001:db8::1/64 dev "${INTERFACE}" | ||
${SUDO} ip route add "${PREFIX}" via 2001:db8::1 dev "${INTERFACE}" | ||
} | ||
|
||
cleanup_interface() { | ||
${SUDO} ip a d 2001:db8::1/64 dev "${INTERFACE}" | ||
${SUDO} ip route del "${PREFIX}" via 2001:db8::1 dev "${INTERFACE}" | ||
} | ||
|
||
cleanup() { | ||
echo "Cleaning up..." | ||
cleanup_interface | ||
trap "" INT QUIT TERM EXIT | ||
} | ||
|
||
|
||
PREFIX=$1 | ||
|
||
if [ -n "$2" ]; then | ||
PORT=$2 | ||
fi | ||
|
||
trap "cleanup" INT QUIT TERM EXIT | ||
|
||
setup_interface | ||
|
||
if [ -z "${PORT}" ]; then | ||
echo "Network enabled over CDC-ECM" | ||
echo "Press Return to stop" | ||
read -r | ||
else | ||
"${USB_CDC_ECM_DIR}/../pyterm/pyterm" -p "${PORT}" | ||
fi |