国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

Python腳本實現網卡流量監控

2020-02-23 06:24:38
字體:
來源:轉載
供稿:網友
#/usr/bin/env/python#coding=utf-8import sys,re,time,osmaxdata = 50000 #單位KBmemfilename = '/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都限制流量,才搞了這個小腳本

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 蓬安县| 岱山县| 石景山区| 德兴市| 阜南县| 蒙山县| 鹤庆县| 石门县| 光泽县| 大埔区| 新密市| 四川省| 教育| 隆林| 云和县| 红桥区| 竹山县| 阿城市| 体育| 黄石市| 朝阳市| 大埔区| 内乡县| 金塔县| 浦江县| 兴安县| 辽阳市| 浠水县| 长沙县| 定安县| 平凉市| 鸡泽县| 洪洞县| 武川县| 涟水县| 米脂县| 长宁区| 稻城县| 谢通门县| 漳州市| 大悟县|