-1

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.
Egor Skriptunoff
  • 23,359
  • 2
  • 34
  • 64
sundar
  • 1
  • 1

2 Answers2

0

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!

koyaanisqatsi
  • 2,585
  • 2
  • 8
  • 15
  • 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. – sundar Aug 06 '21 at 12:34
0

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.

sundar
  • 1
  • 1
  • An online Lua environment is sandboxed/limitated and has never the ability of executing critical functions (normaly in the table ```io``` and ```os```) to run system commands. – koyaanisqatsi Aug 06 '21 at 13:59