Skip to content

Instantly share code, notes, and snippets.

@assassingj
Created October 22, 2012 11:07
Show Gist options
  • Save assassingj/3931024 to your computer and use it in GitHub Desktop.
Save assassingj/3931024 to your computer and use it in GitHub Desktop.
monitor network status using systemconfigure
#!/usr/bin/python
# -*- coding: utf-8 -*-
from Foundation import *
from SystemConfiguration import *
import os
import commands
EVENT_LIST=['State:/Network/Global/IPv4']
def getIpAddress(iname):
status,output = commands.getstatusoutput("ifconfig | grep -A4 %s | grep 'inet\>' | awk '{print $2}'" % iname)
if status != 0 :
print "error while get ip address"
import sys
sys.exit(-1)
return output
def onIpChange(store, keys, info):
if SCDynamicStoreCopyValue(store, keys[0]) is not None:
print getIpAddress('en1')
store = SCDynamicStoreCreate(None, "ip change watcher", onIpChange, None)
SCDynamicStoreSetNotificationKeys(store, None, EVENT_LIST)
CFRunLoopAddSource(CFRunLoopGetCurrent(), SCDynamicStoreCreateRunLoopSource(None, store, 0), kCFRunLoopCommonModes)
CFRunLoopRun()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment