How do you change network port configuration (ethernet off/on).

I want to create an applescript that will disable the built in ethernet port and one to enable it. I am not sure how to code this for the network pref pane.

Tangier

hi tan,

this should work, but for some reason i’m having trouble with it. it turns off the ethernet fine, but it does not seem to set it in ‘down’ mode, so my script will never let you reenable. i’ll look at this later and see if i can figure it out. any input welcome:


set addIp to "/sbin/ifconfig en0 up"
set killIp to "/sbin/ifconfig en0 down"
set checkIp to 0

try
	set checkIp to (do shell script "/sbin/ifconfig -d | grep en0")
end try

if checkIp is 0 then
	set myStatus to button returned of (display dialog "Do you really want to stop the built in ethernet?" buttons {"Yes", "Cancel"} default button "Cancel")
	if myStatus is "Yes" then
		do shell script killIp with administrator privileges
	end if
else
	set myStatus to button returned of (display dialog "Do you want to reenable the built in ethernet?" buttons {"Yes", "Cancel"} default button "Cancel")
	if myStatus is "Yes" then
		do shell script addIp with administrator privileges
	end if
end if

if myStatus is "Yes" then
	do shell script addIp with administrator privileges
end if

CAVEAT SCRIPTER: running this script will kill your built in ethernet. if you open the Terminal and do a ‘sudo ifconfig en0 down’ and then come back and run the script again i’ve found it does restore the connection.

Waltr;

After playing with this for a while (not with en0 but with en1, which I don’t use), it occurred to me that this might be a security issue. Perhaps AppleScripts are not supposed to be able to turn on an ethernet port. If you turn it on in the network pp, it says UP, but if you send it an up, it doesn’t.

hi adam,

yes, it’s kind of a mystery. if you go to the Terminal and type: ‘ifconfig en0 down’ (as root, or using sudo) and then you do ‘ifconfig -d | grep en0’, it will show (the -d flag is for ‘down’ interfaces). but when you do the same thing in AppleScript it kills the interface, but does not go into ‘down’ mode.

i’ll look into this a bit further. i think i can get it to work.

ok, i am officially a moron.

if you look at the earlier posted script, you’ll notice at the bottom there is an ‘if’ statement that always runs and since i used the same variable name for both dialog boxes. it tries to reenable but fails somehow and leaves me in a halfway state. here is the corrected code:


set addIp to "/sbin/ifconfig en0 up"
set killIp to "/sbin/ifconfig en0 down"
set checkIp to 0

try
	set checkIp to (do shell script "/sbin/ifconfig -d | grep en0")
end try

if checkIp is 0 then
	set myUpStatus to button returned of (display dialog "Do you really want to stop the built in ethernet?" buttons {"Yes", "Cancel"} default button "Cancel")
	if myUpStatus is "Yes" then
		do shell script killIp with administrator privileges
	end if
else
	set myDownStatus to button returned of (display dialog "Do you want to reenable the built in ethernet?" buttons {"Yes", "Cancel"} default button "Cancel")
	if myDownStatus is "Yes" then
		do shell script addIp with administrator privileges
	end if
end if

sorry! i’ve been bouncing in and out of a couple of different languages today. i originally put that ‘if’ statement down at the bottom so i could figure out the syntax, then cut and pase it, only i seem to have copied and pasted it. DOAH!

this new script is the real deal.