Every time I connect to a VPN I need to extract my IP adress from ifconfig and manually update it in an rc-file accordingly.

I would like to automate this process. I.e. once connected to the VPN and the IP for the tun0 device got assigned, I would like to write said IP to the environment variable $VPN_IP.

Question: How can I automatically detect that the system is connected to a vpn, that tun0 has an IP address, and update the environment variable accordingly?

Extracting the IP from ifconfig output is the easiest part. But how do I conveniently detect the network state change on OSX? launchd? Automator script? Something totally different?

  • Where would you set the variable? Existing shells won't see it, no? What's the end result. Cracking that nut directly might be far more productive. – bmike Oct 25 '15 at 1:08
  • spawning a new shell before execution of my program should not be a problem, but indeed is not desireable. The end result should be that the rc file always holds the current IP for tun0 when the program is launched. – jottr Oct 25 '15 at 1:26
  • I would write the status to a named pipe or a file or better, use defaults write to set and read the status. There are several questions I recall on modifying your Von software to trigger a script which would be better than setting up a periodic check for VPN status. If you post the script you have already for checking ifconfig, the change might be trivial for someone to answer. – bmike Oct 25 '15 at 2:05
  • What sort of VPN client are you using? Most offer the ability to run a script whenever a tunnel is opened and/or closed – agentroadkill Oct 25 '15 at 3:26
  • I'm using Tunnelblick I couldn't find an option to run scripts in the settings though. Maybe it is hidden somewhere... – jottr Oct 25 '15 at 15:06

You must log in to answer this question.

Browse other questions tagged .