3

I am trying to calculate total network traffic on wlan1 interface through a python code. Uptil now I tried ethtool,iftop,ifstat,nethogs but most of these tools show ncurses interface(text base UI).

I tried something like this

import subprocess
nw_usage = subprocess.Popen(['ifstat', '-i', 'wlan1'])

but it doesn't give me network usage values.

I could not figure out how to get network usage values in a single variable from ncurses interfaces. ( and I am getting a feeling that there would be some better way to calculate network usage)

Any help or direction will be a great favor.

Thanks

4
  • Have you considered tshark, the commandline version of wireshark? Commented Jul 22, 2014 at 20:53
  • Define network usage. What exactly you want to know? Traffic speed in network interfaces? Commented Jul 23, 2014 at 4:03
  • I need transmit rate on a network interface at a given time. e.g. x=somefunction(wlan1) should give me x Kbps/Mbps which I can compare with max transmission rate of that interface (i.e. 56Mbps connection speed) Commented Jul 23, 2014 at 4:39
  • inspectorG4dget I just tried tshark,but I don't need packets... I need transmission rate Commented Jul 23, 2014 at 4:41

2 Answers 2

5

i know that the question is some weeks old but maybe this answer can still be helpful :)

You could read the device statistics from /proc/net/dev. Read the transmitted / received bytes in a interval and calculate the difference. Here is some simple Python script i hacked together

import re
import time


# A regular expression which separates the interesting fields and saves them in named groups
regexp = r"""
  \s*                     # a interface line  starts with none, one or more whitespaces
  (?P<interface>\w+):\s+  # the name of the interface followed by a colon and spaces
  (?P<rx_bytes>\d+)\s+    # the number of received bytes and one or more whitespaces
  (?P<rx_packets>\d+)\s+  # the number of received packets and one or more whitespaces
  (?P<rx_errors>\d+)\s+   # the number of receive errors and one or more whitespaces
  (?P<rx_drop>\d+)\s+      # the number of dropped rx packets and ...
  (?P<rx_fifo>\d+)\s+      # rx fifo
  (?P<rx_frame>\d+)\s+     # rx frame
  (?P<rx_compr>\d+)\s+     # rx compressed
  (?P<rx_multicast>\d+)\s+ # rx multicast
  (?P<tx_bytes>\d+)\s+    # the number of transmitted bytes and one or more whitespaces
  (?P<tx_packets>\d+)\s+  # the number of transmitted packets and one or more whitespaces
  (?P<tx_errors>\d+)\s+   # the number of transmit errors and one or more whitespaces
  (?P<tx_drop>\d+)\s+      # the number of dropped tx packets and ...
  (?P<tx_fifo>\d+)\s+      # tx fifo
  (?P<tx_frame>\d+)\s+     # tx frame
  (?P<tx_compr>\d+)\s+     # tx compressed
  (?P<tx_multicast>\d+)\s* # tx multicast
"""


pattern = re.compile(regexp, re.VERBOSE)


def get_bytes(interface_name):
    '''returns tuple of (rx_bytes, tx_bytes) '''
    with open('/proc/net/dev', 'r') as f:
        a = f.readline()
        while(a):
            m = pattern.search(a)
            # the regexp matched
            # look for the needed interface and return the rx_bytes and tx_bytes
            if m:
                if m.group('interface') == interface_name:
                    return (m.group('rx_bytes'),m.group('tx_bytes'))
            a = f.readline()


while True:
    last_time  = time.time()
    last_bytes = get_bytes('wlan0')
    time.sleep(1)
    now_bytes = get_bytes('wlan0')
    print "rx: %s B/s, tx %s B/s" % (int(now_bytes[0]) - int(last_bytes[0]), int(now_bytes[1]) - int(last_bytes[1]))
Sign up to request clarification or add additional context in comments.

2 Comments

Awesome, works fine even with Python 3. Really nice job!
Really nice job (Y)
0

There's probably a better way, but an ugly workaround I used to estimate network rates from the command line is:

ifconfig; sleep 10; ifconfig;

Then just subtract the "TX bytes" (in case of transmission) on the relevant interface and divide by 10 (the sleep time) for a rough estimate in Bytes per second.

1 Comment

I need a bit faster results, somewhat closer to real time

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.