仅以此拙劣脚本抛砖引玉下载的时候,如果网速太慢不稳定,就关机。
以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
代码: 全选
%users ALL=NOPASSWD: /sbin/shutdown -h now
#!/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]