2014-09-29shell脚本练习题

回复 收藏
首先说声抱歉,因为这几天公司安排出差,所以课耽误一次,脚本练习题也是好几天没有给大家出。今天回来了,首先补上练习题。

写一个脚本,检测你的网络流量,并记录到一个日志里。需要按照如下格式,并且一分钟统计一次:
2014-09-29 10:11
eth0 input: 1000bps
eth0 output : 200000bps
################
2014-09-29 10:12
eth0 input: 1000bps
eth0 output : 200000bps


提示:使用sar -n DEV  1 59 这样可以统计一分钟的平均网卡流量,只需要最后面的平均值。另外,注意换算一下,1byt=8bit
2014-09-29 15:48 举报
已邀请:
0

wyatt88

赞同来自:

本帖最后由 wyatt88 于 2014-9-30 11:23 编辑

来抢沙发,if条件因为出网卡流量过小,所以增加了判断{:4_111:},只支持单网卡
  1. #!/bin/bash
  2. #trafic.sh
  3. #author:ww
  4. #avg trafic in per min

  5. filename=/tmp/trafic.log

  6. while :;
  7. do
  8.   trac_info=`sar -n DEV 1 59 | tail -n 1`
  9.   echo $trac_info
  10.   in_trac_kb=`echo $trac_info | awk '{print $5}' | cut -d '.' -f1`
  11.   out_trac_kb=`echo $trac_info | awk '{print $6}' | cut -d '.' -f1`
  12.   nowtime=`date +%F" "%H:%M`
  13.   in_bit=`expr $in_trac_kb "*" 1000 "*" 8`
  14.   out_bit=`expr $out_trac_kb "*" 1000 "*" 8`
  15.   if [ $out_trac_kb -eq 0 ]
  16.   then
  17.        out_trac_kb=`echo $trac_info | awk '{print $6}' | cut -d '.' -f2`
  18.        case `echo $out_trac_kb | wc -c` in
  19.        2)
  20.        out_bit=`expr $out_trac_kb "*" 100 "*" 8`
  21.        ;;
  22.        3)
  23.        out_bit=`expr $out_trac_kb "*" 10 "*" 8`
  24.        ;;
  25.        *)
  26.        echo "error"
  27.        ;;
  28.        esac
  29.   fi
  30.   echo $nowtime >> $filename
  31.   echo "eth0 input: "$in_bit"bps" >> $filename
  32.   echo "eth0 output: "$out_bit"bps" >> $filename
  33.   echo "################" >> $filename
  34.   
  35. # sleep 60
  36. done
0

So Long

赞同来自:

先占个位置,哈哈
0

ocean

赞同来自:

  1. [root@www sh]# cat 0929.sh
  2. #!/bin/bash
  3. # Desc : this script is of network traffic statistics every minutes
  4. # Writen by Jeffery.Su
  5. # Date:  Sep,30 2014

  6. DATE=`date +"%Y-%m-%d %H:%M"`
  7. LOG_PATH=/tmp/traffic_check/`date +%Y%m`
  8. LOG_FILE=$LOG_PATH/traffic_check_`date +%d`.log


  9. [ -d $LOG_PATH] || mkdir -p $LOG_PATH

  10. echo " $DATE" >> $LOG_FILE

  11. sar -n DEV 1 59|grep Average|grep -v "IFACE\|lo"|awk '{print "\n",$2,"\t","input:",$5*1000*8,"bps","\t","\n",$2,"\t","output:",$6*1000*8,"bps" }' >> $LOG_FILE

  12. echo -e "#####################\n" >> $LOG_FILE

  13. echo "*/1 * * * * /bin/bash /usr/local/sbin/sh/0929.sh" >>/var/spool/cron/root


0

齐天大圣

赞同来自:

本帖最后由 齐天大圣 于 2014-9-30 12:13 编辑
  1. #!/bin/bash
  2. #Writen by lichao.V1 2014-09-30

  3. while :;
  4. do
  5.   d=`date +%F\ %H:%M`
  6.   sar=/tmp/sar.txt
  7.   sar -n DEV 1 59 >$sar
  8.   input=`grep 'Average' $sar|awk '$2~/eth0/{print $5}'|awk -F'.' '{print $1}'`
  9.   output=`grep 'Average' $sar|awk '$2~/eth0/{print $6}'|awk -F'.' '{print $1}'`
  10.   echo $d>>sar.log
  11.   echo "eth0 input:`expr $input \* 8 \* 1000`bps">>sar.log
  12.   echo "eth0 output:`expr $output \* 8 \* 1000`bps">>sar.log
  13.   echo "####################">>sar.log
  14. done


0

Budweiser小王子

赞同来自:

看答案来了
0

游夜

赞同来自:

本帖最后由 游夜 于 2014-10-2 04:21 编辑
  1. #!/bin/bash
  2. touch networkflow.log
  3. while : ;do
  4. data +%F' '%H:%m>>networkflow.log
  5. sar -n DEV 1 59 |awk '/Average/&&/eth*/{print $2" input:" $5*8*1024"bps";print$2" output:" $6*8*1024"bps"}'>>networkflow.log
  6. echo "################">>networkflow.log
  7. done
0

陈沛

赞同来自:

本帖最后由 陈沛 于 2014-10-4 21:56 编辑
  1. #/bin/bash
  2. # 2014-09-29
  3. # chenpei
  4. # Average network traffic

  5. # log file
  6. log_file="/tmp/traffic.log"
  7. while : ; do
  8.    average=` sar -n DEV  1 59 | grep eth0 | sed -n '60'p`
  9.    date "+%F %T" >> $log_file
  10.    input_average=`echo $average | awk '{print $5}'`
  11.    output_average=`echo $average | awk '{print $6}'`
  12.    echo "eth0 input: " `echo "${input_average}*8" | bc` >> $log_file
  13.    echo "eth0 output: " `echo "${output_average}*8" | bc`  >> $log_file
  14.    echo "#######################################################"   >> $log_file
  15. done
0

Louis

赞同来自:

  1. #!/bin/bash
  2. ## This script is for count network adapter traffic in a regular format every minute.
  3. ## Writed by Louis at 2014/10/01 15:35

  4. log=/tmp/netcount.log
  5. while :; do
  6.     echo `date +'%F %T'` >> $log
  7.     sar -n DEV 1 60 > /tmp/nettraffic.log
  8.     receive_packets=`grep 'Average' /tmp/nettraffic.log|grep 'eth0'|awk '{print $5}'`
  9.     transmit_packets=`grep 'Average' /tmp/nettraffic.log|grep 'eth0'|awk '{print $6}'`
  10.     echo "eth0 input: "`echo "$receive_packets"*8|bc`"Kbps" >> $log
  11.     echo "eth0 output: "`echo "$transmit_packets"*8|bc`"Kbps" >> $log
  12.     echo "###########################" >> $log
  13. done


0

cmzsteven

赞同来自:

#!/bin/bash

while :;do
    average=`sar -n DEV 1 60 | tail -n 1`
    tx=`echo $average |awk '{print $6}'`
    rx=`echo $average |awk '{print $5}'`
    date=`date +"%F %T"`
    echo $date >> net.log
    echo "eth0 input: $rx kB/s">>net.log
    echo "eth0 ouput: $tx kB/s">>net.log
    echo "##########################################">>net.log
done
0

hangtiangazi

赞同来自:


应该用追加重定向吧。而不是重写重定向吧
0

hangtiangazi

赞同来自:

