查看linux網路流量小程式: netiostat

來源:互聯網
上載者:User

#!/usr/bin/python<br />#coding=utf-8<br />#file : netiostat<br />#author : flynetcn<br />from __future__ import division<br />import sys<br />import os<br />import time<br />import signal<br />netcmd = '/sbin/ifconfig eth0 | grep bytes'<br />def getnetio(line):<br />s1 = line.find('RX bytes:')+9<br />e1 = line.find(' ', s1)<br />neti = line[s1:e1]<br />s2 = line.find('TX bytes:')+9<br />e2 = line.find(' ', s2)<br />neto = line[s2:e2]<br />return (int(neti), int(neto))</p><p>def int_handler(signum, frame):<br />print ""<br />sys.exit()<br />signal.signal(signal.SIGINT, int_handler)</p><p>line = os.popen(netcmd).readline().strip()<br />netio = getnetio(line)<br />neti_start = netio[0]<br />neto_start = netio[1]<br />time_start = time.time()<br />count = 60<br />while (count > 0):<br />count -= 1<br />time.sleep(1);<br />info = []<br />line = os.popen(netcmd).readline().strip()<br />netio = getnetio(line)<br />info.append("網路流入總量:%.4fm, 網路流出總量:%.4fm" % (netio[0]/1024/1024, netio[1]/1024/1024))<br />time_curr = time.time()<br />neti_total = netio[0]-neti_start<br />neto_total = netio[1]-neto_start<br />sec_total = time_curr-time_start<br />neti_start = netio[0]<br />neto_start = netio[1]<br />time_start = time_curr<br />info.append("當前網路流入速度:%.4fk/s" % (neti_total/sec_total/1024))<br />info.append("當前網路流出速度:%.4fk/s" % (neto_total/sec_total/1024))<br />show = ", ".join(info)<br />sys.stdout.write(show+"/r")<br />sys.stdout.flush()<br />print ""

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.