mounting a server on a wireless network

spent a number of hours getting this to work, and as such i thought i’d share it with you, dear readers.

most of you probably have no use for this, but for the slice of you involved in installation work or IT with apple computers, this applescript may come in handy.

placed in your Login Items, this script will do the following on login:

  • attempt repeatedly to join a wireless network, whether hidden or visible
  • ping a server on that network until the server responds, and then
  • mount a volume on that server.

hope someone out there finds the script, or parts of it, useful.

-- -----join wireless network-----
set networkResult to false
repeat while not networkResult
    try
        
        do shell script "networksetup -setairportnetwork networkName networkPassword | /bin/bash"
        
        -- network found and joined.
        set networkResult to true
        
    on error errorMsg
        
        -- network not yet available;
        -- wait five seconds and try again.
        set networkResult to false
        delay 5
        
    end try
end repeat

-- -----mount server-----
set serverIP to "10.0.1.1"
set serverUsername to "username"
set serverPassword to "password"
set pingResult to "100% packet loss"
set serverFound to false

repeat while not serverFound
    try
        set pingResult to (do shell script ("ping -c 1 " & serverIP))
    on error e
        set pingResult to e
    end try
    
    if pingResult contains "100% packet loss" then
        -- server not found; delay and then try again
        set serverFound to false
        delay 5
    else
        -- server found; exit loop and mount server
        set serverFound to true
    end if
    
end repeat

-- delay to allow server to finish booting
delay 90
mount volume "afp://" & serverIP as user name serverUsername with password serverPassword

1 Tweet

2 Comments

  1. ericsoco says:

    note that if you’re on snow leopard, you have to add the ‘device name’ to the networksetup shell command. for airport, this is most likely ‘en1′, so the line changes to:

    do shell script "networksetup -setairportnetwork en1 networkName networkPassword | /bin/bash"

    thanks to this site for the help figuring this one out…

Leave a Reply

Additional comments powered by BackType