Mac – How to avoid password prompt in terminal

macpasswordterminal

So sometimes my Mac loses its connection to the Internet.

When I'm in front of it physically I can use the terminal and do the following to repair it:

networksetup -setnetworkserviceenabled ethernet off

type my password then:

networksetup -setnetworkserviceenabled ethernet on

and type my password again.

The thing is the machine is hosting several servers and I need them to be running even when I'm not physically present. So I wanted to run a daily script that would do this when I'm not present and be sure the connection is reset at least once a day. But since I haven't figured out a way to include my password in the command line I can't do that.

So my question is: is it possible to do what I described?

Best Answer

Ok, thank you Batman for suggesting to edit the sudoer file. Instead of crontab or launchd to schedule it once a day (which wasn't good enough anyway), I instead wrote that applescript to do it more regularly and so far it seems to be working:

repeat
    repeat
        try
            do shell script "ping -t 15 8.8.8.8"
            exit repeat
        on error
            display notification "Reconnection attempt"
            do shell script "sudo networksetup -setnetworkserviceenabled ethernet off"
            do shell script "sudo networksetup -setnetworkserviceenabled ethernet on"
            delay 9
            display notification "Connection should be established"
        end try
    end repeat
    delay 5
end repeat

I'm adding more details: I exported it as an .app then I edited the .plist of it with the lines:

<key>LSBackgroundOnly</key>
<string>1</string>

To make it invisible when it runs (no dock icon) and finally I made it run on login.