Welcome to the Tweaking4All community forums!
When participating, please keep the Forum Rules in mind!
Topics for particular software or systems: Start your topic link with the name of the application or system.
For example “MacOS X – Your question“, or “MS Word – Your Tip or Trick“.
Please note that switching to another language when reading a post will not bring you to the same post, in Dutch, as there is no translation for that post!
[Solved] Lazarus - MacOS X/Linux - Do a network Ping from within your appliccations
Delphi, Lazarus, Free Pascal
(@hans)
Famed Member Admin
Joined: 12 years ago
Posts: 2815
Topic starter
April 25, 2016 9:31 AM
You could use Synapse to do a Ping, but so far I have had zero luck in executing any of the example codes - this appear related to required elevated admin rights to actually do a ping that way.
However ... you can do it by using the Terminal/Commandline "ping".
You will need to include the "
process" unit in the
Uses Clause.
Quickly wrapped in a simple function for MacOS X and Linux - This function WILL NOT WORK under Windows:
function TForm1.Ping(Host:string):boolean;
var s : ansistring;
const PingCount = '1'; // number of pings
PingTimeout = '2'; // timout for each ping
begin
{$IFDEF Darwin}
RunCommand('/sbin/ping',['-c '+PingCount,'-t '+PingTimeout,'-q',Host],s);
{$ELSE}
RunCommand('/bin/ping',['-c '+PingCount,'-w '+PingTimeout,'-q',Host],s);
{$ENDIF}
Result := (pos('100.0% packet loss',S)=0) and (pos('100% packet loss',S)=0);
end;
I've tested this under MacOSX and Linux (Ubuntu / Freya OS).
You will see that time out parameter under MacOS X (-t) is slightly different than under Linux (-w), and that the location of ping is different as well.
The parameters and location might be different under different Linux versions as well.
(@hans)
Famed Member Admin
Joined: 12 years ago
Posts: 2815
Topic starter
May 2, 2016 5:21 AM
A version that works for Windows (tested Windows 10) and MacOS X (tested with El Capitan) - downside for Windows is the briefly popping up of a shell window.
function Ping(Host:string):boolean;
var s : ansistring;
const PingCount = '2'; // number of pings
PingTimeout = '2'; // timout for each ping
begin
s:='';
{$IFDEF Darwin}
RunCommand('/sbin/ping',['-c '+PingCount,'-t '+PingTimeout,'-q',Host],s);
{$ENDIF}
{$IFDEF Windows}
RunCommand('ping.exe',['-n ',PingCount,'-w ',PingTimeout,Host],s);
{$ENDIF}
Result := (pos('100.0% packet loss',S)=0) and (pos('100% packet loss',S)=0) and (pos('100% loss',S)=0);
end;
(@hans)
Famed Member Admin
Joined: 12 years ago
Posts: 2815
Topic starter
June 23, 2024 6:59 AM
For those writing apps with the intend to place them in the Apple App Store:
Applications in the App Store are sandboxed, which means that without the proper entitlements, PING will not work.
After wasting an entire day on how to better implement ping (in code for example - note: Synapse's ping host function does not work under macOS), I found that you will need to set these 2 entitlements for ping to work (in the code I presented above):
com.apple.security.network.client
com.apple.security.network.server
One to send "signals" to the network, but the other one is also needed to receive the ping response.
So you entitlements.plist file could look something like this:
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>com.apple.security.app-sandbox</key>
<true/>
<key>com.apple.security.network.server</key>
<true/>
<key>com.apple.security.network.client</key>
<true/>
</dict>
</plist>
Hope that by posting this here I'm saving someone wasting a day as well 😉