#!/usr/bin/env python

# Copyright (c) 2009, Giampaolo Rodola'. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

"""
A clone of 'ifconfig' on UNIX.

$ python scripts/ifconfig.py
lo (speed=0MB, duplex=?, mtu=65536, up=yes):
    IPv4     address   : 127.0.0.1
             broadcast : 127.0.0.1
             netmask   : 255.0.0.0
    IPv6     address   : ::1
             netmask   : ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff
    MAC      address   : 00:00:00:00:00:00
             broadcast : 00:00:00:00:00:00

eth0 (speed=100MB, duplex=full, mtu=1500, up=yes):
    IPv4     address   : 192.168.1.2
             broadcast : 192.168.1.255
             netmask   : 255.255.255.0
    IPv6     address   : fe80::c685:8ff:fe45:641
             netmask   : ffff:ffff:ffff:ffff::
    MAC      address   : c4:85:08:45:06:41
             broadcast : ff:ff:ff:ff:ff:ff
"""

from __future__ import print_function
import socket

import psutil


af_map = {
    socket.AF_INET: 'IPv4',
    socket.AF_INET6: 'IPv6',
    psutil.AF_LINK: 'MAC',
}

duplex_map = {
    psutil.NIC_DUPLEX_FULL: "full",
    psutil.NIC_DUPLEX_HALF: "half",
    psutil.NIC_DUPLEX_UNKNOWN: "?",
}


def main():
    stats = psutil.net_if_stats()
    io = psutil.net_io_counters(pernic=True)
    for nic, addrs in psutil.net_if_addrs().items():
        print("%s:" % (nic))
        if nic in stats:
            print(
                "    stats           : speed=%sMB, duplex=%s, mtu=%s, up=%s" %
                (stats[nic].speed, duplex_map[stats[nic].duplex],
                 stats[nic].mtu, "yes" if stats[nic].isup else "no"))
        if nic in io:
            print(
                "    incoming:       : bytes=%s, pkts=%s, errs=%s, drops=%s" %
                (io[nic].bytes_recv, io[nic].packets_recv,
                 io[nic].errin, io[nic].dropin))
            print(
                "    outgoing:       : bytes=%s, pkts=%s, errs=%s, drops=%s" %
                (io[nic].bytes_sent, io[nic].packets_sent,
                 io[nic].errout, io[nic].dropout))
        for addr in addrs:
            print("    %-5s" % af_map.get(addr.family, addr.family), end="")
            print(" address   : %s" % addr.address)
            if addr.broadcast:
                print("          broadcast : %s" % addr.broadcast)
            if addr.netmask:
                print("          netmask   : %s" % addr.netmask)
            if addr.ptp:
                print("       p2p       : %s" % addr.ptp)
        print("")


if __name__ == '__main__':
    main()

@@ -1276,16 +1276,25 @@
 )%0A    io
+_counters
  = psuti
@@ -1324,16 +1324,16 @@
 c=True)%0A
-
     for 
@@ -1439,35 +1439,40 @@
             
-print(%0A    
+st = stats%5Bnic%5D%0A
             
@@ -1467,24 +1467,30 @@
             
+print(
 %22    stats  
@@ -1500,16 +1500,46 @@
       : 
+%22, end='')%0A            print(%22
 speed=%25s
@@ -1565,24 +1565,26 @@
 %25s, up=%25s%22 %25
+ (
 %0A           
@@ -1592,19 +1592,10 @@
     
-(
 st
-ats%5Bnic%5D
 .spe
@@ -1611,24 +1611,16 @@
 x_map%5Bst
-ats%5Bnic%5D
 .duplex%5D
@@ -1620,16 +1620,24 @@
 duplex%5D,
+ st.mtu,
 %0A       
@@ -1648,25 +1648,8 @@
     
-  stats%5Bnic%5D.mtu,
  %22ye
@@ -1656,24 +1656,16 @@
 s%22 if st
-ats%5Bnic%5D
 .isup el
@@ -1694,16 +1694,25 @@
 ic in io
+_counters
 :%0A      
@@ -1713,34 +1713,46 @@
             
-print(%0A   
+io = io_counters%5Bnic%5D%0A
             
@@ -1747,25 +1747,30 @@
             
- 
+print(
 %22    incomin
@@ -1770,17 +1770,17 @@
 incoming
-:
+ 
        :
@@ -1772,32 +1772,62 @@
 coming        : 
+%22, end='')%0A            print(%22
 bytes=%25s, pkts=%25
@@ -1841,32 +1841,34 @@
 =%25s, drops=%25s%22 %25
+ (
 %0A               
@@ -1864,32 +1864,26 @@
             
-(
 io
-%5Bnic%5D
 .bytes_recv,
@@ -1881,29 +1881,24 @@
 tes_recv, io
-%5Bnic%5D
 .packets_rec
@@ -1903,33 +1903,11 @@
 ecv,
-%0A                 io%5Bnic%5D
+ io
 .err
@@ -1908,29 +1908,24 @@
 io.errin, io
-%5Bnic%5D
 .dropin))%0A  
@@ -1940,32 +1940,37 @@
   print(
-%0A
+%22
     
-   
+outgoing
         
  %22    ou
@@ -1965,32 +1965,40 @@
     
+:
  %22
-    outgoing:       : 
+, end='')%0A            print(%22
 byte
@@ -2032,16 +2032,18 @@
 ps=%25s%22 %25
+ (
 %0A       
@@ -2055,16 +2055,10 @@
     
-(
 io
-%5Bnic%5D
 .byt
@@ -2068,21 +2068,16 @@
 sent, io
-%5Bnic%5D
 .packets
@@ -2086,33 +2086,11 @@
 ent,
-%0A                 io%5Bnic%5D
+ io
 .err
@@ -2096,21 +2096,16 @@
 rout, io
-%5Bnic%5D
 .dropout

improve ifconfig script