Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1654465
  • 博文数量: 82
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 0
  • 用 户 组: 普通用户
  • 注册时间: 2017-12-09 12:58
文章分类

全部博文(82)

文章存档

2011年(7)

2010年(3)

2009年(11)

2008年(4)

2007年(57)

我的朋友

分类:

2011-10-13 13:48:12

最近些了一些脚本,在blog上晒下,写的比较菜,希望大家批评指正。
1 ssh自动启动脚本。
#!/bin/bash
# By Nick.Ma
# The script is monitor PID with many services of gather.



DIR=$PWD
DATE=`date +%y-%m-%d_%H:%M:%S`
SERV="sshd"
LOG_DIR=/root

source $DIR/import.sh

for SERV in $SERV
  do
    netstat -tnlp | grep "$SERV" > /dev/null
    if [ $? -ne "0" ] ;then
        if [ -f /etc/init.d/sshd ];then
           /etc/init.d/sshd start
           echo -e " $DATE\t$SERV is running... Log save $LOG_DIR/$SERV.log" >> $LOG_DIR/$SERV.log
        else
           /etc/init.d/ssh start
           echo -e " $DATE\t$SERV is running... Log save $LOG_DIR/$SERV.log" >> $LOG_DIR/$SERV.log
        fi
    fi
done

2 icmp探测健康范围主机。
#!/bin/bash
# By Nick.Ma
# The script is test networking connect with many hosts.

DIR=${PWD}
startIP="37"
endIP="38"
NETWORK="192.168.1."
COUNT=1
LOG="connect_state.log"
source $DIR/import.sh

echo  -e " ############################# Test Start ##############################\n"
echo  -e  "\t\t\tTest progrom running....... \n"
echo  -e " ############################# Test End ################################\n"

for IP in $( seq $startIP $endIP )
  do
     MAX=`ping -c $COUNT $NETWORK$IP | grep \
        "received" | awk '{
                           print $4
                         }'`
     if [ $MAX -eq 0 ] ;then
      echo -e "$NETWORK$IP  :  false\n" >> $DIR/$LOG
     else
      echo -e "$NETWORK$IP  :  true\n" >> $DIR/$LOG
     fi
done && read -p  "Please Enter anykey.... "

3 收集网络接口流量。
#!/bin/bash
# By Nick.Ma
# The script is gather to interface bond flow.

DIR=$PWD
source $DIR/import.sh

typeset in in_old dif_in dif_in1 dif_out1
typeset out out_old dif_out
read -p "Please enter interface name , Example:eth0  " interface
in_old=$(cat /proc/net/dev | grep ${interface} | sed 's=^.*:==' | awk '{ print $1 }' )
out_old=$(cat /proc/net/dev | grep ${interface} | sed 's=^.*:==' | awk '{ print $9 }')
while true
do
sleep 1
in=$(cat /proc/net/dev | grep ${interface} | sed 's=^.*:==' | awk '{ print $1 }')
out=$(cat /proc/net/dev | grep ${interface} | sed 's=^.*:==' | awk '{ print $9 }')
dif_in=$((in-in_old))
dif_in1=$((dif_in * 8 / 1024 / 1024 ))
dif_out=$((out-out_old))
echo -e "IN: ${dif_in} bytes\t\t OUT: ${dif_out} bytes "
echo -e "IN: ${dif_in} bytes\t\t OUT: ${dif_out} bytes " | cat >> ./gatherflow.log
dif_out1=$((dif_out * 8 / 1024 / 1024 ))
echo -e "IN: ${dif_in1} mbps\t\t OUT: ${dif_out1} mbps"
echo -e "IN: ${dif_in1} mbps\t\t OUT: ${dif_out1} mbps" | cat >> ./gatherflow.log
in_old=${in}
out_old=${out}
trap "echo Finish gather up interface && kill -9 $$" 2
done

4 作简单备份。
#!/bin/bash
# The program for backup *.pp files with modules or node info before SVN system finish.


# echo -e "Backup start....."
Main_dir=/etc/puppet
Manifests_dir=$Main_dir/manifests
Mod_dir=$Main_dir/modules
Node_dir=$Main_dir/nodes
Bak_dir="/home/cloud/puppet/bak_dir"
Config_file=""
Date=`date "+%y-%m-%d.%H_%M"`
File_type="tar.gz"

for i in $Mod_dir $Manifests_dir
  do
     if [ ! -d $Bak_dir ];then
           mkdir $Bak_dir
           cd $Bak_dir
           tar zcvf `echo "$i" | sed 's/\//\_/g'`_$Date.$File_type -c $i
        else
           cd $Bak_dir
           tar zcvf `echo "$i" | sed 's/\//\_/g'`_$Date.$File_type -c $i
     fi
done

Hour=`date +%k`
if [ $Hour -eq "24" ];then
        find $Bak_dir -mtime 1 -exec rm -rf {} \;
fi

5 记录接口网络流量
#!/bin/bash
# By Nick.Ma
# The script is gather up interface bond flow.

function netspeed () {
INTERFACE="$1"
echo "$INTERFACE" | awk '
                {
                print $1" Receive\t\t",$1" Transmit\t\t","Time\n","====================================================================================="
                }'

while true
do
   RX=`cat /sys/class/net/$1/statistics/rx_bytes`
   TX=`cat /sys/class/net/$1/statistics/tx_bytes`
     sleep 2
   RX1=`cat /sys/class/net/$1/statistics/rx_bytes`
   TX1=`cat /sys/class/net/$1/statistics/tx_bytes`
   RX_speed=`expr $RX1 - $RX`
   TX_speed=`expr $TX1 - $TX`

   RX_Speed=`echo "$RX_speed" | awk '{print $1/1024"Kb/s"}'`
   TX_Speed=`echo "$TX_speed" | awk '{print $1/1024"Kb/s"}'`
   DATE=`date -R | awk '{print $5}' `
     echo -e "$RX_Speed\t          $TX_Speed\t        $DATE"
     trap "echo Oh yeah ! The Program is over ." EXIT
done
}

if [ -z $1 ] ; then
  echo -e "\n\tSorry ! $0 Usage: command running error.\n\tExec command Example: $0 eth0\n"
else
  netspeed $1 | tee ./netspeed.log
fi



阅读(586) | 评论(0) | 转发(0) |
0

上一篇:puppet installed

下一篇:轻量级DDOS防护

给主人留下些什么吧!~~