Mac OS X:script實現添加無線網串連

來源:互聯網
上載者:User
Mac OS X:指令碼實現添加無線網串連

這是以前另一篇《Mac OS X:
bash指令碼實現添加無線SSID(Update2009-10-09)
》的更新,

 

      當初在Mac OS X 10.4 Tiger系統裡面,要通過命令添加無線網串連,那是相當的簡單,只要找到airport命令就好辦了。可是在Mac OS X 10.5 Leopard的各個版本中,就沒有那麼簡單了,因為原來的方式沒有使用Keychain來管理密碼,都是Airport自己管理密碼和串連的儲存,隨著安全性不斷提高的要求,Keychain被Apple接受為所有安全資訊管理的全域工具,因此無線網路也被劃歸通過它了管理了,另外,它還引入Preferred Network-優先網路-的概念,這樣就又多了一個需要管理和操作的要點。所以單單使用原來的airport命令就無法完成現在的任務了。同時Apple推薦使用networksetup這個命令,作為一個更高層次的命令來管理所有網路事物,所以大家也不必找airport了,而是通過networksetup來操作了,但是問題是,單單通過10.5中提供的networksetup -setairportnetwork <SSID> <password>命令依然無法完成任務,因為這個命令可以實現無線網串連,但是它不能儲存密碼,並把該串連添加到Preferred NEtwork中,這樣不用說重新啟動,就是切換使用者或者從ScreenSaver中回來,剛才建立的串連都會丟失。

 

      這樣的問題是,對於個人使用者沒有任何影響,因為絕大多數情況下,使用者通過圖形介面操作,而對於企業系統管理員就會很麻煩。原來的解決辦法是,把原來製作好的系統影像檔案重新製作,以便預先把企業的無線網路添加好。這樣雖然解決問題,但是還有不便之處,比如,一個大型企業/教育系統內,為了安全起見,不是所有的電腦都可以預設登陸到所有其它部門/學校的無線網路的,那麼系統管理員沒有一個遠程方便控制的方式,一是不利於遠程錯誤修正,而是沒有一個方法來控制電腦的可適應性。

 

      所以,現在在Mac OS X 10.6 Snow Leopard中添加了更多的命令列參數,針對無線網路的,就有如下的命令:

 

    #networksetup -getairportnetwork <device name>
    #networksetup -setairportnetwork <network> <device name> [password]
    #networksetup -getairportpower <device name>
    #networksetup -setairportpower <device name> <on off>
    #networksetup -listpreferredwirelessnetworks <device name>
    #networksetup -addpreferredwirelessnetworkatindex <device name> <network> <index> <security type> [password]
    #networksetup -removepreferredwirelessnetwork <device name> <network>
    #networksetup -removeallpreferredwirelessnetworks <device name>

 

      而其中的-addpreferredwirelessnetworkatindex參數就可以完全做到添加無線網路的要求。

 

      原來寫的那些指令碼, 星期五終於有時間調試了,其實它證明了我的判斷的正確,完全可以實現方便即時的控制無線網路的建立,從而為管理員在10.5 Leopard系統上方便控制無線網路提供便利。

      這裡把那個測試後的指令碼在帖一下,以備後用, 它需要plistbuddy命令列才操作plist檔案:

 

