Made an api for displaying public IPv6 and IPv4

Finally made an API which displays the public IPv6 prefix and public IPv4 address using http://192.168.1.1/cgi-bin/public-ip.sh locally instead of querying a remote server.

upload the public-ip.sh file in /www/cgi-bin/ subdirectory with 755 permission.

#!/bin/sh
printf "Content-Type: text/plain\n\n"

ipv4=`ubus call network.interface.wan status | grep \"address\" | grep -oE '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}';`

ipv6=`ubus call network.interface.wan6 status | grep \"ipv6-prefix\" -A 10 | grep \"address\" | grep -o '"[^"]\+"' | sed 's/"//g' | grep '^2' | head -n 1;`

echo "{"
echo "  \"ipv4\": \"$ipv4\","
echo "  \"ipv6\": \"$ipv6\""
echo "}"

exit 0

Or modify the script to just show public IP without the json format.

Let me know if any improvements can be made.

  • How does this differ from viewing them on the Interfaces page - what issue does this solve?
  • What if the interfaces aren't named wan or wan6?
1 Like

To check for IP changes from scripts in servers without logging into the router.

Just rename the interfaces in the script to correct names.

do you know the luci-app-commands?
there you can also define custom commands

You still have to log in to any linux based systems to run anything so the permissions isn’t the thing here.
And once logged in this script becomes extra work in order to get to final destination I would say since the ip command have this data to begin with.
The simplest way without finetuning is ip address and they all show up.

1 Like

dnsmasq can provide dynamic ip of an interface via DNS and does not even need a shell script running as root.