Created
August 29, 2018 08:35
-
-
Save jplitza/40d071a14d8170434b6be86518cc3c32 to your computer and use it in GitHub Desktop.
Flash a router running OpenWrt via SSH
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
#!/bin/sh | |
set -eu | |
if [ "$#" != 2 ]; then | |
echo "Usage: $(basename "$0") <iface> <file>" | |
exit 1 | |
fi | |
IFACE="$1" | |
FILE="$2" | |
if ! ip link show dev "$IFACE" | grep -q "LOWER_UP"; then | |
echo " + Waiting for Ethernet link..." | |
ip -o monitor dev "$IFACE" | grep -q -m 1 "LOWER_UP" | |
fi | |
echo " + Establishing Network Manager connection..." | |
nmcli connection up Fixed ifname "$IFACE" > /dev/null | |
while sleep 2; do | |
echo " + Determining remote address..." | |
ADDR="$(ping -r -n -L -i 1 -w 60 -c 1 -I "$IFACE" ff02::1 | awk '/^[0-9]+ bytes from / {print $4}')" | |
ADDR="${ADDR%:}" | |
if [ -z "$ADDR" ]; then | |
continue | |
fi | |
echo " + Found it: ${ADDR}" | |
echo " + Testing SSH connection..." | |
if ssh "$ADDR" true 2> /dev/null; then | |
echo " + OK!" | |
break | |
else | |
echo " + Failed!" | |
fi | |
done | |
echo " + Copying image file..." | |
scp -q "$FILE" "[${ADDR}]:/tmp/firmware.bin" | |
echo " + Starting sysupgrade..." | |
set +e | |
ssh "$ADDR" sysupgrade -n /tmp/firmware.bin | |
set -e | |
echo " + Upgrade running. Please disconnect cable!" | |
ip -o monitor dev "$IFACE" | grep -q -m 1 "NO-CARRIER" | |
echo " + Exiting." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment