Skip to content

Instantly share code, notes, and snippets.

@glarizza
Created October 19, 2010 16:52
Show Gist options
  • Save glarizza/634552 to your computer and use it in GitHub Desktop.
Save glarizza/634552 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
from SystemConfiguration import *
import syslog
interface = "en0"
store = SCDynamicStoreCreate(None, "global-network", None , None)
ifKey = "State:/Network/Interface/" + interface + "/IPv4"
keyStore = SCDynamicStoreCopyValue(store, ifKey)
# If the Print fails, then there's no IP Address for that interface and we can assume
# that the interface is in a down state - we return false
try:
print "The IP Address for interface: " + interface + " is: " + keyStore['Addresses'][0]
except TypeError:
syslog.syslog(syslog.LOG_ALERT, "Interface " + interface + " not active.")
return "false"
# The IP Address is returned in Unicode - beware of that
return keyStore['Addresses'][0]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment