Skip to content

Instantly share code, notes, and snippets.

@chilcote
Created October 11, 2015 18:58
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chilcote/6649e2867e1a547f4a06 to your computer and use it in GitHub Desktop.
Save chilcote/6649e2867e1a547f4a06 to your computer and use it in GitHub Desktop.
Get primary interface name and IP
#!/usr/bin/python
from SystemConfiguration import SCDynamicStoreCreate, SCDynamicStoreCopyValue
net_config = SCDynamicStoreCreate(None, "net", None, None)
def get_primaryinterface():
states = SCDynamicStoreCopyValue(net_config, "State:/Network/Global/IPv4")
return states['PrimaryInterface']
def get_ip_address(iface):
addresses = SCDynamicStoreCopyValue(net_config, "State:/Network/Interface/%s/IPv4" % iface)
return addresses['Addresses'][0]
primary_interface = get_primaryinterface()
print (primary_interface, get_ip_address(primary_interface))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment