Check available connection with tsNet?

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Zax
Posts: 519
Joined: Mon May 28, 2007 10:12 am
Contact:

Re: Check available connection with tsNet?

Post by Zax » Tue Mar 31, 2020 2:56 pm

bogs wrote:
Tue Mar 31, 2020 2:45 pm
Please copy/paste field 2's result, or give me an example address, cause without anything to go on, I can't possibly improve the solution for you :P
Well, it was in fact a friend's website in development, so maybe he has made some kind of special Apache settings. Sorry.
I will post URL here if I find other websites that returns strange values.

Anyhow, thanks for your help :)

bogs
Posts: 5480
Joined: Sat Feb 25, 2017 10:45 pm

Re: Check available connection with tsNet?

Post by bogs » Tue Mar 31, 2020 3:16 pm

If that is the case, that would be a perfect example to base it on, I don't care about the ip. part of field 2, just give me whatever it says below that in the return information. Or whenver you find another site that doesn't return ping, or comes up with something other than what we're testing for.

In other words, what we need to test with is contained in these parts
PING .... 56(84) bytes of data.
64 bytes from ......: icmp_seq=1 ttl=53 time=21.5 ms

---.........---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 21.522/21.522/21.522/0.000 ms
Image

mrcoollion
Posts: 738
Joined: Thu Sep 11, 2014 1:49 pm

Re: Check available connection with tsNet?

Post by mrcoollion » Tue Mar 31, 2020 6:15 pm

This also works On my Windows 10 machine and is much faster than my previous script.

Code: Select all

on mouseUp
   if CheckInternetConnection () is true
   then
      answer "ok"
   else
      answer "No connection"
   end if
end mouseUp

function CheckInternetConnection
   put "ping -n 1 google.com" into pCommandLines
   set the hideConsoleWindows to true
   put shell(pCommandLines) into tAnswer
   if tAnswer contains "could not find" then put False into tAnswer else put True into tAnswer
   return tAnswer
end CheckInternetConnection

bogs
Posts: 5480
Joined: Sat Feb 25, 2017 10:45 pm

Re: Check available connection with tsNet?

Post by bogs » Tue Mar 31, 2020 9:17 pm

Yah, I suspect it should work on any OS, provided the ping command is formatted correctly for the OS in question (does mobile have ping? No clue :D ), which means at worst if your multi-platform, your talkingi about if / then or select / case structure to determine how to format the ping.
Image

mrcoollion
Posts: 738
Joined: Thu Sep 11, 2014 1:49 pm

Re: Check available connection with tsNet?

Post by mrcoollion » Wed Apr 01, 2020 7:04 am

Ping for Mac
https://ss64.com/osx/ping.html

Ping for Linux
https://vitux.com/linux-ping-command/
https://linux.die.net/man/8/ping

Ping for Windows
https://docs.microsoft.com/en-us/window ... mands/ping

You still need to test the response on each platform with and without an internet connection to realize the correct true and false answer.

Zax
Posts: 519
Joined: Mon May 28, 2007 10:12 am
Contact:

Re: Check available connection with tsNet?

Post by Zax » Wed Apr 01, 2020 8:16 am

bogs wrote:
Tue Mar 31, 2020 12:47 pm
No, but all websites that are reachable return 'something', and you can test for that
I found this:
The responses to pings have been disabled by OVH on shared hosting.
It can be tested with "durandexpertises.com"

Code: Select all

on mouseUp
   get "durandexpertises.com"
   put shell("ping " & it &" -c1") into fld "response"
end mouseUp
PING durandexpertises.com (136.0.165.69): 56 data bytes

--- durandexpertises.com ping statistics ---
1 packets transmitted, 0 packets received, 100.0% packet loss

bogs
Posts: 5480
Joined: Sat Feb 25, 2017 10:45 pm

Re: Check available connection with tsNet?

Post by bogs » Wed Apr 01, 2020 12:20 pm

Short answer: there is a lot more wrong with that site than just dropping ping packets, so I'm not sure that is a valid test :P

Longer answer:
1.) I noticed the question in this thread is starting to take a turn. Originally, the question / goal as I read it was merely ~
I would like to test if an internet connection is available from a stack.
Ping satisfies that question fairly quickly, *if* you just want to know if your connected, and while pinging google certainly works, there are other options (such as a dns server, etc). In fact, IF I really want to know if i'm connected using ping, I'd probably pick 3 sites known to respond to ping for the 1 ping test, as the likelyhood that 3 sites would be down simultaneously is not very high.

2.) In what I am going to call part 2 of this thread, the question is shifting to 'how do you tell if a particular website is online if it drops ping packets'. Your example site, "durandexpertises.com" brings up some interesting challenges, but I am not sure they are due to ping, per se.
Other testing tools give at best mixed results. Normally when I am looking for blockage on the network, I resort to one of the various forms of traceroute (tracerrt on Win, traceroute or mtr on Lin and probably on OSX).
Traceroute literally traces every hop it takes you to get somewhere, but of course, it takes longer than ping normally. For your example site, the last hop letting it run unrestricted wasn't very informative, but I do know it connected at 13 hops -

Code: Select all

MTR.0.92,1585739064,OK,durandexpertises.com,13,???,100.00,10,10,0.00,0.00,0.00,0.00,0.00
Telnet is another good way to tell if something is up, but your example apparently isn't setup to answer on the default port(s) tested by telnet. Go figure.

Code: Select all

~$ telnet durandexpertises.com
Trying 136.0.165.69...  <-- resolved the host name to an IP address, anyway...
telnet: Unable to connect to remote host: Connection timed out
3.) Things I sometimes use to test off the cli, which I did in this case, using an actual browser to check the url, and using " Is It Down Right Now".
First, the web browser -
Firefox wrote: Warning: Potential Security Risk Ahead

Firefox detected an issue and did not continue to www.durandexpertises.com. The website is either misconfigured or your computer clock is set to the wrong time.

It’s likely the website’s certificate is expired, which prevents Firefox from connecting securely. If you visit this site, attackers could try to steal information like your passwords, emails, or credit card details.

What can you do about it?

Your computer clock is set to 4/1/2020. Make sure your computer is set to the correct date, time, and time zone in your system settings, and then refresh www.durandexpertises.com.

-->> If your clock is already set to the right time, the website is likely misconfigured, and there is nothing you can do to resolve the issue. You can notify the website’s administrator about the problem.
Since my computer is set (as probably most are) to an ntp server, I'm relatively certain my clock and date are right. This brought me to my last test, the "is it down" site.
Website Name:Durandexpertises
durandexpertises.comURL Checked:
no responseResponse Time:
unknownLast Down:
DOWN
Durandexpertises.com is DOWN
It is not just you. The server is not responding...
Considering the results I received outside of 'ping', again, I'm not really sure this site is a good example site even for the 2nd part of the thread. At best, you can only tell that the dns resolves, and that it can be reached in 13 hops, but you won't get much more than that from standard testing methods.
Image

Zax
Posts: 519
Joined: Mon May 28, 2007 10:12 am
Contact:

Re: Check available connection with tsNet?

Post by Zax » Wed Apr 01, 2020 1:10 pm

bogs wrote:
Wed Apr 01, 2020 12:20 pm
Considering the results I received outside of 'ping', again, I'm not really sure this site is a good example site even for the 2nd part of the thread.
Despite the fact that this website seems to have trouble, as far as I know, all shared websites hosted at OVH don't respond to pings.

But you're right, we are far from my initial request!
Anyhow, the 'ping' way was a very interesting solution to test if my LC standalone has a valid internet connection.

My previous script:

Code: Select all

function getPingDelay
   // return 0 if not available connection
   put "ping google.com -c1  | sed '$!d;s|.*/\([0-9.]*\)/.*|\1|'" into cmd
   get shell(cmd)
   if (it is not a number) then
      return 0 // instead of an error string
   else return it
end getPingDelay
is not working under Windows, so I finaly use this. It works under MacOS and windows, but I'm not fully satisfied.

The problem is that the 'ping' returned values under Windows are localized!

Code: Select all

function net_isConnected // return boolean
   put "google.com" into tUrl
   
   if (the platform <> "MacOS") then
      put "ping -n 1 " & tUrl into cmd
      set the hideConsoleWindows to true
   else put "ping " & tUrl & " -c1" into cmd
   get shell(cmd) // localized windows string!!!
   
   return ("TTL=" is in it) // unsafe test
end net_isConnected

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7392
Joined: Sat Apr 08, 2006 8:31 pm
Contact:

Re: Check available connection with tsNet?

Post by jacque » Wed Apr 01, 2020 5:32 pm

bogs wrote:
Tue Mar 31, 2020 9:17 pm
Yah, I suspect it should work on any OS, provided the ping command is formatted correctly for the OS in question (does mobile have ping?
Shell is not supported on mobile, it's a security risk.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

bogs
Posts: 5480
Joined: Sat Feb 25, 2017 10:45 pm

Re: Check available connection with tsNet?

Post by bogs » Wed Apr 01, 2020 5:56 pm

I didn't really think it was available, but figured I'd ask for completeness :)
Image

Zax
Posts: 519
Joined: Mon May 28, 2007 10:12 am
Contact:

Re: Check available connection with tsNet?

Post by Zax » Fri Apr 03, 2020 8:35 am

Well, it appears my previous script doesn't work on Windows 10.
This one was successfully tested on MacOS, Windows 7 and Windows 10:

Code: Select all

function isConnected // return boolean
   put "google.com" into tUrl
   
   if (the platform <> "MacOS") then
      put "ping -n 1 " & tUrl & " > NUL && echo 1 || echo 0" into cmd
      set the hideConsoleWindows to true
   else put "ping -c 1 " & tUrl & " &> /dev/null && echo 1 || echo 0" into cmd
   get shell(cmd)
   
   return (it = 1)
end isConnected

mrcoollion
Posts: 738
Joined: Thu Sep 11, 2014 1:49 pm

Re: Check available connection with tsNet?

Post by mrcoollion » Fri Apr 03, 2020 9:22 am

Glad you have a working solution... :D

bogs
Posts: 5480
Joined: Sat Feb 25, 2017 10:45 pm

Re: Check available connection with tsNet?

Post by bogs » Fri Apr 03, 2020 10:15 am

Why write it as a test only for Mac or Win? Essentially, the ping command is the same syntax on either Mac or Lin, so you *could* write it as ~

Code: Select all

function isConnected // return boolean
   put "google.com" into tUrl
   
   if (the platform is not "Win32") then -- we're not on a Windows machine...
      put "ping -c 1 " & tUrl & " &> /dev/null && echo 1 || echo 0" into cmd
      set the hideConsoleWindows to true
   else put "ping -n 1 " & tUrl & " > NUL && echo 1 || echo 0" into cmd
   get shell(cmd)

   return (it = 1)
end isConnected
Curious no terminating 'end if' on that if then, If you really want it shorter by even a few words, you could probably write it like this ...

Code: Select all

# Updated to change the return statement....
function isConnected // return boolean
   put "google.com" into tUrl; put the platform into tPlat
   
   if tPlat is not "Win32" then put "ping -c 1 " & tUrl & " &> /dev/null && echo 1 || echo 0" into cmd
   if tPlat is "Win32" then put "ping -n 1 " & tUrl & " > NUL && echo 1 || echo 0" into cmd
      
   set the hideConsoleWindows to true

   return matchText(shell(cmd),"1 received")
end isConnected
I actually took the time to run the above on 'nix in the order of
yours...
mine 1...
mine 2...
Here is the return of 'it' output

Code: Select all

connect: Invalid argument
0


1
PING google.com (172.217.12.206) 56(84) bytes of data.
64 bytes from lga25s63-in-f14.1e100.net (172.217.12.206): icmp_seq=1 ttl=51 time=22.6 ms

--- google.com ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 22.632/22.632/22.632/0.000 ms


1
PING google.com (172.217.12.206) 56(84) bytes of data.
64 bytes from lga25s63-in-f14.1e100.net (172.217.12.206): icmp_seq=1 ttl=51 time=21.6 ms

--- google.com ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 21.646/21.646/21.646/0.000 ms
**NOTE - you may need to check your return statement, I got 'false' for all 3 with it in there uncommented, since it isn't returning just '1'.
Simply changing it to
return matchText(shell(cmd),"1 received")
works like a charm, though :)

* Edit - for that matter, changing the statement from
if tPlat is not "Win32" then put "ping -c 1 " & tUrl & " &> /dev/null && echo 1 || echo 0" into cmd
to
if tPlat is not "Win32" then put "ping -c 1 " & tUrl into cmd
also worked here, unless you need it redirected for some reason.

I'm sure there are even shorter ways to write it, but my coffee level hasn't reached the point of lucid clarity yet Image
Image

Zax
Posts: 519
Joined: Mon May 28, 2007 10:12 am
Contact:

Re: Check available connection with tsNet?

Post by Zax » Fri Apr 03, 2020 12:12 pm

Thanks for these clarifications, bogs.
bogs wrote:
Fri Apr 03, 2020 10:15 am
Curious no terminating 'end if' on that if then
Hum, yes, you're right. It is a (bad) habit that I took from the time of Hypercard. :wink:

bogs
Posts: 5480
Joined: Sat Feb 25, 2017 10:45 pm

Re: Check available connection with tsNet?

Post by bogs » Fri Apr 03, 2020 12:26 pm

I don't know that I'd say it was 'bad' or not, for instance, if I use an if / then one liner, as above, you may notice I don't close it either :P

It also wasn't a 'Hypercard' thing solely, Delphi doesn't require it either. It is, as I said, merely a curiousity thing on my part, I'm always fascinated by the differences in writing styles :)
Image

Post Reply