Hi everyone,
I’m writing a script that uses “do shell script” to ping a web server, and if the ping time is above a certain threshold it repeats until the ping time is within the threshold. What I need to know is how to parse the ping results to extract the ping time.
Thanks,
Gabe
Model: MacBook Pro
AppleScript: 2.1.1
Browser: Firefox 3.6.2
Operating System: Mac OS X (10.6)
Hi,
Maybe this short sample code gets you started?
set address to "[url=http://www.apple.com]www.apple.com[/url]"
repeat 10 times
set {pingmstime, errmsg} to my getpingmstime(address)
if pingmstime is not false then
tell me
activate
display dialog "Address: " & address & return & "Time: " & pingmstime
end tell
else
tell me
activate
display dialog "Something went wrong:" & return & return & errmsg
end tell
end if
end repeat
on getpingmstime(address)
try
set output to do shell script "ping -c 1 " & address
set outputline to (item 2 of (paragraphs of output))
set esoffset to offset of "=" in (reverse of (characters of outputline)) as text
set mstime to (characters -(esoffset - 1) through -4 of outputline) as text
return {mstime, missing value}
on error errmsg number errnum
return {false, errmsg}
end try
end getpingmstime
Best regards,
Martin
Hi Martin,
Thanks! That helps a lot!
I’m having a bit of trouble using that returned value to set up a threshold. Basically, if the ping time returned over 100ms, I want the script to repeat until the ping time is under 100ms. If not, I want it to return an Automator workflow to be passed into an Automator action. Here’s what I’m using now, it could be totally wrong.
if pingmstime is greater than 1000 then
return (theWorkflowFolder & "Folder Backup.workflow") as alias
else if pingmstime is not greater than 1000 then
pause 500
end if
Does that make any sense?
Thanks!
Gabe
Hi,
I guess you mean something like this (did not test the code):
(You will need to save this code with the “Stay Open”-option, this way the script will run the «idle»-handler every 60 seconds or whatever you enter)
on idle
try
set address to "[url=http://www.apple.com]www.apple.com[/url]"
set {pingmstime, errmsg} to my getpingmstime(address)
if pingmstime is false then
return 60
else
set pingmstime to pingmstime as real
if pingmstime is greater than 1000 then
set workflowpath to (theWorkflowFolder & "Folder Backup.workflow") as text
set command to "/usr/bin/automator " & quoted form of POSIX path of workflowpath
do shell script command
end if
end if
end try
-- every 60 seconds = 1 Minute
return 60
end idle
on getpingmstime(address)
try
set output to do shell script "ping -c 1 " & address
set outputline to (item 2 of (paragraphs of output))
set esoffset to offset of "=" in (reverse of (characters of outputline)) as text
set mstime to (characters -(esoffset - 1) through -4 of outputline) as text
return {mstime, missing value}
on error errmsg number errnum
return {false, errmsg}
end try
end getpingmstime
Hi Martin,
I think that’s headed in the right direction, maybe let me explain what I’m doing. I’m running this script inside an automator workflow, and if the ping time is within the threshold it passes the automator action to the next step. Sorry, I’m really new at this
Thanks SO much for all your help!