#!/usr/bin/lua local socket = require("socket") function sleep(n) local sleep = socket.select(nil, nil, n) return sleep end local net = {"eth0", "em1"} local name = os.tmpname() os.execute([[ifconfig | head -1 | cut -d‘ ‘ -f1 >]]..name) local readName = io.open(name, "r") local realName = readName:read("*l") for _, var in ipairs(net) do if var == realName then file1 = io.open("/sys/class/net/em1/statistics/tx_bytes","r") file2 = io.open("/sys/class/net/em1/statistics/tx_bytes","r") break else file1 = io.open("/sys/class/net/eth0/statistics/tx_bytes","r") file2 = io.open("/sys/class/net/eth0/statistics/tx_bytes","r") end end local tx1 = file1:read("*l") sleep(1) local tx2 = file2:read("*l") local mb = (tx2-tx1)*8/1024/1024 print(math.ceil(mb)) file1:close() file2:close() readName:close() os.remove(name)
本文出自 “Art_Hero” 博客,请务必保留此出处http://curran.blog.51cto.com/2788306/1392498
原文:http://curran.blog.51cto.com/2788306/1392498