Search code examples
macosshellterminalautomator

How to delete a password from keychain with a shell command?


I wanted to make a shell script that connects to a hidden wireless network on my macbook pro by creating a network. However, I only want to connect to that network when I want to and to prevent airport from connecting automatically I'll have to delete the network's password from keychain.

So far I have:

networksetup -setairportnetwork en1 $ssid $networkpassword
networksetup -removepreferredwirelessnetwork en1 $ssid

Im also aware that in OSX 10.7 the security command can modify the keychain.

Anyway I would appreciate any help someone can give me. I'm also willing to use applescript or something else if it works. Thank you.


Solution

  • I figured it out. The command I used is:

    sudo security delete-generic-password -l $ssid "/Library/Keychains/System.keychain"
    

    sudo was needed because this keychain is located in the systems library. security also contains a lot more delete and useful commands and you can specify how you want to indicate the location in this command.


    edit: I was having some trouble using this command and went back and learned I could use another command to not remember the network at all which I would have used before if I had found it. With this option you can do a wealth of things with your adapter along with the networksetup option.

    Here is my final script which first runs as root. then it changes the system preferences to not remember new networks. Then it disconnects from any current networks. Then it adds the new network. Lastly, it makes the system remember new wifi networks.

    sudo -i
    sudo /System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport prefs RememberRecentNetworks=NO
    sudo /System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport -Z
    networksetup -setairportnetwork en1 $ssid $password
    sudo /System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport prefs RememberRecentNetworks=YES