Posts: 57
Threads: 28
Joined: May 2018
Reputation:
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.
Posts: 4896
Threads: 28
Joined: Aug 2017
Reputation:
222
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
Posts: 8014
Threads: 43
Joined: Jun 2015
Reputation:
463
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
Posts: 57
Threads: 28
Joined: May 2018
Reputation:
1
08.08.2019, 08:10
(This post was last modified: 08.08.2019, 08:10 by MantasJ.)
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
Posts: 4896
Threads: 28
Joined: Aug 2017
Reputation:
222
Correct your line 6 to this
Code: ping=ping('192.168.100.222')
------------------------------
Ctrl+F5
Posts: 57
Threads: 28
Joined: May 2018
Reputation:
1
Thank You Daniel, it works perfectly
Posts: 363
Threads: 82
Joined: Jun 2017
Reputation:
8
22.04.2020, 21:23
(This post was last modified: 22.04.2020, 21:24 by AlexLV.)
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
Posts: 8014
Threads: 43
Joined: Jun 2015
Reputation:
463
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)
Posts: 363
Threads: 82
Joined: Jun 2017
Reputation:
8
OK,
Big thanks for correcting our mistakes ans possibility to learn right ways of LUA programming!
Alex
Posts: 34
Threads: 6
Joined: Dec 2023
Reputation:
0
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
Posts: 8014
Threads: 43
Joined: Jun 2015
Reputation:
463
Disable "Block unsafe functions in scripts" in Utilities > General configuration.
Posts: 335
Threads: 74
Joined: Jan 2021
Reputation:
0
30.01.2025, 09:44
(This post was last modified: 30.01.2025, 09:47 by khalil.)
Dear admin,
is there any concerns using telnet in this way, while I got response as ping 0 == succeed
Code: os.execute('telnet ' .. ip .. port)
Best Regards,
Posts: 8014
Threads: 43
Joined: Jun 2015
Reputation:
463
Instead of executing ping/telnet you can try connecting to a known open TCP port on a device if it has one.
Code: function pingtcp(ip, port)
local sock = require('socket').tcp()
sock:settimeout(1)
local res, err = sock:connect(ip, port)
sock:close()
return res, err
end
res, err = pingtcp('192.168.1.1', 80)
log(res, err)
Posts: 335
Threads: 74
Joined: Jan 2021
Reputation:
0
(30.01.2025, 11:35)admin Wrote: Instead of executing ping/telnet you can try connecting to a known open TCP port on a device if it has one.
Code: function pingtcp(ip, port)
local sock = require('socket').tcp()
sock:settimeout(1)
local res, err = sock:connect(ip, port)
sock:close()
return res, err
end
res, err = pingtcp('192.168.1.1', 80)
log(res, err)
thank you
does this work with ftp port 21?
Best Regards,
Posts: 8014
Threads: 43
Joined: Jun 2015
Reputation:
463
It should work with any service that uses TCP - HTTP, FTP, Telnet, SSH etc.
|