shell脚本

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了shell脚本相关的知识,希望对你有一定的参考价值。

12.监控iptables 

cat 12_iptables.sh

#!/bin/bash

#Usage:Monitor the iptables

#Author:chengyanli

#Date:2016/7/15


watch -n 1 ‘/etc/init.d/iptables status‘


14.查看路由 

cat 14_route.sh

#!/bin/bash

#Usage:Judge 10.0.0.0 is one of the route or not

#Author:chengyanli

#Date:/2016/7/20


route -n | grep 10.0.0.0 > /dev/null

if [ $? != 0 ]; then

echo ‘The 10.0.0.0 is not one of the route‘

else

echo ‘The 10.0.0.0 is one of the route‘

fi


17系统负载

 cat 17_load.sh

#!/bin/bash

#Usage:Judge the server load is bigger than 4 or not

#Author:chengyanli

#Date:2016/7/15


IP=`ip addr show|sed ‘8!d‘|awk ‘{print $2}‘|cut -f 1 -d ‘/‘`

[email protected]


#The load of server in 1min,5min,15min

a=`uptime | awk -F ‘:‘ ‘{print $5}‘ |awk -F ‘,‘ ‘{print $1}‘ |awk -F ‘.‘ ‘{print $1}‘`

b=`uptime | awk -F ‘:‘ ‘{print $5}‘ |awk -F ‘,‘ ‘{print $2}‘ |awk -F ‘.‘ ‘{print $1}‘`

c=`uptime | awk -F ‘:‘ ‘{print $5}‘ |awk -F ‘,‘ ‘{print $3}‘ |awk -F ‘.‘ ‘{print $1}‘`

d=4


#Judge server load in 1min is bigger than 4 or not

if [ "$a" -ge "$d" ] ; then

echo -e " ${IP} sever load is bigger than 4 \n\n `date +%Y-%m-%d`" |mail -s ‘WARNING‘ $USER

fi


#Judge server load in 5min is bigger than 4 or not

if [ "$b" -ge "$d" ] ; then

echo -e " ${IP} sever load is bigger than 4 \n\n `date +%Y-%m-%d`" |mail -s ‘WARNING‘ $USER

fi


#Judge server load in 15min is bigger than 4 or not

if [ "$c" -ge "$d" ] ; then

echo -e " ${IP} sever load is bigger than 4 \n\n `date +%Y-%m-%d`" |mail -s ‘WARNING‘ $USER

fi



18.批量查看home分区使用

 cat 18_home.sh

#!/bin/bash

#Usage:count the use of /home

#Author:chengyanli

#Date:2016/07/25


#This is a function to achieve auto_ssh_login

auto_ssh_copy_id(){

expect -c "set timeout -1;

spawn /usr/bin/ssh-copy-id -i /root/.ssh/id_rsa.pub [email protected]$1;

expect {

*(yes/no)* {send -- yes\r;exp_continue;}

*password:* {send -- $2\r;exp_continue;}

eof {exit 0;}

}";

}


#login remote host and count the use of home

while read line

do

ip=`echo $line | awk ‘{print $1}‘`

pass=`echo $line | awk ‘{print $2}‘`

auto_ssh_copy_id ${ip} ${pass} >/dev/null 2>&1

sleep 1

home_used=`ssh -n $ip ‘df -h|grep home‘`

echo ${ip} home used: $home_used

echo

done < /mnt/ip_list



19查看syslog是否正常工作

 cat 19_syslog.sh

#!/bin/bash

#Usage:monitor the syslog with error

#Author:chengyanli

#Date:2016/7/15


IP=`ip addr show|sed ‘8!d‘|awk ‘{print $2}‘|cut -f 1 -d ‘/‘`

[email protected]


#if error messages,give an alarm

cat /var/log/messages |grep error >/dev/null 2>&1

if [ $? = 0 ];then

echo -e "Notice:${IP} error messages \n\n `date +%Y-%m-%d`"

else

echo ‘no error messages‘

fi



20.非法ip入侵

 cat 20_ip.sh

#!/bin/bash

#Usage:ip unusual

#Author:chengyanli

#Date:2016/7/20

#The history ip

myarray=`last|grep pts|awk ‘{print $3}‘|sort|uniq`

#A module to check unusual ip

for i in ${myarray}

do

cat /etc/hosts.allow|grep ${i}

if [ ! $? -eq 0 ]

then

echo "Notice:unusual ip:${i} in"

fi

done


本文出自 “真水无香” 博客,请务必保留此出处http://chengyanli.blog.51cto.com/11399167/1846785

以上是关于shell脚本的主要内容,如果未能解决你的问题,请参考以下文章

Shell脚本--变量(后附简单shell脚本案例)!

shell脚本翻译 急求

shell脚本书写方法

如何在shell脚本里调用另一个shell脚本

shell脚本获取参数&在线执行shell脚本

shell脚本中#是啥意思