#/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權限運行,maxdata為最大流量限制 超過這個限制,系統自動關機 當然,你可以改os.system('init 0')為你想要的命令 主要是現在VPS都限制流量,才搞了這個小腳本
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061
微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元

