shell脚本

Posted

tags:

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

21.新增端口监控 

cat 21_port.sh

#!/bin/bash

#Usage:Judge new port

#Author:chengyanli

#Date:2016/7/20


#The history port

if [ ! -f port ]

then

netstat -antulp|awk ‘{print $4}‘|awk -F ‘:‘ ‘{print $NF}‘|sed ‘1,2d;/^$/d‘ |sort|uniq > ./port

fi


#The port at now

myarray=`netstat -antulp|awk ‘{print $4}‘|awk -F ‘:‘ ‘{print $NF}‘|sed ‘1,2d;/^$/d‘|sort|uniq`


for item in ${myarray}

do

cat ./port|grep ${item} > /dev/null

if [ ! $? -eq 0 ]

then

echo "A new port:${item} appear"

else

echo ‘no new port appear‘

break

fi

done



22.网卡版本查看 

cat 22_interface.sh

#!/bin/bash

#Usage:check the interface

#Author:chengyani

#Date:2016/7/20


watch -n 1 "modinfo bnx2|grep version|sed ‘1!d‘"


23.测试主机连通性

 cat 23.24_connect.sh

#!/bin/bash

#Usage:check the connect between host

#Author:chengyanli

#Date:2016/07/22


read -p ‘plz input a ip:‘ IP

nc -z -w 5 $IP 22 2&>/dev/null

if [ $? = 0 ]; then

echo ‘The connect of the host is ok‘

fi



25.查看磁盘io 

cat 25_io.sh

#!/bin/bash

#Usage:monitor the io of disk

#Author:chengyanli

#Date:2016/7/15


#The local ip,device and mail

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

Device=`iostat -d|sed ‘4!d‘|awk ‘{print $1}‘`

[email protected]


#The total_io,total_int and high_int

total_int=`iostat -d|sed ‘4!d‘|awk ‘{print $2}‘|awk -F ‘.‘ ‘{print $1}‘`

high_int=20


#Judge the total_io bigger than high_int and give an alarm

if [ ${total_int} -ge ${high_int} ]; then

echo "${IP} ${Device} total_io:${total_int}>=${high_int},plz do something"|mail -s ‘WARNING‘ ${USER}

fi


#The monitor

while true

do

iostat -d|sed ‘3!d‘

iostat -d|sed ‘4!d‘

echo

sleep 2

done



26.网卡流量监控

 cat 26_IFflow.sh

#!/bin/bash

#Usage:monitor the flow of interface

#Author:chengyanli

#Date:2016/7/15


clear


#The active interface,local ip and mail

IF=`ifconfig -a|grep eth1|awk ‘{print $1}‘`

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

[email protected]


#The total tx,total rx and high num

total_rate=`vnstat|sed ‘5!d‘|awk ‘{print $8}‘|awk -F ‘.‘ ‘{print $1}‘`

high_rate=200


#Judge total rate > high rate,if so,give an alarm

if [ ${total_rate} -ge ${high_rate} ];then

echo "${IP} ${IF} total_rate:${total_rate}>=${high_rate},plz do something"|mail -s ‘WARNING‘ ${USER}

fi


#The tx_rate rx_rate and total_rate

flow_rate=`vnstat|sed ‘5!d‘`

##The monitor

while true

do

echo ${flow_rate}

sleep 1

done


27.查看crontab是否正常工作

cat 31_cron.sh

#!/bin/bash


#Ssh remote host and update user passwd

file=`find / -name $user.sh$`

for ip in `cat /mnt/ip_only`

do

echo "00 03 * * */1 ssh ${ip} ‘sh ${file}‘" > /var/spool/cron

done



28.修改用密码

cat 31_user.sh

#!/bin/bash

#Usage:update user passwd

#Author

#Date:2016/07/30


passwd=`mkpasswd -l 10`

mkdir /md5 >/dev/null 2>&1

md5sum /etc/shadow > /md5/shadow.md5

pass(){

md5sum -c /md5/shadow.md5 >/dev/null 2>&1

if [ $? = 0 ];then

echo "${passwd}"|passwd --stdin tom >/dev/null 2>&1

if [ $? = 0 ];then

echo ‘passwd updated successfully‘

else

echo ‘passwd updated failed‘

fi

fi

}



29.查看系统信息 

cat 34_sys.sh

#!/bin/bash

#Usage:HW Information

#Author:chengyanli

#Date:2016/7/20


#This is about cpu type

echo ‘The cpu type:‘

cat /proc/cpuinfo|grep name|awk ‘{print $5}‘

echo


#This is about mem type

echo ‘The mem type:‘

sudo dmidecode -t memory|type

echo


#This is about mem use

echo ‘The mem use:‘

free -m

echo


#This is about SN

echo ‘The SN:‘

dmidecode |grep -i serial|sed ‘4!d‘

echo


#This is about inet addr

echo ‘The inet addr:‘

ifconfig|grep ‘inet addr‘|sed ‘1!d‘|awk ‘{print $1,$2}‘

echo


#This is about mac addr

echo ‘The mac addr:‘

ifconfig|sed ‘1!d‘|awk ‘{print $4,$5}‘

echo




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

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

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

shell脚本翻译 急求

shell脚本书写方法

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

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

shell脚本中#是啥意思