Created
October 22, 2012 11:07
-
-
Save assassingj/3931024 to your computer and use it in GitHub Desktop.
monitor network status using systemconfigure
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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