I’d like to know if there’s a way via shell or otherwise to detect whether or not the Mac is connecter to the network.
I have a script that runs at everyone’s login. It mounts a server, and copies files down to the local hard drive. However, if the script runs on a laptop that’s not connected to our network, I’d like the script to just launch, see that there’s no network, and quit. I though this would work:
try
mount volume "afp://TheServer"
--do stuff
end try --If there's an error mounting the server, don't fret, just quit
But that still generates an error that the user has to say OK to if there’s no network connection at all.
try
ignoring application responses
mount volume "afp://TheServer"
--do stuff
end ignoring
end try --If there's an error mounting the server, don't fret, just quit
Seconding the try-error block methodology here. The problem is that you can detect if your network interface is up or down (via ifconfig), and you can see if you have valid DNS information . . . but does this mean you can get to the server you need? Not always. Best to put it in a try-error block that does nothing interactive on error and lets the script continue running.
waltr, I tried ‘ignoring application responses’ without luck.
Mikey-San, the try-error block methodology doesn’t work. It avoids an AppleScript error, but the error message above still pops up. (twice, actually). I’ll look into ifconfig.
I ended up ping’ing our local intranet site for a response before trying to mount the server.
Er, yeah. That’s what I get for posting when I’m high on crack. I should’ve realized that you were talking about the Finder’s “server not found error”, not an AppleScript error.
Yeah, you will likely have to dig into ifconfig and perhaps even ping.
sorry the solution didn’t work. the error must be coming from another part of your code, because when i run your example here with the ‘ignoring’, it fails silently. possibly from the ‘–do stuff’ portion.
I have the same problem (machines that may or may not be connected). Here’s an excerpt from a script I’ve been using for some time:
set serverURL to "[url=http://www.google.com]www.google.com[/url]" -- use the URL (or an IP address) of your server, or one that you know is ALWAYS up
set netStatus to "offline"
set shellCmd to "ping -q -c 1 " & serverURL & " | grep \"packet loss\" | awk '{ print $7 }'"
set ifInfo to do shell script shellCmd
if ifInfo = "" or ifInfo = "100%" then
beep
set msg to "Either you don't have an active network connection, or the file server is not available."
display dialog msg buttons {"OK"} default button 1 with icon note
else
set netStatus to "online"
end if
The shell command will return an empty string if it can’t locate the URL, or “100%” if the machine is not responding (i.e., it gets a 100% packet loss). If everything is working OK, you get some percentage less than 100% (usually zero).
try
ignoring application responses
mount volume "afp://TheServer"
end ignoring
end try --If there's an error mounting the server, don't fret, just quit
set chkDisk to list disks
if "TheServer" is in chkDisk then
display dialog "works"
--do stuff **Moved do stuff to here**
else
quit
end if