Skip to content

Instantly share code, notes, and snippets.

@zielmicha
Created December 9, 2013 14:09
Show Gist options
  • Save zielmicha/7872751 to your computer and use it in GitHub Desktop.
Save zielmicha/7872751 to your computer and use it in GitHub Desktop.
# Usage: ifconfig | python ifco.py
import sys
import socket
readline = sys.stdin.readline
def ip2num(a):
return int(socket.inet_aton(a).encode('hex'), 16)
def num2ip(i):
return socket.inet_ntoa(hex(i)[2:].decode('hex'))
while True:
while True:
line = readline()
if 'Link encap:Ethernet' in line:
break
if not line:
break
if not line:
break
if not line.startswith('eth0.'):
continue
num = int(line.split()[0][5:])
while True:
line = readline()
if 'inet addr:' in line:
break
inetl = line.split()
s_addr = inetl[1].split(':')[1]
s_bcast = inetl[3].split(':')[1]
addr = ip2num(s_addr)
bcast = ip2num(s_bcast)
print 'h3. VLAN %d %s/%d' % (num, num2ip(addr & bcast), bin(bcast).count('1'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment