|
| 1 | +#! /usr/bin/python |
| 2 | +# -*- python -*- |
| 3 | +# -*- coding: utf-8 -*- |
| 4 | +# Copyright (C) 2008 Red Hat Inc. |
| 5 | +# |
| 6 | +# Arnaldo Carvalho de Melo <acme@redhat.com> |
| 7 | +# |
| 8 | +# This application is free software; you can redistribute it and/or |
| 9 | +# modify it under the terms of the GNU General Public License |
| 10 | +# as published by the Free Software Foundation; version 2. |
| 11 | +# |
| 12 | +# This application is distributed in the hope that it will be useful, |
| 13 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | +# General Public License for more details. |
| 16 | + |
| 17 | +import getopt, ethtool, sys |
| 18 | + |
| 19 | +def usage(): |
| 20 | + print '''Usage: |
| 21 | + pifconfig <interface> |
| 22 | +''' |
| 23 | + |
| 24 | +def flags2str(flags): |
| 25 | + string = "" |
| 26 | + if flags & ethtool.IFF_UP: |
| 27 | + string += "UP " |
| 28 | + if flags & ethtool.IFF_BROADCAST: |
| 29 | + string += "BROADCAST " |
| 30 | + if flags & ethtool.IFF_DEBUG: |
| 31 | + string += "DEBUG " |
| 32 | + if flags & ethtool.IFF_LOOPBACK: |
| 33 | + string += "LOOPBACK " |
| 34 | + if flags & ethtool.IFF_POINTOPOINT: |
| 35 | + string += "POINTOPOINT " |
| 36 | + if flags & ethtool.IFF_NOTRAILERS: |
| 37 | + string += "NOTRAILERS " |
| 38 | + if flags & ethtool.IFF_RUNNING: |
| 39 | + string += "RUNNING " |
| 40 | + if flags & ethtool.IFF_NOARP: |
| 41 | + string += "NOARP " |
| 42 | + if flags & ethtool.IFF_PROMISC: |
| 43 | + string += "PROMISC " |
| 44 | + if flags & ethtool.IFF_ALLMULTI: |
| 45 | + string += "ALLMULTI " |
| 46 | + if flags & ethtool.IFF_MASTER: |
| 47 | + string += "MASTER " |
| 48 | + if flags & ethtool.IFF_SLAVE: |
| 49 | + string += "SLAVE " |
| 50 | + if flags & ethtool.IFF_MULTICAST: |
| 51 | + string += "MULTICAST " |
| 52 | + if flags & ethtool.IFF_PORTSEL: |
| 53 | + string += "PORTSEL " |
| 54 | + if flags & ethtool.IFF_AUTOMEDIA: |
| 55 | + string += "AUTOMEDIA " |
| 56 | + if flags & ethtool.IFF_DYNAMIC: |
| 57 | + string += "DYNAMIC " |
| 58 | + |
| 59 | + return string.strip() |
| 60 | + |
| 61 | +def show_config(device): |
| 62 | + ipaddr = ethtool.get_ipaddr(device) |
| 63 | + netmask = ethtool.get_netmask(device) |
| 64 | + flags = ethtool.get_flags(device) |
| 65 | + print '%-9.9s' % device, |
| 66 | + if not (flags & ethtool.IFF_LOOPBACK): |
| 67 | + print "HWaddr %s" % ethtool.get_hwaddr(device), |
| 68 | + print ''' |
| 69 | + inet addr:%s''' % ipaddr, |
| 70 | + if not (flags & (ethtool.IFF_LOOPBACK | ethtool.IFF_POINTOPOINT)): |
| 71 | + print "Bcast:%s" % ethtool.get_broadcast(device), |
| 72 | + print ''' Mask:%s |
| 73 | + %s |
| 74 | +''' % (netmask, flags2str(flags)) |
| 75 | + |
| 76 | +def main(): |
| 77 | + global all_devices |
| 78 | + |
| 79 | + try: |
| 80 | + opts, args = getopt.getopt(sys.argv[1:], |
| 81 | + "h", |
| 82 | + ("help",)) |
| 83 | + except getopt.GetoptError, err: |
| 84 | + usage() |
| 85 | + print str(err) |
| 86 | + sys.exit(2) |
| 87 | + |
| 88 | + for o, a in opts: |
| 89 | + if o in ("-h", "--help"): |
| 90 | + usage() |
| 91 | + return |
| 92 | + |
| 93 | + active_devices = ethtool.get_active_devices() |
| 94 | + for device in active_devices: |
| 95 | + show_config(device) |
| 96 | + |
| 97 | +if __name__ == '__main__': |
| 98 | + main() |
0 commit comments