Created
October 11, 2015 18:58
-
-
Save chilcote/6649e2867e1a547f4a06 to your computer and use it in GitHub Desktop.
Get primary interface name and IP
This file contains 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 | |
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