Connected is a LiteStep module that keeps track of your internet connection and executes commands when you connect and/or disconnect. Take a look at the readme for more info.

This module is distributed under the GNU Public Licence, the source code is included in the zipfile.

Connected 0.11 (5 kb) source
Connected 0.1 (10 kB)