#!/bin/bash<br /># --------------------------------------------------------------------<br /># Script to add Wwireless connection to system preferred network<br />#<br /># Name: addwireless<br />#<br /># Operating System:<br /># Tested on 10.5.8<br />#<br /># 2010 Tony Liu<br /># Copyright GNU GPL<br /># Version 0.1.1<br />#<br /># Version History<br /># 0.0.1: 2009-10-11 Initial<br /># 0.1.1: 2010-05-29 testing passed<br /># --------------------------------------------------------------------<br />#<br /># Usage: change the following parameters to fit your needs.<br />#<br />yourSSID="YourwirelessSSID"<br />yourPWD="ThewirelessPasswrod"<br />SecurityType="WirelessType"<br /># WEP is tested<br />WEPOpenSystem=true<br />#<br /># Start.<br />#<br />logger "Adding Wireless started."<br />networkUUID=`uuidgen`<br />pwdUUID=`uuidgen`<br />cTime=`date "+%a %b %e %H:%M:%S %Z %Y"`<br />function addSSID_Airport_Pref<br />{<br />sudo plistbuddy /Library/Preferences/SystemConfiguration/com.apple.airport.preferences.plist -c "add KnownNetworks:$networkUUID dict"<br />sudo plistbuddy /Library/Preferences/SystemConfiguration/com.apple.airport.preferences.plist -c "add KnownNetworks:$networkUUID:SecurityType string $SecurityType"<br />sudo plistbuddy /Library/Preferences/SystemConfiguration/com.apple.airport.preferences.plist -c "add KnownNetworks:$networkUUID:SSID_STR string $yourSSID"<br />sudo plistbuddy /Library/Preferences/SystemConfiguration/com.apple.airport.preferences.plist -c "add KnownNetworks:$networkUUID:'Unique Password ID' string $pwdUUID"<br />sudo plistbuddy /Library/Preferences/SystemConfiguration/com.apple.airport.preferences.plist -c "add KnownNetworks:$networkUUID:'Remembered channels' array {0 integer 1}"<br />sudo plistbuddy /Library/Preferences/SystemConfiguration/com.apple.airport.preferences.plist -c "add KnownNetworks:$networkUUID:_timeStamp date $cTime"<br />sudo plistbuddy /Library/Preferences/SystemConfiguration/com.apple.airport.preferences.plist -c "add KnownNetworks:$networkUUID:WEPOpenSystem bool $WEPOpenSystem"<br />#sudo plistbuddy /Library/Preferences/SystemConfiguration/com.apple.airport.preferences.plist -c "print KnownNetworks:$networkUUID"<br />}</p><p>function addPWD_SystemKeychain<br />{<br />security add-generic-password -a "$yourSSID" -s "$pwdUUID" -p "$yourPWD" -l "$yourSSID" -D "AirPort network password" -A /Library/Keychains/System.keychain<br />}<br />function addPreferredNetworks<br />{<br />currentSet=`plistbuddy /Library/Preferences/SystemConfiguration/preferences.plist -c "print CurrentSet"`<br />currentSet=${currentSet/////":"}</p><p>sudo plistbuddy /Library/Preferences/SystemConfiguration/preferences.plist -c "add $currentSet:Network:Interface:en1:AirPort:PreferredNetworks:0 dict"<br />sudo plistbuddy /Library/Preferences/SystemConfiguration/preferences.plist -c "add $currentSet:Network:Interface:en1:AirPort:PreferredNetworks:0:SSID_STR string $yourSSID"<br />sudo plistbuddy /Library/Preferences/SystemConfiguration/preferences.plist -c "add $currentSet:Network:Interface:en1:AirPort:PreferredNetworks:0:SecurityType string 'WEP'"<br />sudo plistbuddy /Library/Preferences/SystemConfiguration/preferences.plist -c "add $currentSet:Network:Interface:en1:AirPort:PreferredNetworks:0:'Unique Newtork ID' string $networkUUID"<br />sudo plistbuddy /Library/Preferences/SystemConfiguration/preferences.plist -c "add $currentSet:Network:Interface:en1:AirPort:PreferredNetworks:0:'Unique Password ID' string $pwdUUID"<br />sudo plistbuddy /Library/Preferences/SystemConfiguration/preferences.plist -c "set $currentSet:Network:Interface:en1:AirPort:RequireAdmin bool No"<br />}<br />function CleanAll<br />{<br /># 1. Delete from preferences.plist<br />currentSet=`plistbuddy /Library/Preferences/SystemConfiguration/preferences.plist -c "print CurrentSet"`<br />currentSet=${AA/////":"}<br />sudo plistbuddy /Library/Preferences/SystemConfiguration/preferences.plist -c "delete $currentSet:Network:Interface:en1:AirPort:PreferredNetworks:0"<br />#2. Delete Airport_Pref<br />sudo plistbuddy /Library/Preferences/SystemConfiguration/com.apple.airport.preferences.plist -c "delete KnownNetworks:$networkUUID"<br />#3. Delete Keychain<br />security default-keychain -s /Library/Keychain/System.keychain<br />sudo security add-generic-password -a "$yourSSID" -D "AirPort network password" -s "" -l "" -w "" -j "wireless_setup_v1.0" -A -U<br />security default-keychain -s ""<br />}</p><p>ln -f /usr/libexec/PlistBuddy /usr/sbin/plistbuddy<br />addSSID_Airport_Pref<br />addPWD_SystemKeychain<br />addPreferredNetworks<br />networksetup -setairportpower off<br />sleep 5<br />networksetup -setairportpower on<br />logger "Adding Wireless finished successfully."<br />exit 0

 

Tony Liu, May 29, 2010

in Calgary

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.