From 99db061cdb2acb9adbe684449b5e752ff5c36948 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartosz=20Cie=C5=9Blik?= Date: Sat, 19 Oct 2024 20:49:06 +0200 Subject: [PATCH] Add ipv6 support to openwisp-get-address --- .../files/sbin/openwisp-get-address.lua | 25 ++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) mode change 100644 => 100755 openwisp-config/files/sbin/openwisp-get-address.lua diff --git a/openwisp-config/files/sbin/openwisp-get-address.lua b/openwisp-config/files/sbin/openwisp-get-address.lua old mode 100644 new mode 100755 index ce66eee7..8789bcc4 --- a/openwisp-config/files/sbin/openwisp-get-address.lua +++ b/openwisp-config/files/sbin/openwisp-get-address.lua @@ -1,13 +1,32 @@ #!/usr/bin/env lua -- gets the first useful address of the specified interface +-- fistly tries to get an IPv4 address, if not found, tries to get an IPv6 address +-- with the --prefer-ipv6 option, it will try to get an IPv6 address first -- usage: --- * openwisp-get-address --- * openwisp-get-address +-- * openwisp-get-address [--prefer-ipv6] +-- * openwisp-get-address [--prefer-ipv6] local os = require('os') local net = require('openwisp.net') local name = arg[1] -local interface = net.get_interface(name) +local prefer_ipv6 = arg[2] == "--prefer-ipv6" + +local ip_version1, ip_version2 + +if prefer_ipv6 then + ip_version1 = 'inet6' + ip_version2 = 'inet' +else + ip_version1 = 'inet' + ip_version2 = 'inet6' +end + + +local interface = net.get_interface(name, ip_version1) +if not interface then + interface = net.get_interface(name, ip_version2) +end + if interface then print(interface.addr)