Skip to content

Instantly share code, notes, and snippets.

@icpz
Created August 28, 2021 04:41
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 icpz/82dc49a0c815ec9b7ef7237d1a8cc177 to your computer and use it in GitHub Desktop.
Save icpz/82dc49a0c815ec9b7ef7237d1a8cc177 to your computer and use it in GitHub Desktop.
Get static ipv6 info from /etc/config/dhcp
#!/bin/sh
. /lib/functions.sh
. /lib/functions/network.sh
log() {
logger -s -t "ddns[nas]" "$@"
}
join_ipv6() {
local _prefix="$1"
local _suffix="$2"
owipcalc "$_prefix" add "$_suffix"
}
# 1. interface
get_ipv6_prefix() {
local _interface="$1"
local _ip6prefix="$(ifstatus $_interface | jq -Mr '."ipv6-prefix-assignment"[0].address')"
if [ "$_ip6prefix" = "null" ]; then
log "failed to get ipv6 prefix on $_interface"
exit 0
fi
echo "$_ip6prefix"
}
dhcp_host_cb() {
local _cfg="$1"
local _hostname="$2"
local _val="$3"
local _name _hostid
config_get _name "$_cfg" name
config_get _hostid "$_cfg" hostid
if [ "$_name" = "$_hostname" ]; then
export ${NO_EXPORT:+-n} "${_val}=${_hostid}"
fi
}
# 1. hostname
match_ipv6_suffix() {
local _hostname="$1"
config_load dhcp
local _suffix
config_foreach dhcp_host_cb host $_hostname _suffix
if [ -z "$_suffix" ]; then
log "failed to match ipv6 suffix for $_hostname"
exit 0
fi
echo "$_suffix"
}
# 1. interface
# 2. hostname
get_ipv6_address() {
local _if="$1"
local _hn="$2"
join_ipv6 "$(get_ipv6_prefix $_if)" "::$(match_ipv6_suffix $_hn)"
}
_HOSTNAME="$1"
_IFACE="${2:-lan}"
get_ipv6_address "$_IFACE" "$_HOSTNAME"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment