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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
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: 4986
Threads: 28
Joined: Aug 2017
Reputation:
228
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: 8125
Threads: 43
Joined: Jun 2015
Reputation:
471
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:
1 2 3 4
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
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 )
else
log (
'Disconected' )
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:
1 2 3 4
Ping IP 08.08.2019 11 :
04 :
40
*
string :
Connected
Ping IP 08.08.2019 11 :
04 :
40
*
function :
0xb6b9d158
Posts: 4986
Threads: 28
Joined: Aug 2017
Reputation:
228
Correct your line 6 to this
Code:
1
ping =
ping (
'192.168.100.222' )
------------------------------
Ctrl+F5
Posts: 57
Threads: 28
Joined: May 2018
Reputation:
1
Thank You Daniel, it works perfectly
Posts: 368
Threads: 83
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
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
grp.checkwrite (
'11/0/1' ,
true )
else
log (
'Disconected' )
grp.checkwrite (
'11/0/1' ,
false )
end
ping =
ping (
'192.168.45.2' )
if ping then
grp.checkwrite (
'11/0/2' ,
true )
grp.checkwrite (
'11/0/2' ,
false )
end
ping =
ping (
'192.168.45.205' )
if ping then
grp.checkwrite (
'11/0/3' ,
true )
else
grp.checkwrite (
'11/0/3' ,
false )
end
ping =
ping (
'192.168.45.235' )
if ping then
grp.checkwrite (
'11/0/4' ,
true )
else
grp.checkwrite (
'11/0/4' ,
false )
end
ping =
ping (
'192.168.45.210' )
if ping then
grp.checkwrite (
'11/0/5' ,
true )
else
grp.checkwrite (
'11/0/5' ,
false )
end
Alex
Posts: 8125
Threads: 43
Joined: Jun 2015
Reputation:
471
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:
1 2 3 4 5 6
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: 368
Threads: 83
Joined: Jun 2017
Reputation:
8
OK,
Big thanks for correcting our mistakes ans possibility to learn right ways of LUA programming!
Alex
Posts: 35
Threads: 6
Joined: Dec 2023
Reputation:
0
Hi All!
The solution doesn't work on LSS100200.
My script is follow:
Code:
1 2 3 4 5 6 7 8 9
function ping (
ip )
local res =
os.execute (
'ping -c 2 -W 5 ' ..
ip )
return res ==
0
end
res =
ping (
'10.25.90.12' )
grp.checkupdate (
'5.SHAVIP.2_MEGateway_Status' ,
res )
In error log I see this:
Posts: 8125
Threads: 43
Joined: Jun 2015
Reputation:
471
Disable "Block unsafe functions in scripts" in Utilities > General configuration.
Posts: 336
Threads: 75
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:
1
os.execute (
'telnet ' ..
ip ..
port )
Best Regards,
Posts: 8125
Threads: 43
Joined: Jun 2015
Reputation:
471
Instead of executing ping/telnet you can try connecting to a known open TCP port on a device if it has one.
Code:
1 2 3 4 5 6 7 8 9 10
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: 336
Threads: 75
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:
1 2 3 4 5 6 7 8 9 10
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: 8125
Threads: 43
Joined: Jun 2015
Reputation:
471
It should work with any service that uses TCP - HTTP, FTP, Telnet, SSH etc.