Need to verify/check ipv6 address using ping in lua script

90 views Asked by At

i am not aware of lua script but i need some help. Basically current lua script will receive structure. in those structure has address parameter where will get two index parameters(ipv6 & ipv4) addresses.

lua script need to implement below case

  1. ping ipv6 address and result will get store in local variable. if local variable gets (ping success) will connect/call uv.tcp_connect for passed ipv6 address. otherwise i will check the same for ipv4 address and try to connect/call uv.tcp_connect.
2

There are 2 answers

1
koyaanisqatsi On

To store output of system commands i suggest io.popen().
An example for conditional ping that tries first IPv6 and if fail IPv4...

> code.cmd
-- cmd(shell)
return function(shell)
return io.popen(shell, 'r'):read('a+')
end
> results={}
> results.ping=load(code.cmd)()('ping -q -c1 -6 localhost 2>&1 >/dev/null && printf "IPv6: true" || (ping -q -c1 localhost 2>&1 >/dev/null && printf "IPv4 true" || printf "false")')
> print(results.ping)
IPv6: true

...typed in a Lua console.

EDIT enter image description here Online Lua Environments dont support above code!

1
sundar On

I am using online lua editor there its returning nil.

local results = load('ping -q -c1 -6 localhost 2>&1 >/dev/null && printf "IPv6: true" || (ping -q -c1 www.google.com 2>&1 >/dev/null && printf "IPv4 true" || printf "false")') print(results)

output is:nil

and

if i am using in lua online editor ..

local handler = io.popen("ping -c 3 -i 0.5 www.google.com")-- wrong here. local response = handler:read("*a") print(response)

output error : lua: main.lua:3: expected near '"ping -c 3 -i 0.5 www.google.com"'

kindly suggest me , am i missing something above.