Skip to content

Instantly share code, notes, and snippets.

@jossef
Created November 13, 2016 07:08
Show Gist options
  • Select an option

  • Save jossef/97b62a8444ffebab99d6523d01cb09dd to your computer and use it in GitHub Desktop.

Select an option

Save jossef/97b62a8444ffebab99d6523d01cb09dd to your computer and use it in GitHub Desktop.
python parse ipconfig command
import subprocess
import re
def get_interfaces():
output = subprocess.check_output("ipconfig /all")
lines = output.splitlines()
lines = filter(lambda x: x, lines)
ip_address = ''
mac_address = ''
name = ''
for line in lines:
# -------------
# Interface Name
is_interface_name = re.match(r'^[a-zA-Z0-9].*:$', line)
if is_interface_name:
# Check if there's previews values, if so - yield them
if name and ip_address and mac_address:
yield {
"ip_address": ip_address,
"mac_address": mac_address,
"name": name,
}
ip_address = ''
mac_address = ''
name = line.rstrip(':')
line = line.strip().lower()
if ':' not in line:
continue
value = line.split(':')[-1]
value = value.strip()
# -------------
# IP Address
is_ip_address = not ip_address and re.match(r'ipv4 address|autoconfiguration ipv4 address|ip address', line)
if is_ip_address:
ip_address = value
ip_address = ip_address.replace('(preferred)', '')
ip_address = ip_address.strip()
# -------------
# MAC Address
is_mac_address = not ip_address and re.match(r'physical address', line)
if is_mac_address:
mac_address = value
mac_address = mac_address.replace('-', ':')
mac_address = mac_address.strip()
if name and ip_address and mac_address:
yield {
"ip_address": ip_address,
"mac_address": mac_address,
"name": name,
}
if __name__ == '__main__':
for interface in get_interfaces():
print interface
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment