This forum uses cookies
This forum makes use of cookies to store your login information if you are registered, and your last visit if you are not. Cookies are small text documents stored on your computer; the cookies set by this forum can only be used on this website and pose no security risk. Cookies on this forum also track the specific topics you have read and when you last read them. Please confirm that you accept these cookies being set.

Ping IP doesn't work
#1
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.
Reply
#2
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.
------------------------------
Ctrl+F5
Reply
#3
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
Reply
#4
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
Reply
#5
Correct your line 6 to this
Code:
ping=ping('192.168.100.222')
------------------------------
Ctrl+F5
Reply
#6
Thank You Daniel, it works perfectly Smile
Reply
#7
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
Reply
#8
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)
Reply
#9
OK,

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

Alex
Reply
#10
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
Reply
#11
Disable "Block unsafe functions in scripts" in Utilities > General configuration.
Reply


Forum Jump: