Created
October 13, 2010 14:31
-
-
Save glarizza/624133 to your computer and use it in GitHub Desktop.
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
from SystemConfiguration import * | |
import syslog | |
def checkIP(self, interface): | |
"""This function accepts a BSD interface name and returns the system IP Address | |
for that interface using the SystemConfiguration Framework. | |
--- | |
Arguments: | |
interface - Either en0 or en1, the BSD interface name of a Network Adapter. | |
Returns: | |
bool - True or False depending on if the interface is both active and is on the Huron Network. | |
""" | |
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" | |
# If the interface isn't down, use our checkNetwork method to check that the IP is on our | |
# Huron Network. | |
return self.checkNetwork(str(keyStore['Addresses'][0])) | |
---------------------------OR THIS ------------------------ | |
#!/usr/bin/python2.5 | |
# | |
# LinkState.py - Library function for determining that state of network adapters | |
# | |
__author__ = 'Gary Larizza (gary@huronhs.com)' | |
__version__ = '0.1' | |
import subprocess | |
_IPCONFIG = '/usr/sbin/ipconfig' | |
def status(NetPort): | |
"""Uses /usr/sbin/ipconfig to check link status of a network adapter | |
Args: | |
NetPort: One of 'en0' or 'en1' - Ethernet or Airport | |
Returns: | |
Exit status code which determines link status. 0 is active, 1 is inactive. | |
""" | |
command = [_IPCONFIG, 'getifaddr', NetPort] | |
task = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE) | |
(stdout, stderr) = task.communicate() | |
return task.returncode |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment