Get Terminal Output from Applescript without waiting

Hello

in my application i have to ping several servers and get the online status.

In my repeat loop i send a <do shell script "ping -c 1 -t1 "> to the server.

But then i have to wait for the response this is taking a very long time before i get the result of all servers.

In this forum i found a solution to without waiting by send output to “> /dev/null 2>&1 &” now i can get the pid with the apple script command “set pid to the result as text”

Is there a possibility to get the output from the pid via applescript?

Or is there another way to send the ping commands simultaneously?

Here I adapted little the script written by DJ Bazzie Wazzie:


set aServer to "192.168.1."
set {aFirst, aLast} to {100, 199}

repeat with i from aFirst to aLast
	set nextServer to (aServer & i)
	do shell script "ping -o -t 1 " & nextServer & " &>/dev/null && echo yes || echo no"
	set serverIsOnline to not (result as boolean)
	if serverIsOnline then exit repeat
end repeat

display dialog "The first available IP is : " & nextServer

Or, you can use that:


set serversList to {"192.168.1.101", "215.124.3.118"}

set serversOnlineList to {}

repeat with nextServer in serversList
	do shell script "ping -o -t 1 " & nextServer & " &>/dev/null && echo yes || echo no"
	set serverIsOnline to not (result as boolean)
	if serverIsOnline then
		display notification "Server " & nextServer & " is available"
		set serversOnlineList to serversOnlineList & nextServer
	end if
end repeat

return serversOnlineList

thx for the fast answer.

in your script i had to remove the “not” from line:

set serverIsOnline to not (result as boolean)< to get it work.

But unfortunately this solution did not solve my problem
To repeat through all 100 servers it tooks 92 Seconds in which my program is not responding.

I need a solution where the ping command is operating in background or if possible more than one ping is send simultaneously. Maybe there is another solution in ASOC?

I tried the AsObjC version. It is only 1.5 times faster:


use AppleScript version "2.5"
use scripting additions
use framework "Foundation"
use framework "AppKit"
property |NSURL| : a reference to current application's |NSURL|
property NSURLRequest : a reference to current application's NSURLRequest
property NSURLConnection : a reference to current application's NSURLConnection
property theServers : {"https://www.macscripter.net/viewforum.php?id=3", "https://www.macscripter.net/viewforum.php?id=4", "https://www.macscripter.net/viewforum.php?id=5", "https://www.macscripter.net/viewforum.php?id=6", "https://www.macscripter.net/viewforum.php?id=7", "https://www.macscripter.net/viewforum.php?id=8", "https://www.macscripter.net/viewforum.php?id=9", "https://www.macscripter.net/viewforum.php?id=10", "https://www.macscripter.net/viewforum.php?id=11", "https://www.macscripter.net/viewforum.php?id=12", "https://www.macscripter.net/viewforum.php?id=13", "https://www.macscripter.net/viewforum.php?id=14", "https://www.macscripter.net/viewforum.php?id=15", "https://www.macscripter.net/viewforum.php?id=16", "https://www.macscripter.net/viewforum.php?id=17", "https://www.macscripter.net/viewforum.php?id=18", "https://www.macscripter.net/viewforum.php?id=19", "https://www.macscripter.net/viewforum.php?id=20", "https://www.macscripter.net/viewforum.php?id=21", "https://www.macscripter.net/viewforum.php?id=22", "https://www.macscripter.net/viewforum.php?id=23", "https://www.macscripter.net/viewforum.php?id=24", "https://www.macscripter.net/viewforum.php?id=25", "https://www.macscripter.net/viewforum.php?id=26", "https://www.macscripter.net/viewforum.php?id=27", "https://www.macscripter.net/viewforum.php?id=28", "https://www.macscripter.net/viewforum.php?id=29", "https://www.macscripter.net/viewforum.php?id=30", "https://www.macscripter.net/viewforum.php?id=31", "https://www.macscripter.net/viewforum.php?id=32", "https://www.macscripter.net/viewforum.php?id=33", "https://www.macscripter.net/viewforum.php?id=34", "https://www.macscripter.net/viewforum.php?id=35", "https://www.macscripter.net/viewforum.php?id=36", "https://www.macscripter.net/viewforum.php?id=37", "https://www.macscripter.net/viewforum.php?id=38", "https://www.macscripter.net/viewforum.php?id=39", "https://www.macscripter.net/viewforum.php?id=40", "https://www.macscripter.net/viewforum.php?id=41", "https://www.macscripter.net/viewforum.php?id=42", "https://www.macscripter.net/viewforum.php?id=43", "https://www.macscripter.net/viewforum.php?id=44", "https://www.macscripter.net/viewforum.php?id=45", "https://www.macscripter.net/viewforum.php?id=46", "https://www.macscripter.net/viewforum.php?id=47", "https://www.macscripter.net/viewforum.php?id=48", "https://www.macscripter.net/viewforum.php?id=49", "https://www.macscripter.net/viewforum.php?id=50", "https://www.macscripter.net/viewforum.php?id=51", "https://www.macscripter.net/viewforum.php?id=52", "https://www.macscripter.net/viewforum.php?id=53", "https://www.macscripter.net/viewforum.php?id=54", "https://www.macscripter.net/viewforum.php?id=55", "https://www.macscripter.net/viewforum.php?id=56", "https://www.macscripter.net/viewforum.php?id=57", "https://www.macscripter.net/viewforum.php?id=58", "https://www.macscripter.net/viewforum.php?id=59", "https://www.macscripter.net/viewforum.php?id=60", "https://www.macscripter.net/viewforum.php?id=61", "https://www.macscripter.net/viewforum.php?id=62", "https://www.macscripter.net/viewforum.php?id=63", "https://www.macscripter.net/viewforum.php?id=64", "https://www.macscripter.net/viewforum.php?id=65", "https://www.macscripter.net/viewforum.php?id=66", "https://www.macscripter.net/viewforum.php?id=67", "https://www.macscripter.net/viewforum.php?id=68", "https://www.macscripter.net/viewforum.php?id=69", "https://www.macscripter.net/viewforum.php?id=70", "https://www.macscripter.net/viewforum.php?id=71", "https://www.macscripter.net/viewforum.php?id=72", "https://www.macscripter.net/viewforum.php?id=73", "https://www.macscripter.net/viewforum.php?id=74", "https://www.macscripter.net/viewforum.php?id=75", "https://www.macscripter.net/viewforum.php?id=76", "https://www.macscripter.net/viewforum.php?id=77", "https://www.macscripter.net/viewforum.php?id=78", "https://www.macscripter.net/viewforum.php?id=79", "https://www.macscripter.net/viewforum.php?id=80", "https://www.macscripter.net/viewforum.php?id=81", "https://www.macscripter.net/viewforum.php?id=82", "https://www.macscripter.net/viewforum.php?id=83", "https://www.macscripter.net/viewforum.php?id=84", "https://www.macscripter.net/viewforum.php?id=85", "https://www.macscripter.net/viewforum.php?id=86", "https://www.macscripter.net/viewforum.php?id=87", "https://www.macscripter.net/viewforum.php?id=88", "https://www.macscripter.net/viewforum.php?id=89", "https://www.macscripter.net/viewforum.php?id=90", "https://www.macscripter.net/viewforum.php?id=91", "https://www.macscripter.net/viewforum.php?id=92", "https://www.macscripter.net/viewforum.php?id=93", "https://www.macscripter.net/viewforum.php?id=94", "https://www.macscripter.net/viewforum.php?id=95", "https://www.macscripter.net/viewforum.php?id=96", "https://www.macscripter.net/viewforum.php?id=97", "https://www.macscripter.net/viewforum.php?id=98"}

set connectedServers to {}
repeat with aServer in theServers
	set aURL to (|NSURL|'s alloc()'s initWithString:aServer)
	set aRequest to (NSURLRequest's alloc()'s initWithURL:aURL cachePolicy:(1) timeoutInterval:5.0)
	if not ((NSURLConnection's sendSynchronousRequest:aRequest returningResponse:(missing value) |error|:(missing value)) is missing value) then set connectedServers to connectedServers & aServer
end repeat
return connectedServers

NOTE: As I see, makes sense convert this code to {on run, on idle} application (that is, background stay-open application) which would every 60 seconds check connections and write its states to text file. Then your main program can read the contents of this text file when is needed.


-- Here put the use statments and properties as is above

on run
	-- Here create little RAM disk. Creating RAM disk:
	set dName to "RAM Disk"
	set dCapacity to 2048 --2MB
	set aCmd to "diskutil erasevolume HFS+ '" & dName & "' `hdiutil attach -nomount ram://" & (dCapacity as string) & "`"
	try
		do shell script aCmd
	end try
	-- Here create new text file on RAM Disk
end run

on idle
	set connectedServers to ""
	repeat with aServer in theServers
		set aURL to (|NSURL|'s alloc()'s initWithString:aServer)
		set aRequest to (NSURLRequest's alloc()'s initWithURL:aURL cachePolicy:(1) timeoutInterval:5.0)
		if (NSURLConnection's sendSynchronousRequest:aRequest returningResponse:(missing value) |error|:(missing value)) is missing value then
			set end of connectedServers to "0"
		else
			set end of connectedServers to "1"
		end if
	end repeat
	-- here put code which writes text connectedServers to text file
	return 60 -- delay to check every 60 seconds
end idle

on quit
	tell application "Finder" to eject "RAM DISK"
end quit

Save it as stay-open application. Then your main program can read this big string of “0” and “1” and get state of each server by offset in this string. To make things faster, and to not destroy hard disk, makes sense create little RAM disk.

As you see, the solution is not so simple. Good luck.