How to prevent password popup everytime for changing network configuration in OS X Mountain Lion?

Make sure that your scripts have the right permissions and owner. It should be root:wheel like this(wifi is the script name):

lion:~/Downloads napcae$ ls /usr/local/bin/
-rwxr-xr-x  1 root      wheel       739 25 Nov 18:35 wifi

You can achieve this by typing

lion:~/Downloads napcae$ sudo chown root:wheel wifi

Now you can sudo your commands in the script, i.e.

sudo networksetup -setnetworkserviceenabled "Huawei Modem" on;

The Terminal/Finder should stop asking for the password. I hope this helps.


not sure if this is the right way to do it from a security viewpoint, but it works:

1) Copy networksetup in /usr/sbin/ to another file (this way, all the other services using it will continue to require the password when run):

sudo cp /usr/sbin/networksetup /usr/sbin/NewNetConfig

2) Add this line in your sudoers file to skip password when calling NewNetConfig:

username   ALL=NOPASSWD: /usr/sbin/NewNetConfig

3) Restart. Then use this to change your network configuration and it shouldn't ask for a password:

sudo NewNetConfig -setsocksfirewallproxy "Wi-Fi" localhost ####

I had the same problem, instead I Used a service to switch the proxy state by pressing a shortcut key.

After you've done coping networksetup to NewNetConfig and editing /etc/sudoers, Create a new Service in Automator named "ChangeWiFiProxy". Add a Shell Script and add this script:

STATUS=`NewNetConfig -getsocksfirewallproxy Wi-Fi | grep -c 'Enabled: Y'`
if   [ "$STATUS" == "0" ] ; then
    sudo NewNetConfig -setsocksfirewallproxystate Wi-Fi on
say on
else
    sudo NewNetConfig -setsocksfirewallproxystate Wi-Fi off
    say off
fi

Then assign it a shortcut like Cmd + Option + ";" in "System Prefrences > Keyboard > Keyborad Shortcuts > Services".