Python腳本實(shí)現(xiàn)網(wǎng)卡流量監(jiān)控
#/usr/bin/env/python #coding=utf-8 import sys,re,time,os maxdata = 50000 #單位KB memfilename = '/tmp/newnetcardtransdata.txt' netcard = '/proc/net/dev' def checkfile(filename): if os.path.isfile(filename): pass else: f = open(filename, 'w') f.write('0') f.close() def get_net_data(): nc = netcard or '/proc/net/dev' fd = open(nc, "r") netcardstatus = False for line in fd.readlines(): if line.find("eth0") > 0: netcardstatus = True field = line.split() recv = field[0].split(":")[1] recv = recv or field[1] send = field[8] if not netcardstatus: fd.close() print 'Please setup your netcard' sys.exit() fd.close() return (float(recv), float(send)) def monfirst(filename): nowtime = time.strftime('%m-%d %H:%M',time.localtime(time.time())) sec = time.localtime().tm_sec if nowtime == '01-01 00:00': if sec < 10: f = open(filename, 'w') f.write('0') f.close() def net_loop(): (recv, send) = get_net_data() checkfile(memfilename) monfirst(memfilename) lasttransdaraopen = open(memfilename,'r') lasttransdata = lasttransdaraopen.readline() lasttransdaraopen.close() totaltrans = int(lasttransdata) or 0 while True: time.sleep(3) (new_recv, new_send) = get_net_data() recvdata = (new_recv - recv) / 1024 senddata = (new_send - send) / 1024 totaltrans += int(recvdata) totaltrans += int(senddata) memw = open(memfilename,'w') memw.write(str(totaltrans)) memw.close() if totaltrans >= maxdata: os.system('init 0') if __name__ == "__main__": net_loop()
用ROOT權(quán)限運(yùn)行,maxdata為最大流量限制 超過(guò)這個(gè)限制,系統(tǒng)自動(dòng)關(guān)機(jī) 當(dāng)然,你可以改os.system('init 0')為你想要的命令 主要是現(xiàn)在VPS都限制流量,才搞了這個(gè)小腳本
相關(guān)文章
Python輸入正負(fù)10進(jìn)制,轉(zhuǎn)4位16進(jìn)制問(wèn)題
這篇文章主要介紹了Python輸入正負(fù)10進(jìn)制,轉(zhuǎn)4位16進(jìn)制問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-06-06使用瀏覽器訪問(wèn)python寫(xiě)的服務(wù)器程序
這篇文章主要介紹了用瀏覽器訪問(wèn)python寫(xiě)的服務(wù)器程序,本文通過(guò)實(shí)例代碼,需要的朋友可以參考下2019-10-10一道題學(xué)會(huì)Python函數(shù)中形參和實(shí)參
在Python編程中,函數(shù)的形參和實(shí)參是函數(shù)定義和調(diào)用的重要概念,本文主要介紹了一道題學(xué)會(huì)Python函數(shù)中形參和實(shí)參,具有一定的參考價(jià)值,感興趣的可以了解一下2024-01-01Python爬蟲(chóng)實(shí)現(xiàn)簡(jiǎn)單的爬取有道翻譯功能示例
這篇文章主要介紹了Python爬蟲(chóng)實(shí)現(xiàn)簡(jiǎn)單的爬取有道翻譯功能,結(jié)合實(shí)例形式分析了Python基于urllib庫(kù)實(shí)現(xiàn)的爬蟲(chóng)爬取有道翻譯相關(guān)定義與數(shù)據(jù)交互功能操作技巧,需要的朋友可以參考下2018-07-07深入了解Python中反射和動(dòng)態(tài)屬性的無(wú)限可能
理解 Python 中的反射和動(dòng)態(tài)屬性是編寫(xiě)靈活和強(qiáng)大程序的關(guān)鍵,在這篇文章中,小編將帶領(lǐng)大家一起反射和動(dòng)態(tài)屬性的概念,并提供大量示例代碼,希望對(duì)大家有所幫助2023-11-11使用Python獲取網(wǎng)段IP個(gè)數(shù)以及地址清單的方法
今天小編就為大家分享一篇使用Python獲取網(wǎng)段IP個(gè)數(shù)以及地址清單的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-11-11python正則表達(dá)式常見(jiàn)的知識(shí)點(diǎn)匯總
正則表達(dá)式提供了一些可用的匹配模式,比如忽略大小寫(xiě)、多行匹配等,下面這篇文章主要給大家介紹了關(guān)于python正則表達(dá)式常見(jiàn)的知識(shí)點(diǎn),文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-05-05