Skip to content

Instantly share code, notes, and snippets.

@acidprime
Created September 12, 2013 07:17
Show Gist options
  • Save acidprime/6533934 to your computer and use it in GitHub Desktop.
Save acidprime/6533934 to your computer and use it in GitHub Desktop.
Get the systems ethernet BSD device
#!/usr/bin/python
import re
import commands
import plistlib
from subprocess import Popen, PIPE, STDOUT
networksetup = '/usr/sbin/networksetup'
sysctl = '/usr/sbin/sysctl'
# Hardware Test for MacbookAir,*
def getPlatformPortName():
# Hardware test for Air
airTest = re.compile(".*(a|A)ir.*")
hwmodel = commands.getoutput(sysctl + " hw.model").split(' ')
if osVersion['minor'] >= LION:
# Updating this for retina displays and beyond?
arguments = [networksetup,'-listallhardwareports']
execute = Popen(arguments, stdout=PIPE)
out, err = execute.communicate()
retina_regex = re.search(".*(Wi-Fi|AirPort).*\nDevice: (en\d+)*",out)
bsd_port = retina_regex.group(2)
return bsd_port
else:
if airTest.match(hwmodel[-1]):
return 'en0'
else:
return 'en1'
def getSystemVersion():
# Our Operating System Constants
global LEOP,SNOW,LION,MLION,MAVRK
LEOP = 5
SNOW = 6
LION = 7
MLION = 8
MAVRK = 9
systemVersionPath = '/System/Library/CoreServices/SystemVersion.plist'
try:
systemVersion = plistlib.Plist.fromFile(systemVersionPath)
except:
print 'Unable to parse file at path: %s' % systemVersion
sys.exit(1)
ProductVersion = systemVersion['ProductVersion'].split('.')
returnDict = {}
returnDict['major'] = int(ProductVersion[0])
returnDict['minor'] = int(ProductVersion[1])
returnDict['bugfx'] = int(ProductVersion[2])
return returnDict
# Get OS Version
global osVersion
osVersion = getSystemVersion()
print getPlatformPortName()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment