1# $OpenBSD: IfInfo.py,v 1.2 2020/12/25 14:25:58 bluhm Exp $ 2 3# Copyright (c) 2017 Florian Obser <florian@openbsd.org> 4# Copyright (c) 2020 Alexander Bluhm <bluhm@openbsd.org> 5# 6# Permission to use, copy, modify, and distribute this software for any 7# purpose with or without fee is hereby granted, provided that the above 8# copyright notice and this permission notice appear in all copies. 9# 10# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 11# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 12# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 13# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 14# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 15# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 16# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 17 18import pprint 19import subprocess 20import re 21 22class IfInfo(object): 23 def __init__(self, ifname): 24 self.ifname = ifname 25 self.mac = None 26 self.ll = None 27 self.out = subprocess.check_output(['ifconfig', ifname], 28 encoding='UTF-8') 29 self.parse(self.out) 30 31 def __str__(self): 32 return "{0}: mac: {1}, link local: {2}".format(self.ifname, 33 self.mac, self.ll) 34 35 def parse(self, str): 36 lines = str.splitlines() 37 for line in lines: 38 lladdr = re.match("^\s+lladdr (.+)", line) 39 link_local = re.match("^\s+inet6 ([^%]+)", line) 40 if lladdr: 41 self.mac = lladdr.group(1) 42 continue 43 elif link_local: 44 self.ll = link_local.group(1) 45 46 if self.mac and self.ll: 47 return 48