a
  1. #!/bin/bash
  2. ## test by gxw
  3. date=`date +"%F %T"
  4. log=/tmp/sar/log
  5. daytime=`date +%F %T`
  6. input=`sar -n DEV |grep "average" | grep "eth0" |awk '{print $5}'`
  7. output=`sar -n DEV |grep "average" | grep "eth0" |awk '{print $6}'`
  8. input1=$[$input*8*1000]
  9. output2=$[$output*8*1000]
  10. echo "$date">$log
  11. echo "etho input : $input1">$log
  12. echo "etho output :$output2">$log
  13. echo "#########################">4log或者是上边的的市局合成一句
  14. echo -e "date\n etho input :$input\n
  15. etho output :$output \n #############\n">4.log
  16. 计划任务:
  17. */1 * * * * /bin/bash /root/shell/sar.sh


0

翟厚翔

赞同来自:

#!/bin/bash
while :; do
ADATE=`date +%F_%H:%M|sed -r  's#_# #g'`
AVGE=`sar -n DEV 1 59|grep 'Average'|grep 'eth0'`
AVGER=`echo "$AVGE"|awk -F' ' '{print $5}'|awk -F'.' '{print $1}'`
AVGET=`echo "$AVGE"|awk -F' ' '{print $6}'|awk -F'.' '{print $1}'`
echo "$ADATE" >> 1.log
let AVGER1=AVGER*8
let AVGET1=AVGET*8
echo "eth0 input: $AVGER1 bps" >> 1.log
echo "ech0 output: $AVGET1 bps" >> 1.log
echo "#####################"  >> 1.log
done
0

shc1985

赞同来自:

kanxia daan
0

kevinjin

赞同来自:

#! /bin/bash
S=`date +%S`
date=`date +"%Y-%m-%d %H:%M"`
in=`sar -n DEV 1 1 |grep 'Average' |grep 'eth0' |awk '{print $5}'`
out=`sar -n DEV 1 1 |grep 'Average' |grep 'eth0' |awk '{print $6}'`
inbit=[$in]*8000
outbit=[$out]*8000

while [ $S==0 ]
do
    echo "$date" >> traffic.log
    echo "eth0 input: $inbit bps" >> traffic.log
    echo "eth0 output: $output bps" >> traffic.log
    echo "################" >> traffic.log
done
0

hhao

赞同来自:

1
0

riverxyz

赞同来自:

#!/bin/bash
while :;
do
d=`date +%F""%H%M`
rx=`sar -n DEV  1 10|grep eth0|tail -1 |awk -F " " '{print $5*1000*8}'`

tx=`sar -n DEV  1 10|grep eth0|tail -1 |awk -F " " '{print $6*1000*8}'`

echo $d >> /tmp/eth0.log
echo "eth0 input:$rx" >> /tmp/eth0.log
echo "eth0 output:$tx" >> /tmp/eth0.log
sleep 60
done
~
0

linuxcp

赞同来自:

HTRH
0

linuxcp

赞同来自:

HTRHTR
0

kw是id

赞同来自:

#!/bin/bash
d=`date +"%F %H:%M"`
file1=/tmp/flow.txt
file2=/tmp/eth0.txt
while :
do
   /usr/bin/sar -n DEV 1 59 |tail -n1 |awk '{print $5,$6}' >>$file1
   input=`/usr/bin/tail -n1 $file1 |awk '{print $1}'`
   output=`/usr/bin/tail -n1 $file1 |awk '{print $2}'`
   echo $d >>$file2
   echo "eth0 input:$input" >>$file2
   echo "eth0 output:$output" >>$file2
   echo "#######################" >>$file2
done

0

大雁

赞同来自:

 d=`date +%F" "%H:%M`

 sar -n DEV 1 10 |tail -n 5|grep eth0 > th.txt

 inp=`awk '{print $5}' th.txt`

 outp=`awk '{print $6}' th.txt`

 i=`echo "$inp*1024*8"|bc`

 o=`echo "$outp*1024*8"|bc`

 echo "#####################"

 echo "$d"

 echo "eth0 input: $i bps"

 echo "eth0 output: $o bps"

回复帖子,请先登录注册

退出全屏模式 全屏模式 回复
评分
可选评分理由: