Mac OS X: script to add Wireless Network Connections
This is another article titled Mac OS X:
Bash script implementation add wireless SSID (Update2009-10-09)
,
In the Mac OS X 10.4 tiger system, it was quite simple to add a wireless network connection through commands. Just find the Airport Command. However, in Mac OS X 10.5 leopard versions, it is not that simple, because the original method did not use keychain to manage passwords, all of which are airport's own password management and connection storage, with the increasing security requirements, keychain is accepted by Apple as a global tool for security information management. Therefore, wireless networks are managed through it. In addition, it also introduces the preferred network-priority network-concept, which adds another key point for management and operation. Therefore, using the original Airport Command alone cannot complete the current task. At the same time, it is recommended that Apple use the networksetup command as a high-level command to manage all network things, so you do not need to look for airport, but use networksetup to operate, but the problem is, the networksetup-setairportnetwork <SSID> <password> Command provided in 10.5 alone still cannot complete the task, because this command can achieve wireless network connection, but it cannot store the password, and add the connection to the preferred network. Needless to say, restarting means switching the user or returning from the screensaver. All the connections just created will be lost.
This problem does not affect individual users, because in most cases, users operate on the GUI, which is very troublesome for enterprise system administrators. The original solution is to re-create the original system image file so that the wireless network of the enterprise can be added in advance. In this way, although the problem is solved, there are still some inconveniences. For example, in a large enterprise/education system, to ensure security, not all computers can log on to the wireless network of all other departments/schools by default. Therefore, the system administrator does not have a convenient remote control method. First, it is not conducive to remote error correction, instead, there is no way to control the computer's adaptability.
Therefore, more command line parameters are added to Mac OS X 10.6 snow leopard. for wireless networks, the following command is provided:
# 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>
The-addpreferredwirelessnetworkatindex parameter can be used to add a wireless network.
The scripts I wrote finally had time to debug on Friday. In fact, they proved that my judgment was correct and allowed me to conveniently control the establishment of the wireless network in real time, this allows administrators to conveniently control wireless networks on the 10.5 leopard system.
Here we post the script after the test for future use. It requires the plistbuddy command line to operate the plist file:
#! /Bin/bash <br/> # ------------------------------------------------------------------ <br/> # script to add wwireless connection to system preferred network <br/> # Name: addwireless <br/> # operating system: <br/> # tested on 10.5.8 <br/> #2010 Tony Liu <br/> # copyright gnu gpl <br/> # version 0.1.1 <br/> # <br/> # version history <br/> #0.0.1: initial <br/> #0.1.1: 2010-05-29 testing passed <br/> # reset <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/> # Start. <br/> # <br/> logger "adding wireless started. "<br/> networkuuid = 'uidgen '<br/> pwduuid = 'uidgen' <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: 'membered channel' 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/network> setup-setairportpower off <br/> sleep 5 <br/> networksetup- setairportpower on <br/> logger "adding wireless finished successfully. "<br/> exit 0
Tony Liu, May 29,201 0
In Calgary