2

On OSX 10.10 I created a plist file in /Library/LaunchDaemons/ to start at launch an OpenVPN connection. However I encountered DNS problems when going from an internet interface to another (as WiFi to LAN Network) and thought that I needed to restart my OpenVPN daemon with launchctl to fix it.

But with this method I need to go to a console, type a monstrous launchctl's command with sudo and, of course, type my admin password.

Can this typing process be automated?

Edit

Thanks for @bmike general answer which will open new horizons and @Graham Miln though I'm not fond of AppleScript, but as @Mark requested here's my .plist file :

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>KeepAlive</key>
    <true/>
    <key>Label</key>
    <string>net.openvpnd</string>
    <key>ProgramArguments</key>
    <array>
        <string>/usr/sbin/openvpn</string>
        <string>/a/path/to/file.ext</string>
    </array>
    <key>RunAtLoad</key>
    <true/>
    <key>SuccessfullExit</key>
    <true/>
    <key>StandardErrorPath</key>
    <string>/Library/Logs/ovpnerr.log</string>
    <key>StandardOutPath</key>
    <string>/Library/Logs/ovpn.log</string>
    <key>EnvironmentVariables</key>
    <dict>
      <key>PATH</key>
      <string>/usr/sbin:/another/path</string>
    </dict>
</dict>
</plist>
  • 1
    As the daemon runs as root you would need to run as root - simple solution put the long command in an alias of shell script. However in this case I think the daemon should only be run as needed to will restart only when needed - so we need to see your plist to suggest what it should be – Mark Nov 29 '16 at 10:57
  • I've answered the general - "what automation options exist" - feel free to ask a follow on question with the specific details @mark has mentioned if you need help with letting launchd handling the start / stop for you systematically. – bmike Nov 29 '16 at 12:05
  • Smells like an X-Y problem. Better fix your OpenVPN config/DNS setup. – klanomath Dec 1 '16 at 9:51
  • Hi @klanomath, I would totally agree but I'm now curious about ways to restart a root daemon without admin privileges. I'm also excited about knowing ways to resolve my X problem, could you write an answer here or should I ask another question ? – PaulCo Dec 1 '16 at 19:03
1

You could automate this process using AppleScript's do shell script … with administrator privileges command.

do shell script "command" user name "username" password "pass" with administrator privileges

The AppleScript can be saved as an application and launched as needed.

To avoid needing to embed administrator credentials within the AppleScript, omit the user name and password parameters. Without these parameters, you will be prompted to enter your credentials each time.

To avoid needing to authenticate each time, while also avoiding embedded credentials, you can investigate storing the credentials in your local Keychain and adding your AppleScript application to the Access Control list through Keychain Access.app.

0

The first thing to automate is use a shell alias or write a shell/Python script to cut down the typing. If you choose a script, you could add it to the sudoers file so the admin password isn't needed for that script.

Next up would be something like fastscripts or LaunchBar apps to use a shortcut key to trigger your script. Apple's automation tools would work too. There are dozens of ways to improve or automate this, but the tools do have a learning curve to use properly.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .