How can I execute a bash script when the external IP is changed. I wonder if there is a hook in macOS to run such a script.

1 Answer 1


Your computer has no way to detect by itself that the external IP has changed, as this is handled by the router that translates your private IP to the public one.

So you must poll either the router itself, or an external site.

The router can be interrogated through SNMP le if it supports this protocol. If it doens't but has a command line (CLI) access then you can use it with a language like expect. If the router is only accesible through a web interface, this is doable but more complex.

You can actually avoid polling if the router support sending SNMP traps or has an event handler, then it is the router that can notify you computer about the change, and then you run your script on reception of such notification.

  • You may be able to use uPnP for this as well, but probably SNMP and uPnP are overkill and you can poll some external site giving you the request ip.
    – allo
    Commented Sep 14, 2017 at 14:13
  • @allo Polling an external site periodically is what I'm thinking to do, since I'm more comfortable to implement it.
    – gfot
    Commented Sep 14, 2017 at 14:23
  • There are services like dyndns that provide a dns name and polls to see when the up address changes
    – mmmmmm
    Commented Sep 14, 2017 at 21:48

You must log in to answer this question.

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