码迷,mamicode.com
首页 > 系统相关 > 详细

Shell程序实例集锦一

时间:2015-02-03 10:45:48      阅读:264      评论:0      收藏:0      [点我收藏+]

标签:

2007-12-13 07:51:40
Shell程序实例集锦一
 
 
 前言:下面这些hell实例都是自己写的或者用过的一些Shell小程序。整理整理。    ——Ajian

1、删除B文件中和A文件相同的内容
#!/bin/sh
# Ajian
for file in `cat a.list | cut -d. -f1`
do
sed -i ‘/‘$file‘/d‘ b.list
done
 

2、根据文件名的前四位创建二级目录
#!/bin/sh
#Ajian
for dir in `cat dir.list`
do
dir1=`echo $dir | cut -c1-2`
dir2=`echo $dir | cut -c3-4`
if [ ! -d "$dir1/$dir2" ]; then
       mkdir -p "$dir1/$dir2"
fi
done


3、查看网卡流量
#!/bin/bash
#netflood
#Ajian
while : ; do
       time=`date +%m"-"%d" "%k":"%M`
       day=`date +%m"-"%d`
       rx_before=`ifconfig eth0|sed -n "8"p|awk ‘{print $2}‘|cut -c7-`
       tx_before=`ifconfig eth0|sed -n "8"p|awk ‘{print $6}‘|cut -c7-`
       sleep 2
       rx_after=`ifconfig eth0|sed -n "8"p|awk ‘{print $2}‘|cut -c7-`
       tx_after=`ifconfig eth0|sed -n "8"p|awk ‘{print $6}‘|cut -c7-`
       rx_result=$[(rx_after-rx_before)/256]
       tx_result=$[(tx_after-tx_before)/256]
       echo "$time Now_In_Speed: "$rx_result"kbps Now_OUt_Speed: "$tx_result"kbps"
       sleep 2
done


4、系统状况监控
#!/bin/sh
#statistic.sh
#Ajian
IP=192.1681.41
top -n 2| grep "Cpu" >>./data/cpu.txt
free -m | grep "Mem" >> ./data/mem.txt
df -k | grep "sda1" >> ./data/drive_sda1.txt
#df -k | grep sda2 >> ./data/drive_sda2.txt
df -k | grep "/mnt/storage_0" >> ./data/mnt_storage_0.txt
df -k | grep "/mnt/storage_pic" >> ./data/mnt_storage_pic.txt
time=`date +%m"."%d" "%k":"%M`
connect=`netstat -na | grep "172.16.20.5:80" | wc -l`
echo "$time  $connect" >> ./data/connect_count.txt


5、系统服务及系统状况分析

#!/bin/sh
#server
#################################
#modify by ajian
#function: It is convenient to restart some servers and reduce the wasted time.
#DATE:  2007-7-2
#################################
#路径配置区
APACHEPATH=""
SMB_LOCAL_PATH1=""
SMB_REMOTE_PATH1=""
SMB_LOCAL_PATH2=""
SMB_REMOTE_PATH2=""
#################################
#应用配置区
SMBUSER=""
SMBPASS=""
SMB_UID=
SMB_GID=
#################################
case $1 in
       "apache")
               if [ $2 = "start" ]
               then
                       echo "starting apache ..."
                       $APACHEPATH/apachectl start
                       echo "apache is started."
                       pidof httpd
               elif [ $2 = "stop" ]
               then
                       echo "stopping apache ..."
                       $APACHEPATH/apachectl stop
                       echo "apache is stopped."
               elif [ $2 = "restart" ]
               then
                       echo "restarting apache ..."
                       $APACHEPATH/apachectl stop
                       sleep 5
                       $APACHEPATH/apachectl start
                       echo "apache is restarted."
                       pidof httpd
               else
                       echo "apache‘s $2 is error."
               fi
               ;;
       "smb")
               if [ $2 = "remount" ]
               then
                       echo "remounting smbfs..."
                       umount -f $SMB_LOCAL_PATH1
                       umount -f $SMB_LOCAL_PATH2
           echo "umount is finished."
                       sleep 5
                       mount -t smbfs -o username=$SMBUSER,password=$SMBPASS,ui
d=$SMB_UID,gid=$SMB_GID $SMB_REMOTE_PATH1 $SMB_LOCAL_PATH1
                       mount -t smbfs -o username=$SMBUSER,password=$SMBPASS,ui
d=$SMB_UID,gid=$SMB_GID $SMB_REMOTE_PATH2 $SMB_LOCAL_PATH2
                       echo "mount is finished."
                       df -h
               elif [ $2 = "mount" ]
               then
                       mount -t smbfs -o username=$SMBUSER,password=$SMBPASS,ui
d=$SMB_UID,gid=$SMB_GID $SMB_REMOTE_PATH1 $SMB_LOCAL_PATH1
                       mount -t smbfs -o username=$SMBUSER,password=$SMBPASS,ui
d=$SMB_UID,gid=$SMB_GID $SMB_REMOTE_PATH2 $SMB_LOCAL_PATH2
                       echo "mount is finished."
                       df -h
               else
                       echo "smb‘s $2 is error."
               fi
               ;;
       "all")
               echo "restart apache and remount..."
               $APACHEPATH/apachectl stop
       sleep 5
               umount -f $SMB_LOCAL_PATH1
               umount -f $SMB_LOCAL_PATH2
               sleep 5
               mount -t smbfs -o username=$SMBUSER,password=$SMBPASS,uid=$SMB_U
ID,gid=$SMB_GID $SMB_REMOTE_PATH1 $SMB_LOCAL_PATH1
               mount -t smbfs -o username=$SMBUSER,password=$SMBPASS,uid=$SMB_U
ID,gid=$SMB_GID $SMB_REMOTE_PATH2 $SMB_LOCAL_PATH2
               echo "mount is finished."
               df -h
               $APACHEPATH/apachectl start
               echo "apache is restarted."
               sleep 5
               pidof httpd
               ;;
   "status")
       if [ $2 = "grather" ]
       then
           echo "Gather the status of the computer ....."
           /movivi/status.sh
           echo "Gather is finished."
       elif [ $2 = "analyze"  ]
       then
           echo "analyze the status.ch ......"
           /movivi/analyze.sh
           cat /movivi/data/sumary.txt
       fi
       ;;
       *)
               echo "The method of using the script."
               echo ""
       echo "  server apache stop      means:  关闭apache."
               echo "  server apache start     means:  启动apache."                
               echo "  server apache restart   means:  重启apache."
               echo "  server remount            means:  关闭Mount点再挂载."
       echo "  server mount              means:  直接挂载Mount点."
               echo "  server all                    means:  Include all of above."
               echo "  server status gather     means:  收集系统信息."
       echo "  server status analyze    means:  分析处理并显示系统信息."
               echo "  "
               echo ""
               ;;
esac


6、服务器采样程序(主要用于邮件发送报警 格式问题)
#!/bin/sh
# Create By : Ajian
# file: watch.sh
# Time: 2007-10-31
############################
INPUT=/root/message
TIME=`date +%m"."%d" "%k":"%M`
echo "###################################################################################################" >> $INPUT
echo "###################################################################################################" >> $INPUT
echo "采样时间点: $TIME" >> $INPUT
echo "////////////////////////////////////" >>  $INPUT
echo "CPU使用情况:" >> $INPUT
top -n 3 -b  | grep "Cpu" | awk ‘{print $1 $2 $3 $4 $5}‘>> $INPUT
echo "////////////////////////////////////" >>  $INPUT
echo "内存使用情况:(单位M)" >> $INPUT
free -m >> $INPUT
echo "////////////////////////////////////" >>  $INPUT
echo "服务器连接数:" >> $INPUT
echo "Web连接     :`netstat -na | grep ":80" | wc -l`" >> $INPUT
echo "TIMEWAIT连接:`netstat -na | grep ":80" | grep "TIME_WAIT" | wc -l`" >> $INPUT
echo "数据库连接:`netstat -na | grep "3306" | wc -l`" >> $INPUT
echo "////////////////////////////////////" >>  $INPUT
echo "Apache 进程数:`ps aux | grep httpd | wc -l`" >> $INPUT
echo "注:采样十分钟取一次,一小时发送一次" >> $INPUT

Shell程序实例集锦一

标签:

原文地址:http://www.cnblogs.com/hanxing/p/4269173.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!