监视网络流量以关机脚本

sh/bash/dash/ksh/zsh等Shell脚本
回复
头像
naturalaw
帖子: 1360
注册时间: 2011-09-06 22:04

监视网络流量以关机脚本

#1

帖子 naturalaw » 2012-08-11 17:47

脚本功能
下载的时候,如果网速太慢不稳定,就关机。

以5秒钟作为基准,如果5秒内下载量少于400k,累计12次,就关机。
仅以此拙劣脚本抛砖引玉
我的是eth1
且ifconfig eth1 输出格式如下,不同的网卡awk打印的列可能不一样

代码: 全选

eth1      Link encap:Ethernet  HWaddr 00:16:6F:BC:70:87  
          inet addr:192.168.0.102  Bcast:192.168.0.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:49172 errors:0 dropped:2 overruns:0 frame:0
          TX packets:36761 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:54506823 (51.9 Mb)  TX bytes:4429432 (4.2 Mb)
          Interrupt:16 Base address:0x4000 Memory:d0001000-d0001fff 
另外,要在/etc/sudoers 添加一行

代码: 全选

%users ALL=NOPASSWD: /sbin/shutdown -h now
[bash]
#!/bin/bash
result=10240
n=1;
total=400
secs=5;
i=12;
# speed=400/5=80
# In 1min should download no less than 4.6875MB(400*12/1024)

while [ $result -ge $total -o $n -le $i ];do
old=$(($(ifconfig eth1 | awk 'BEGIN{FS="[ \t:(]+"} /bytes/{print $4}') / 1024))
new=$(sleep $secs; echo $(($(ifconfig eth1 | awk 'BEGIN{FS="[ \t:(]+"} /bytes/{print $4}') / 1024)))
result=`expr $new - $old`
[ $result -lt $total ] && echo "off count down: `expr 13 - $n`" && n=`expr $n + 1`
done

sudo shutdown -h now

[/bash]
上次由 naturalaw 在 2012-08-12 2:19,总共编辑 1 次。
  • The eternal law
头像
eexpress
帖子: 58428
注册时间: 2005-08-14 21:55
来自: 长沙

Re: 监视网络流量以关机脚本

#2

帖子 eexpress » 2012-08-11 21:07

routel|grep -m 1 '\<24\>'|sed 'sm/mmg'
试试。

不明白你的脚本干嘛的。没仔细看。
● 鸣学
头像
naturalaw
帖子: 1360
注册时间: 2011-09-06 22:04

Re: 监视网络流量以关机脚本

#3

帖子 naturalaw » 2012-08-12 2:18

eexpress 写了:routel|grep -m 1 '\<24\>'|sed 'sm/mmg'
试试。

不明白你的脚本干嘛的。没仔细看。
下载的时候,如果网速太慢不稳定,就关机。

以5秒钟作为基准,如果5秒内下载量少于400k,累计12次,就关机。
  • The eternal law
头像
lldonger
帖子: 101
注册时间: 2010-10-15 15:42

Re: 监视网络流量以关机脚本

#4

帖子 lldonger » 2012-12-04 12:30

楼主为啥不用tcpdump呢?
回复