Skip to content

Instantly share code, notes, and snippets.

@the-real-t30d0r
Last active September 22, 2021 18:41
Show Gist options
  • Save the-real-t30d0r/4e7ee7a56a3d969e1a6789894679ec19 to your computer and use it in GitHub Desktop.
Save the-real-t30d0r/4e7ee7a56a3d969e1a6789894679ec19 to your computer and use it in GitHub Desktop.
USG 3-P UniFi Script for pulling the IPv6-Address from DynDNS

USG 3-P UniFi Script for pulling the IPv6-Address from DynDNS and insert it into a IPv6-Group that is created on the WebUI of the UniFi Controller

Thanks to:

https://community.ui.com/questions/Dynamically-updating-an-address-group-with-a-script/fc25b83e-7b9e-4878-8b92-166185c943e2#comment/bec2da2d-b63a-4ba3-bb3d-0a6d5de4d7ac

The script from above was only for IPv4 so I modified it a bit.

How To:

  • Create a new IPv6-Address Group on your UniFi Controller via WebUI (you can insert a random IPv6-Address)

  • SSH into your USG3-P

  • sudo su root

  • show configuration

  • Now look for the group that you created previously on the WebUI and copy the ID .

Example:

ipv6-address-group (614a0a974e56af19bf2c4ec8) <- this is the group ID

After this quit the output and:

  • vi ipv6-dyndns.sh
  • Copy the code from below

#/bin/bash
run=/opt/vyatta/bin/vyatta-op-cmd-wrapper
white_list=$($run show firewall group AlphaPiDDNS_IPv6 | grep -A5 Members |grep -v Members | awk '{ print $1 }' )
resolved_ips=$(getent hosts example.dyndv6.net | awk '{ print $1 }')
resolved_ips="$resolved_ips"
iparray=( `echo "$resolved_ips"`)
if [ "$white_list" = "$resolved_ips" ]; then
:
else
#if addresses have changed, remove address-group "614a0a974e56af19bf2c4ec8" and recreate it with the new addresses
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
/opt/vyatta/sbin/vyatta-cfg-cmd-wrapper begin
/opt/vyatta/sbin/vyatta-cfg-cmd-wrapper delete firewall group ipv6-address-group 614a0a974e56af19bf2c4ec8
/opt/vyatta/sbin/vyatta-cfg-cmd-wrapper set firewall group ipv6-address-group 614a0a974e56af19bf2c4ec8 ipv6-address "${iparray[0]}"
/opt/vyatta/sbin/vyatta-cfg-cmd-wrapper commit
/opt/vyatta/sbin/vyatta-cfg-cmd-wrapper save
/opt/vyatta/sbin/vyatta-cfg-cmd-wrapper end
fi

Replace example.dyndv6.net with your dyndns.

Replace at:

/opt/vyatta/sbin/vyatta-cfg-cmd-wrapper delete firewall group ipv6-address-group 614a0a974e56af19bf2c4ec8

/opt/vyatta/sbin/vyatta-cfg-cmd-wrapper set firewall group ipv6-address-group 614a0a974e56af19bf2c4ec8 ipv6-address "

with your group ID

And that's it.

don't forget chmod+x ipv6-dyndns.sh

If you want you can create an cronjob with cronjob -e and run it for example every minute.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment