Skip to content

Instantly share code, notes, and snippets.

@suominen
Created March 17, 2021 05:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save suominen/127f22f63927924941ad2fb8356a77c8 to your computer and use it in GitHub Desktop.
Save suominen/127f22f63927924941ad2fb8356a77c8 to your computer and use it in GitHub Desktop.
#!/bin/sh
#
# Copyright (c) 2020 Kimmo Suominen
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer
# in the documentation and/or other materials provided with the
# distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
#
# Report on dhcpcd(8) lease dump data
#
# 20201019 Kimmo Suominen
#
set -eu
PATH=/bin:/usr/bin:/sbin:/usr/sbin
export PATH
PROG="${0##*/}"
PDIR="${0%/*}"
dhcpcd=dhcpcd
case "${PDIR}" in
/*)
if [ -x "${PDIR}/dhcpcd" ]
then
dhcpcd="${PDIR}/dhcpcd"
fi
;;
esac
die()
{
echo "${PROG}: ${*}" 1>&2
exit 1
}
get_dhcpcd_dump()
{
"${dhcpcd}" -U \
| sed -n '
/^reason=ROUTERADVERT$/,/^$/ {
p
}
'
}
get_interface_nd_data()
{
local interface nd data
interface="${1}"
nd="${2}"
data="${3}"
echo "${data}" \
| sed -n '
/^interface='"${interface}"'$/,/^$/ {
/^nd'"${nd}"'_/ {
s//_/
p
}
}
'
}
get_interfaces()
{
echo "${1}" \
| sed -n '
/^interface=/ {
s///
p
}
'
}
#
# Mimic "ndp -r" output from "dhcpcd -U" data
#
ndp_r()
{
local data
data="${1}"
for int in $(get_interfaces "${data}")
do
i=1
while :
do
_from=
eval $(get_interface_nd_data "${int}" "${i}" "${data}")
if [ "${_from:+exists}" != "exists" ]
then
break
fi
echo -n "${_from}"
case "${_from}" in
fe80:*)
echo -n "%${int}"
;;
esac
echo -n " if=${int}"
flags=
pref=medium
for flag in $(echo "${_flags}" | grep -i .)
do
case "${flag}" in
h)
pref=high
;;
*)
flags="${flags}${flag}"
;;
esac
done
echo -n ", flags=${flags}, pref=${pref}"
exp=$((${_lifetime} + ${_acquired} - ${_now}))
exp_minutes=$((${exp} / 60))
exp_seconds=$((${exp} % 60))
echo -n ", expire="
[ ${exp_minutes} -gt 0 ] && echo -n "${exp_minutes}m"
echo -n "${exp_seconds}s"
echo
i=$((${i} + 1))
done
done
}
usage()
{
cat <<EOF
Usage: ${PROG} [-r] [-f file]
${PROG} -h
Options:
-f Read dhcpcd(8) lease dump data from a file
-h Show this usage message
-r Show default router list
EOF
}
# It is the only mode for now, so make it default
mode=ndp_r
source=
while getopts f:hr opt
do
case "${opt}" in
f) source="${OPTARG}";;
h) usage; exit 0;;
r) mode=ndp_r;;
n) noop=true;;
p) mode=prune;;
*) usage 1>&2; exit 1;;
esac
done
shift $((${OPTIND} - 1))
case "${source}" in
'')
data="$(get_dhcpcd_dump)"
;;
*)
if [ -r "${source}" ]
then
data="$(cat "${source}")"
else
die "Cannot read '${source}'"
fi
;;
esac
case "${mode}" in
ndp_r)
ndp_r "${data}"
;;
*)
die "Unknown mode '${mode}'"
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment