Mac OS X:script实现添加无线网连接

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

 

Tony Liu, May 29, 2010

in Calgary

你可能感兴趣的:(String,OS,Security,delete,NetWork,interface)