Created
June 7, 2020 20:40
-
-
Save erpel/65e2aa105f3af59be757893b89abf2e8 to your computer and use it in GitHub Desktop.
erpel_v6_cleanup
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 | |
# inet6 ... prefixlen 64 deprecated autoconf pltime 0 vltime 1984906 | |
# inet6 ... prefixlen 64 autoconf pltime 174916 vltime 2162116 | |
# special code to allow hup | |
COMMAND=ifconfig | |
LOGFILE=/var/log/erpel_cleanup.log | |
WAIT_SECONDS=0 | |
while getopts i:dnw arg | |
do | |
case ${arg} in | |
i) | |
IFACE=${OPTARG} | |
;; | |
d) | |
echo "debug mode" | |
set -x | |
;; | |
n) | |
# dry run mode | |
COMMAND="echo ifconfig" | |
;; | |
w) | |
WAIT_SECONDS=10 | |
;; | |
?) | |
exerr "invalid argument" | |
esac | |
done | |
rpl_logpart() { | |
echo -n "$1" >> $LOGFILE | |
} | |
rpl_log() { | |
echo "$1" >> $LOGFILE | |
} | |
drop_addrs() { | |
while read -r addr; do | |
[ -z $addr ] && echo "z" && continue | |
rpl_logpart "Dropping address $addr: " | |
if $COMMAND $IFACE inet6 $addr -alias; then | |
rpl_log " Success" | |
else | |
rpl_log " Failed" | |
fi | |
done << EOS | |
${1} | |
EOS | |
} | |
print_count() { | |
echo $2 | |
echo "$1" | wc -l | |
} | |
if [ "x" = "x${IFACE}" ]; then | |
echo "Required option -i not set" >&2 && exit 1 | |
fi | |
rpl_log "Invoked at $(date)." | |
sleep ${WAIT_SECONDS} | |
rpl_log "Running cleanup on interface ${IFACE} at $(date)." | |
RAW=$(ifconfig -L "${IFACE}") | |
INET6=$(echo "$RAW" | grep -E ' *inet6') | |
AUTOCONF=$(echo "$INET6" | grep autoconf) | |
NONDEP=$(echo "$AUTOCONF" | grep -v deprecated) | |
DEP_ADDRS=$(echo "$RAW" | grep -E 'inet6.*deprecated' | awk '{print $2}') | |
drop_addrs "$DEP_ADDRS" | |
NONDEP_ADDRS=$(echo "$NONDEP" | grep -vE 'inet6.*deprecated' | grep -E 'inet6.*pltime' | awk '{print $2}') | |
# keep current line | |
NONDEP_ADDRS_OLD=$(echo "$NONDEP_ADDRS" | sed '$d') | |
drop_addrs "$NONDEP_ADDRS_OLD" | |
rpl_log $(ifconfig -L "${IFACE}" | grep -E ' *inet6') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment