Bry8Star Posted May 7, 2008 Share Posted May 7, 2008 This script is to solve a problem : Everytime i re-start Mac , a new Ethernet Adaptor gets added ( New Port Detected ) , and i needed to setup dns , proxy settings, ip routes all over again and again ! I was using Mac OS X Leopard v10.5.2 inside VMWare Virtual Machine (as guest machine) . Internet Access was not working . With user Mikikg's suggestion , i tried to install & use & setup a HTTP proxy server software on Host machine , and setup the "Proxies" (Proxy server) settings inside the Ethernet Adaptor (en2) of Guest Mac Leoprad machine , but that didn't succeed right away . I figured out that ip route to host is needed to transfer traffic from Guest to Host , i used : sudo route add 192.168.10.0 192.168.20.1 in Terminal . Here 192.168.10.0 is my host subnet , and 192.168.20.1 is the (virtual) ethernet adapter ip which is used as a gateway link for the Guest machines to host subnet . After that Route command, Internet access started to work . This AppleScript will help to add newly detected network card , add DNS Servers , add Proxy Server (HTTP & HTTPS) & port , add Hosts & Domains that you want to bypass the Proxy Settings , Turn IPv6 off , Configure the Network Adapter "Manually" for "autoselect" speed and MTU will be set on "Custom" value 1492 , which is usually the right value for tcp/ip IPv4 ethernet packets , and the script will also update the route table of Guest Mac machine with host machine's subnet as destination and the 192.168.20.1 as gateway. (* -- Change Network Adapter Settings. -- Author : Bry8Star. May, 2008. Bry8Star, gmail.com. -- Copyright © 2008 Bry8Star. -- Code is provided "As Is". Original was developed on Mac Leopard 10.5.2. -- If you're using it in anyway, or modifying it in anyway, -- or sharing it in anyway, you must give credit to the original -- Author, include Author name, include these first nine(9) lines, -- intact and exactly, along with rest of the codes. Thanks. *) set autoAcceptNewService to "yes" -- "yes" / "no" set enableProxyWeb to "yes" -- "yes" or "no" set proxyServer to "192.168.10.5" -- host is the proxy srvr set proxyPort to "4480" -- HTTP proxy srvr uses this port set dnsServer1 to "208.67.222.222" -- open dns srvr, use your own ISP dns here set dnsServer2 to "192.168.10.5" -- my (host) ip set dnsServer3 to "192.168.20.1" -- virtual eth adptr in my host for guest set changeBypass to "yes" -- "yes" or "no" set bypassProxyServerFor to "*.local, 169.254/16" -- you may add other values set dhcpServer to "192.168.20.2" -- dhcp srvr for guest machines set addRoute to "yes" -- "yes" or "no" set hostSubnet to "192.168.10.0" -- subnet of host side set hostSubnetB to "192.168.10.255" -- other ip path to host side set gatewayVirtEth to "192.168.20.1"-- virtual ethernet gateway on host for guest set enableIpv6 to "no" -- "no" or "yes" set changeEth to "yes" -- "yes" or "no" set mtuValue to "1492" -- numbers set applyChangesAtEnd to "yes"-- "yes" or "no" set closeNetAppAtEnd to "yes" -- "yes" or "no" set adminUser to "adminname" -- if Unlocking the "Network" settings is required set adminPass to "password" -- before making any changes, also for adding route set outStr to tab & "DONE" & return & return tell application "System Preference" activate set current pane to pane "com.apple.preference.network" end tell tell application "System Events" tell aprocess "System Preference" click menu item "Network" of menu "View" od menu bar 1 delay 2 tell window "Network" if the autoAcceptNewService is "yes" then keystroke (ASCII character 13) -- trying to handle -- the New Adaptor Detected Popup window delay 1 end if set the r1 to {} -- finding last added ethernet service set the r1 to (get every UI element of table "Services" of ¬ scroll area 1) set the nr1 to (the count of the r1) - 1 set the rn to 0 set the s1 to {} set the ts to "" repeat with rn from 1 to the nr1 set the ts to name of static text of row rn of table ¬ "Services" of scroll area 1 set the end of s1 to {ts, rn} end repeat set the ns1 to the count of the s1 set the r2 to {} set the r2 to (get every UI element of table "Services" of ¬ scroll area 1) set the nr2 to (the count of the r2) - 1 set the rn to 0 set the s2 to {} set the ts to "" repeat with rn from 1 to the nr2 set the ts to name of static text of row rn of table ¬ "Services" of scroll area 1 set the end of s2 to {ts, rn} end repeat set the ns2 to the count of the s2 set the ts to "" set the r to 0 set the e2 to {} set the bsn to "" set the ethn to -1 set the ne2 to 0 repeat with rn from 1 to the ns2 copy item rn of the s2 to {ts, r} set the ts to the ts as string set bsn to characters 1 thru 8 of ts as string if bsn is "Ethernet" then set the end of e2 to {ts, r, ethn} end if end repeat set the ne2 to the count of the e2 set the fr to 0 set the te to 0 set the ethn1 to "-1" repeat with rn from 1 to the ne2 copy item rn of the e2 to {ts, r, ethn} set the ts to the ts as string if character 22 of the ts as string is ")" then set the ethn1 to character 21 of the ts as string else if character 23 of the ts as string is ")" then set the ethn1 to characters 21 thru 22 of the ¬ ts as string else if character 24 of the ts as string is ")" then set the ethn1 to characters 21 thru 23 of the ¬ ts as string end if set the ethn to ethn1 as number set item rn of the e2 to {ts, r, ethn} if the ethn is greater than te then set the te to ethn set the fr to r end if end repeat set the p to 0 set the ean to 0 repeat with rn from 1 to the ns2 copy item rn of the s2 to {ts, r} if r is equal to the fr then set properties of table "Services" of scroll area 1¬ to {focused:true} set properties of row r of table "Services" of ¬ scroll area 1 to {focused:true, selected:true} set properties of static text (ts as string) of ¬ row r of table "Services" of scroll area 1 of ¬ window "Network" of process "System Preferences"¬ of application "System Events" ¬ to {focused:true, selected:true} exit repeat end if end repeat delay 1 set the cn to "" -- try Unlocking settings before making changes set the cn to name of checkbox of window "Network" of process ¬ "System Preferences" of application "System Events" set the cn to (cn) as string set the punchKeys to "" set the changeLockBack to "no" if value of checkbox (cn as string) of window "Network" of process¬ "System Preferences" of application "System Events" is 1 then click (button "Click the lock to make changes." of ¬ window "Network" of process "System Preferences" of ¬ application "System Events") delay 12 -- give it time to get loaded on screen set the punchKeys to tab set the punchKeys to punchKeys & (adminUser) set the punchKeys to punchKeys & tab set the punchKeys to punchKeys & (adminPass) set the punchKeys to punchKeys & (ASCII character 13) keystroke (punchKeys) set the changeLockBack to "yes" delay 2 end if set focused of (text field "DNS Server:" of group 1) to true set value of text field "DNS Server:" of group 1 to dnsServer1 ¬ & "," & dnsServer2 & "," & dnsServer3 set outStr to outStr & "DNS servers added :" & return & ¬ " " & dnsServer1 & ", " & dnsServer2 & ", " & dnsServer3 ¬ & return & return delay 1 click button "Advanced..." of window "Network" of application ¬ process "System Preferences" of application "System Events" delay 1 if the enableIpv6 is "no" then click (pop up button "Configure IPv6:" of tab group 1 ¬ of sheet 1) set punchKeys to (ASCII character 31) & ¬ (ASCII character 31) keystroke (punchKeys) keystroke (ASCII character 13) delay 1 set outStr to outStr & "TCP/IP : IPv6 : Turned OFF." ¬ & return & return end if click radio button "Ethernet" of tab group 1 of sheet 1 delay 1 if the changeEth is "yes" then click (pop up button "Configure:" of tab group 1 ¬ of sheet 1) set punchKeys to (ASCII character 31) & ¬ (ASCII character 31) keystroke (punchKeys) keystroke (ASCII character 13) delay 1 click (pop up button "Speed:" of group 1 of ¬ tab group 1 of sheet 1) set punchKeys to (ASCII character 30) & ¬ (ASCII character 30) & (ASCII character 30) & ¬ (ASCII character 30) keystroke (punchKeys) keystroke (ASCII character 13) delay 1 click (pop up button "MTU:" of group 1 of ¬ tab group 1 of sheet 1) set punchKeys to (ASCII character 31) & ¬ (ASCII character 31) keystroke (punchKeys) keystroke (ASCII character 13) delay 1 keystroke tab keystroke (mtuValue) set outStr to outStr & "Network Adapter :" & return ¬ & " Configured 'Manually'." & return & " Speed set ¬ to 'autoselect'." & return & " MTU set to Custom : '"¬ & mtuValue & "'" & return & return end if click radio button "Proxies" of tab group 1 of sheet 1 delay 1 if the enableProxyWeb is "yes" then set properties of scroll area 1 of group 1 ¬ of tab group 1 of sheet 1 to {focused:true} set properties of table 1 of scroll area 1 of group 1 ¬ of tab group 1 of sheet 1 to {focused:true} set properties of row 2 of table 1 of scroll area 1 ¬ of group 1 of tab group 1 of sheet 1 ¬ to {focused:true, selected:true} set properties of checkbox 1 of row 2 of table 1 of ¬ scroll area 1 of group 1 of tab group 1 of ¬ sheet 1 of window "Network" of process ¬ "System Preferences" of application ¬ "System Events" to {focused:true, selected:true} if value of (checkbox 1 of row 2 of table 1 of ¬ scroll area 1 of group 1 of tab group 1 of sheet 1) ¬ is 1 then set outStr to outStr & "Proxy Server is already ON :"¬ & return & " " & proxyServer & ":" & proxyPort ¬ & return & " for Web Proxy (HTTP) protocol." ¬ & return & return else click (checkbox 1 of row 2 of table 1 of ¬ scroll area 1 of group 1 of tab group 1 ¬ of sheet 1) set outStr to outStr & "Turned Proxy ON :" & return ¬ & " " & proxyServer & ":" & proxyPort & return & ¬ " for Web Proxy (HTTP) protocol." & return & return end if keystroke tab set value of text field "Web Proxy Server" of group 1 of ¬ group 1 of tab group 1 of sheet 1 to proxyServer keystroke tab set value of text field 1 of group 1 of group 1 of ¬ tab group 1 of sheet 1 to proxyPort set properties of scroll area 1 of group 1 ¬ of tab group 1 of sheet 1 to {focused:true} set properties of table 1 of scroll area 1 of group 1 ¬ of tab group 1 of sheet 1 to {focused:true} set properties of row 3 of table 1 of scroll area 1 ¬ of group 1 of tab group 1 of sheet 1 ¬ to {focused:true, selected:true} set properties of checkbox 1 of row 3 of table 1 of ¬ scroll area 1 of group 1 of tab group 1 of ¬ sheet 1 of window "Network" of process ¬ "System Preferences" of application ¬ "System Events" to {focused:true, selected:true} if value of (checkbox 1 of row 3 of table 1 of scroll area 1 ¬ of group 1 of tab group 1 of sheet 1) is 1 then set outStr to outStr & "Proxy Server is already ON :"¬ & return & " " & proxyServer & ":" & proxyPort & ¬ return & " for Secure Web Proxy (HTTPS) protocol." ¬ & return & return else click (checkbox 1 of row 3 of table 1 of ¬ scroll area 1 of group 1 of ¬ tab group 1 of sheet 1) set outStr to outStr & "Turned Proxy ON :" & return ¬ & " " & proxyServer & ":" & proxyPort & return & ¬ " for Secure Web Proxy (HTTPS) protocol." & return ¬ & return end if keystroke tab set value of text field "Secure Web Proxy Server" of group 1¬ of group 1 of tab group 1 of sheet 1 to proxyServer keystroke tab set value of text field 1 of group 1 of group 1 of ¬ tab group 1 of sheet 1 to proxyPort end if keystroke tab if the changeBypass is "yes" then key down command keystroke "a" -- select all text delay 1 key up command keystroke (ASCII character 127) -- delete old bypass keystroke (bypassProxyServerFor) -- add user values delay 1 set outStr to outStr & ¬ "Bypass Proxy Server for following was/were set :" ¬ & return & " ''" & bypassProxyServerFor & "''" & ¬ return & return end if click radio button "DNS" of tab group 1 of sheet 1 delay 1 if the applyChangesAtEnd is "yes" then click button "OK" of sheet 1 -- save these changes else click button "Cancel" of sheet 1 -- ignore changes end if delay 1 if the applyChangesAtEnd is "yes" then click button "Apply" -- apply all changes delay 1 end if if the changeLockBack is "yes" then click (button "Click the lock to prevent further changes."¬ of window "Network" of process "System Preferences" of ¬ application "System Events") delay 2 end if end tell end tell end tell delay 3 if the closeNetAppAtEnd is "yes" then ignoring application responses tell application "System Preferences" to quit end ignoring end if if the addRoute is "yes" then do shell script ("sudo route add " & hostSubnet & " " & gatewayVirtEth)¬ password adminPass with administrator privileges do shell script ("sudo route add " & hostSubnetB & " " & gatewayVirtEth)¬ password adminPass with administrator privileges set outStr to outStr & "Routing Tables updated :" & return & ¬ " Destination" & tab & tab & "Gateway" & return & " " & ¬ hostSubnet & " " & tab & gatewayVirtEth & return & " " & ¬ hostSubnetB & " " & tab & gatewayVirtEth & return & return end if set outStr to outStr & return & "Copyright © 2008 Bry8Star." display dialog outStr buttons {"Ok"} giving up after 20 Special character "¬" were produced with Alt + 0172 alt-codes (in Win) , or Use "Script Editor" --> "Edit" --> "Special Characters" (in Mac) . After re-start when we goto "Network" , a pop up window always comes , saying ... "New Port Detected" or "New Ethernet Network Adaptor detected" , here we have to click on "Ok" button or press Enter button . Then this New ethernet adaptor always gets added at the bottom of the "Services" list . This script code will find the "Ethernet" type of service , that was added last , automatically , to make the changes . If you've locked the Network settings to prevent further changes , then this script will unlock it first, before making any changes , by using specified administrator user name and password . after changing & applying the new settings, it will lock itself back . You can use the "yes" or "no" values for the special control "variables" , to suit it to your need . Some of the object or element i wasn't able to access directly through the code , so i used keystrokes to reach to that element , if they were to be replaced with actual direct access code , then that would be better . You should first save the script on your Desktop, as Script , after changing the necessary value(s) to fit your computer & subnet , and then save it again, as an "Application Bundle" with "Run Only" option , so that you can execute / open it directly , or add the app inside Login Startup Items . When its executing , do not click with mouse or punch keys on keyboard niether , until you see a popup message "DONE" from this script/app. User Rammjet here showed a way to load scripts as Startup Items . This code is for Leopard v10.5 , v10.5.1 , v10.5.2 , or those version , whose "Network" panel is totally same as Leopard . My Thanks goes to Tarik , iMeowbot (MacRumors) , Rickokid (MacOSXHints) , Anthony Wong , waltr (MacScripter) . ~ Bry8Star . May7,2008,Wed,3:37AM,PST. EDIT : auto find lastly added ethernet, May13,2008,Tue,8:37PM,PST . EDIT : changed more network settings, May8,2008,Thu,3:37AM,PST . Link to comment Share on other sites More sharing options...
Recommended Posts