'sudo ifcong en0 down' - does not stay down in OS X?

My goal is to terminate all ethernet traffic through en0 until I bring it back up with ifconfig en0 up.

When I run ifconfig en0 down through terminal, and then confirm ifconfig and all interfaces are inactive.

However, the next day, I find that en0 is back up automatically. How can I find out what brought it up, and is there a better way to ensure it stays down?


Solution 1:

Use the networksetup command instead. On my system en0 is Wi-Fi, so I use networksetup -setairportpower en0 off. I actually use a little AppleScript that sits in my Dock and toggles the state. See the networksetup manual page for additional information using networksetup.

Save the AppleScript code as an application named Toggle Wi-Fi and then you can run it as needed/wanted. If you run it and Wi-Fi is on, it turns it off, and if it's off, it turns it on.

on run
    set interface to "en0"
    set status to do shell script "ifconfig " & interface & " | awk '/status:/{print $2}'"

    if status is "inactive" then
        do shell script "networksetup -setairportpower " & interface & "  on"
        display dialog "The Wi-Fi Network Adapter is turned: ON" with title "Wi-Fi Network Adapter Status" buttons {"OK"} default button 1 giving up after 3
    else
        do shell script "networksetup -setairportpower " & interface & "  off"
        display dialog "The Wi-Fi Network Adapter is turned: OFF" with title "Wi-Fi Network Adapter Status" buttons {"OK"} default button 1 giving up after 3
    end if
end run

Updated to address Ethernet Adapter usage:

on run
    set interfaceNumber to "en0"
    set interfaceName to "Display Ethernet"
    set status to do shell script "ifconfig " & interfaceNumber & " | awk '/status:/{print $2}'"

    if status is "inactive" then
        do shell script "networksetup -setnetworkserviceenabled " & quoted form of interfaceName & " on with administrator privileges"
        display dialog "Network Adapter " & interfaceNumber & " is turned: ON" with title interfaceName & " Network Adapter Status" buttons {"OK"} default button 1 giving up after 3
    else
        do shell script "networksetup -setnetworkserviceenabled " & quoted form of interfaceName & " off with administrator privileges"
        display dialog "Network Adapter " & interfaceNumber & " is turned: OFF" with title interfaceName & " Network Adapter Status" buttons {"OK"} default button 1 giving up after 3
    end if
end run

Note: In the code above the name of my Ethernet Adapter is "Display Ethernet" and you'll need to change it to whatever the name of your Ethernet Adapter is. You can ascertain it in System Preferences > Network.