I work at an elementary school that has a wpa2 network. The network bumps off users at certain times, and since these mac book’s are being shared with regular classroom activities and then after school functions there is a need to have the computers switch network locations based on time of day.
What I am trying to do is have the computer switch to the network Bradley from 0730 to 1259 and then switch to the network CAPS from 1300 to 1830. Then in a perfect work to have the computer also run this script when it wakes up from sleep too.
The computers are all running 10.5.5.
Any help would be greatly appreciated.
Thank you,
Paul Allen
pallen@csusb.edu
--I am trying to have the network set the location to Bradley from the hours of 0730 to 1259
--and location CAPS from 1300 to 1830
--because of the WPA2 network logging off users at certain times
--script thus far
--find the time
set the date_stamp to ((the current date) as string)
--set the location based on time
if ((date_stamp > 8) and (date_stamp < 12)) then
do shell script "/usr/sbin/scselect Bradley"
end if
if ((date_stamp > 13) and (date_stamp < 20)) then
do shell script "/usr/sbin/scselect CAPS"
end if
--Connect Attempt
say "Connecting to the New Network... Please be patient."
display dialog "Connecting to the New Network.
Please be patient." buttons ["OK"] giving up after 5 with icon caution
--shell scripts
do shell script "networksetup -setairportpower off"
delay 1
do shell script "networksetup -setairportpower on"
delay 15
--Network Connection Check
try
do shell script "curl [url=http://www.apple.com]www.apple.com[/url]"
say "The Network is now available"
display dialog "Network now available.
You can use the internet." buttons ["OK"] giving up after 5 with icon stop
on error
say "Cannot Connect to the network. Please try again later or contact Mr. Allen."
display dialog "Cannot Connect to the network.
Please try again later or contact Mr. Allen x110287" buttons ["OK"] with icon stop
end try
delay 5
quit
quit
this is an approach using Leopard’s new capabilities to script network preferences.
The locations are only changed, if the time is in the time range and the current location is different
To run the script when the computer wakes up, you need a third party tool
--I am trying to have the network set the location to Bradley from the hours of 0730 to 1259
--and location CAPS from 1300 to 1830
--because of the WPA2 network logging off users at certain times
--get current location
tell application "System Events" to tell network preferences to set currentLocation to name of current location
--find the time
set currentTime to time of (current date) -- time in seconds
--set the location based on time
if (currentTime > 26999) and (currentTime < 46800) and currentLocation is not "Bradley" then
setCurrentLocation("Bradley")
resetNetwork()
else if (currentTime > 46799) and (currentTime < 66600) and currentLocation is not "CAPS" then
setCurrentLocation("CAPS")
resetNetwork()
end if
quit
on resetNetwork()
--shell scripts
do shell script "/usr/sbin/networksetup -setairportpower off"
delay 1
do shell script "/usr/sbin/networksetup -setairportpower on"
--Connect Attempt
say "Connecting to the New Network... Please be patient."
display dialog "Connecting to the New Network.
Please be patient." buttons ["OK"] giving up after 5 with icon caution
--Network Connection Check
if chkInternet(15) then
say "The Network is now available"
display dialog "Network now available.
You can use the internet." buttons ["OK"] giving up after 5 with icon stop
else
say "Cannot Connect to the network. Please try again later or contact Mr. Allen."
display dialog "Cannot Connect to the network.
Please try again later or contact Mr. Allen x110287" buttons ["OK"] with icon stop
end if
delay 5
end resetNetwork
on setCurrentLocation(L)
tell application "System Events" to tell network preferences to set current location to location L
end setCurrentLocation
on chkUP(theURL)
return (count (get ((theURL as URL)'s host & {dotted decimal form:""})'s dotted decimal form)) > 0
end chkUP
on chkInternet(ti)
repeat ti times
if chkUP("http://www.apple.com") or chkUP("http://www.google.com") then return true
delay 1
end repeat
return false
end chkInternet
To connect reliably to the specified network, you can use this line
do shell script "/System/Library/PrivateFrameworks/Apple80211.framework/Resources/airport -A'myNetwork' --password=¢¢¢¢¢¢¢"
Thank you soo much. You modifications make sense and seem to work. I only noticed one problem, and that is if the System lock is enabled then the location will not switch. So I was thinking of working with this idea to automatically unlock enter user information automatically, switch location and network connection and then lock again.
user : "user"
pwrd : "123456"
tell application "System Preferences"
activate
set the current pane to pane id "com.apple.preference.network"
get the name of every anchor of pane id "com.apple.preference.network"
tell application "System Events"
tell application process "System Preferences"
click button "Click the lock to make changes." of window "Network"
tell window 1
tell scroll area 1 of group 1
set value of text field 1 to user
set value of text field 2 to pwrd
end tell
click button "OK" of group 2
end tell
end tell
end tell
I am still working on that, just trying to figure out the window names.
You mentioned I needed to run an additional application to run this script from sleep. Do you recommend any?
Thank you again.
Regards,
Paul
property user : "user"
property pwrd : "123456"
tell application "System Preferences"
activate
set the current pane to pane id "com.apple.preference.network"
get the name of every anchor of pane id "com.apple.preference.network"
tell application "System Events"
tell application process "System Preferences"
click button "Click the lock to make changes." of window "Network"
tell window 1
tell scroll area 1 of group 1
set value of text field 1 to user
set value of text field 2 to pwrd
end tell
click button "OK" of group 2
end tell
end tell
end tell
end tell
I decided to add growl features to serve as an additional dialog warning box.
regards,
paul
--get current location
tell application "System Events" to tell network preferences to set currentLocation to name of current location
--to ensure connection if for some reason the computer will not connect to the wireless
do shell script "/usr/sbin/networksetup -setairportpower off"
delay 1
do shell script "/usr/sbin/networksetup -setairportpower on"
delay 2
--find the time
set currentTime to time of (current date) -- time in seconds
--set the location based on time
if (currentTime > 26999) and (currentTime < 46800) and currentLocation is not "Bradley" then
setCurrentLocation("Bradley")
resetNetwork()
else if (currentTime > 46799) and (currentTime < 66600) and currentLocation is not "CAPS" then
setCurrentLocation("CAPS")
resetNetwork()
end if
quit
on resetNetwork()
--shell scripts
do shell script "/usr/sbin/networksetup -setairportpower off"
delay 1
do shell script "/usr/sbin/networksetup -setairportpower on"
delay 2
--Connect Attempt
say "Connecting to the New Network... Please be patient... Please DO NOT TOUCH OR USE THE COMPUTERS while the computer is connecting to the network."
display dialog "Connecting to the Network.
Please be patient.
Please DO NOT TOUCH OR USE THE COMPUTERS while the computer is connecting to the network." buttons ["OK"] giving up after 5 with icon caution
tell application "GrowlHelperApp"
set the allNotificationsList to {"Test Connection"}
set the enabledNotificationsList to {"Test Connection"}
register as application "time_connect" all notifications allNotificationsList default notifications enabledNotificationsList icon of application "time_connect"
-- Send Growl Notification...
notify with name "Test Connection" title "Connecting to the new network" description "Please be patient because this process can take up to 20 seconds.
Please DO NOT TOUCH OR USE THE COMPUTERS while the computer is connecting to the network." application name "time_connect"
end tell
--Network Connection Check
if chkInternet(15) then
say "The Network is now available"
display dialog "Network now available.
You can use the internet." buttons ["OK"] giving up after 5 with icon stop
else
say "Cannot Connect to the network. Please try again later or contact Mr. Allen."
display dialog "Cannot Connect to the network.
Please try again later or contact Mr. Allen x110287" buttons ["OK"] with icon stop
tell application "GrowlHelperApp"
set the allNotificationsList to {"Bad Connection"}
set the enabledNotificationsList to {"Bad Connection"}
register as application "time_connect" all notifications allNotificationsList default notifications enabledNotificationsList icon of application "time_connect"
-- Send Growl Notification...
notify with name "Bad Connection" title "Cannot Connect to the network!" description "Please try again later or contact Mr. Allen x110287" application name "time_connect"
end tell
end if
delay 5
end resetNetwork
on setCurrentLocation(L)
tell application "System Events" to tell network preferences to set current location to location L
end setCurrentLocation
on chkUP(theURL)
return (count (get ((theURL as URL)'s host & {dotted decimal form:""})'s dotted decimal form)) > 0
end chkUP
on chkInternet(ti)
repeat ti times
if chkUP("http://www.apple.com") or chkUP("http://www.google.com") then return true
delay 1
end repeat
return false
end chkInternet
quit
quit