Una tarea común para un administrador de sistemas es programar scripts para hagilizar las tareas rutinarias, una de ellas muchas veces es inventariar servidores, versiones de SO, interfaces, ips, versiones de software, en este artículo nos vamos a centrar en como obtener las interfaces, la MAC y cada una de las ips.
El script en si mismo es bastante autoexplicativo, mediante ciertas librerias conseguimos obtener toda la info necesaria:
#! /usr/bin/python
import os
import sys
import socket
from netifaces import interfaces, ifaddresses, AF_INET, AF_LINK
from IPy import IP
hostname = socket.gethostname()
print "-------------------------------"
print "Analyzing Host: " + hostname
print "-------------------------------"
print " "
print "--- Interface List: ---"
for ifaceName in interfaces():
# Check Vlan Config
if ifaceName.count("."):
(iface, vlan) = ifaceName.split(".")
else:
iface = ifaceName
vlan = "None"
# Check MAC in device
if AF_LINK in ifaddresses(ifaceName):
mac = ifaddresses(ifaceName)[AF_LINK][0]['addr']
else:
mac = "None"
# Check IP Address and print them!!
if AF_INET in ifaddresses(ifaceName):
for address in ifaddresses(ifaceName)[AF_INET]:
type = IP(address['addr']).iptype()
netmask = address['netmask']
print "-- Interface: " + str(iface)
print " ++ MAC: " + mac
print " ++ VLAN: " + vlan
print " ++ IP: " + address['addr']
print " ++ Netmask: " + netmask
print " ++ Type " + type
else:
print "-- Interface: " + str(iface)
print " ++ MAC: " + mac
print " ++ VLAN: " + vlan
print " "