Skip to content

Commit

Permalink
Add ipv6 support to openwisp-get-address
Browse files Browse the repository at this point in the history
  • Loading branch information
Bartosz-lab committed Oct 19, 2024
1 parent 0967c32 commit 99db061
Showing 1 changed file with 22 additions and 3 deletions.
25 changes: 22 additions & 3 deletions openwisp-config/files/sbin/openwisp-get-address.lua
100644 → 100755
Original file line number Diff line number Diff line change
@@ -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 <ifname>
-- * openwisp-get-address <network-name>
-- * openwisp-get-address <ifname> [--prefer-ipv6]
-- * openwisp-get-address <network-name> [--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)
Expand Down

0 comments on commit 99db061

Please sign in to comment.