Logic Machine Forum
Ping IP doesn't work - Printable Version

+- Logic Machine Forum (https://forum.logicmachine.net)
+-- Forum: LogicMachine eco-system (https://forum.logicmachine.net/forumdisplay.php?fid=1)
+--- Forum: Scripting (https://forum.logicmachine.net/forumdisplay.php?fid=8)
+--- Thread: Ping IP doesn't work (/showthread.php?tid=2189)



Ping IP doesn't work - MantasJ - 08.08.2019

Hello,

I am using this script: https://forum.logicmachine.net/showthread.php?tid=1035 to ping an IP adress of a laptop in the same network as the LM is. It somewhy does not work, I transformed script into this:

Code:
function socketping(ip, port, timeout)
  port = port or 80
  local sock = require('socket').tcp()
  sock:settimeout(timeout or 20)
  local res, err = sock:connect(ip, port)
  log(res)
  log(err)
  sock:close()
 
  return res, err
end


ip = '192.168.100.149'

if socketping(ip) then
  log('CONNECTED')
  grp.write('0/0/9', true)
else
  log('DISCONECTED')
end
But it always logs disconected, no matter what I try. If I ping the IP from cmd, it pings fine. Where could be the problem?

Thank You.


RE: Ping IP doesn't work - Daniel - 08.08.2019

Log what error tells you. You need to check on which port you can access this PC. Most likely firewall blocks it. I tested it with another LM and the script itself works.


RE: Ping IP doesn't work - admin - 08.08.2019

This is not standard ping. It tries to connect to port 80, so unless you have a web server running on laptop it will not work.

Use this function instead:
Code:
function ping(ip)
  local res = os.execute('ping -c 2 -W 5 ' .. ip)
  return res == 0
end



RE: Ping IP doesn't work - MantasJ - 08.08.2019

Thank You guys. I've tried the script, but now it always logs that the IP is connected even though it is on another network. I don't understand, if there is something I'm doing wrong? Here is the script:

Code:
function ping(ip)
  local res = os.execute('ping -c 2 -W 5 ' .. ip)
  return res == 0
end

ping('192.168.100.222')

if ping then
  log('Connected')
  log(ping)
  --grp.checkwrite('0/0/9', true)
else
  log('Disconected')
  --grp.checkwrite('0/0/9', false)
end

If I ping this IP from cmd the request times out and it is disconected, but if I run this script it says it is connected. Maybe I have to somehow decode the data from "res"?

This is what logs gives me:

Code:
Ping IP 08.08.2019 11:04:40
* string: Connected
Ping IP 08.08.2019 11:04:40
* function: 0xb6b9d158



RE: Ping IP doesn't work - Daniel - 08.08.2019

Correct your line 6 to this
Code:
ping=ping('192.168.100.222')



RE: Ping IP doesn't work - MantasJ - 08.08.2019

Thank You Daniel, it works perfectly Smile


RE: Ping IP doesn't work - AlexLV - 22.04.2020

Hi,

I want to use ping function for many IP check in one script, but how correctly to use function many times?
I did this way but error.. Resident script:53: attempt to call global 'ping' (a boolean value)
stack traceback:
Error apear when I want to use function with other IP..

Code:
function ping(ip)
  local res = os.execute('ping -c 2 -W 5 ' .. ip)
  return res == 0
end
ping=ping('192.168.45.1')
if ping then
  --log('Connected')
  --log(ping)
  grp.checkwrite('11/0/1', true)
else
  log('Disconected')
  grp.checkwrite('11/0/1', false)
end
ping=ping('192.168.45.2') --line 53
if ping then
  --log('Connected')
  --log(ping)
  grp.checkwrite('11/0/2', true)
--else
  --log('Disconected')
  grp.checkwrite('11/0/2', false)
end
ping=ping('192.168.45.205')
if ping then
  --log('Connected')
  --log(ping)
  grp.checkwrite('11/0/3', true)
else
  --log('Disconected')
  grp.checkwrite('11/0/3', false)
end
ping=ping('192.168.45.235')
if ping then
  --log('Connected')
  --log(ping)
  grp.checkwrite('11/0/4', true)
else
  --log('Disconected')
  grp.checkwrite('11/0/4', false)
end
ping=ping('192.168.45.210')
if ping then
  --log('Connected')
  --log(ping)
  grp.checkwrite('11/0/5', true)
else
  --log('Disconected')
  grp.checkwrite('11/0/5', false)
end

Alex


RE: Ping IP doesn't work - admin - 23.04.2020

It does not work because you've set the result to variable with the same name as the function. So ping is no longer a function but boolean variable.
Another thing is that if/else is not needed, you can pass the result directly to checkwrite:
Code:
function ping(ip)
  local res = os.execute('ping -c 2 -W 5 ' .. ip)
  return res == 0
end
res = ping('192.168.45.1')
grp.checkwrite('11/0/1', res)



RE: Ping IP doesn't work - AlexLV - 23.04.2020

OK,

Big thanks for correcting our mistakes ans possibility to learn right ways of LUA programming!

Alex


RE: Ping IP doesn't work - djaval - 24.04.2024

Hi All!

The solution doesn't work on LSS100200.

My script is follow:
Code:
function ping(ip)
  local res = os.execute('ping -c 2 -W 5 ' .. ip)
  return res == 0
end

-- res = ping('10.25.90.209')
res = ping('10.25.90.12')

grp.checkupdate('5.SHAVIP.2_MEGateway_Status', res)

In error log I see this:
Code:
os.execute call blocked



RE: Ping IP doesn't work - admin - 24.04.2024

Disable "Block unsafe functions in scripts" in Utilities > General configuration.