Last active
November 23, 2018 04:52
-
-
Save ilyaevseev/0c0e01fdfb7144258debeeff156564d7 to your computer and use it in GitHub Desktop.
Script for storing D-Link websmart configs in Rancid via TFTP.
This file contains hidden or 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 | |
test $# = 1 || { echo "Usage: ${0##*/} my-ipaddr"; exit 1; } | |
MY_IPADDR="$1" | |
RANCID_USER="rancid" | |
RANCID_HOME="$(su - -c pwd "$RANCID_USER")" | |
PIDFILE="/var/run/${0##*/}.pid" | |
DBLIST="$(grep -l ';dlink_websmart;up' "$RANCID_HOME"/*/router.db)" | |
for f in "$DBLIST"; do | |
cd "$(dirname "$f")/configs" | |
in.tftpd -lcps -u "$RANCID_USER" -P "$PIDFILE" "$PWD" | |
awk -v ME="$MY_IPADDR" -F ';' \ | |
'$2 == "dlink_websmart" && $3 == "up" { | |
if (match($4, /with_config_id/)) { | |
dest = sprintf("%s %s.new config_id 1", ME, $1) | |
} else if (match ($4, /with_destpath/)) { | |
dest = sprintf("%s %s.new", ME, $1) | |
} else { | |
dest = sprintf("tftp://%s/%s.new", ME, $1) | |
} | |
printf "clogin -t 20 -c \"upload cfg_toTFTP %s; logout\" %s\n", dest, $1 | |
}' "$f" \ | |
| su - "$RANCID_USER" | |
awk -F ';' '$2 == "dlink_websmart" && $3 == "up" { print $1 }' "$f" \ | |
| while read n; do test -s "$n.new" && mv -f "$n.new" "$n"; done | |
cat "$PIDFILE" | xargs -r kill || : | |
sleep 1 | |
done | |
## END ## |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment