平常写的一些shell小脚本,主要是自己练习或者突发奇想写的。
1.显示出每一用户的shell
#!/bin/bash
File=/etc/passwd
UserCount=$(cat ${File} | wc -l)
for i in `seq 1 ${UserCount}`
do
# echo $i
User=$(cat ${File} | head -n $i | tail -n -1 | cut -d : -f 1)
Sh=$(cat ${File} | head -n $i | tail -n -1 | cut -d : -f 7)
echo "User:" ${User} " Shell:" ${Sh}
done
2.检查某个用户是否登入,使用until
#!/bin/bash
until who | grep $1 &> /dev/null
do
sleep 3
echo "$1 not login in"
done
echo "$1 login"
3.shell批量添加删除用户,用户格式可以不统一
#!/bin/bash
for I in `seq 1 $#`
do
if [ $# -gt 0 ]
then
case $1 in
-v|--verbose)
echo "---v"
shift
;;
-h|--help)
echo "`basename $0` -v|--verbose --add userlist , --del userlist ,"
shift
;;
--add)
tmp=$2
for user in `echo $tmp | sed 's#,# #g'`
do
echo ${user}
done
shift 2
;;
--del)
tmp=$2
for user in `echo $tmp | sed 's#,# #g'`
do
echo ${user}
done
shift 2
;;
*)
echo "`basename $0` -v|--verbose --add userl ist , --del userlist ,"
;;
esac
fi
done
4.接收选择,查看信息(利用while和case,cat)
#!/bin/bash
cat << EOF
d|D)Show disk usages
m|M)Show memory usages
s|S)Show swap usages
*)Quit
EOF
read -p "Input your choice " choice
while [ ${choice} != "quit" ]
do
# echo "test"
case ${choice} in
d|D)
df -hP
;;
m|M)
free -h | grep -E "Mem|total"
;;
s|S)
free -h | grep -E "Swap|total"
;;
q|Q)
exit
;;
*)
echo "Usage: D M S Q "
esac
read -p "Input your choice again " choice
done
5.shell读取文件的写法,可以对每一行做后续处理
#!/bin/bash
cat /etc/passwd| while read line
do
echo ${line} #####做后续处理
done
6.批量检测用户是否可以登入(前提是配置好了密钥认证)
#!/bin/bash
for i in `cat ip.list`
do
ssh root@${i} "ls" > /dev/null 2>&1
if [ $? -eq 0 ];then
echo "$i" sucess
else
echo "$i" error
fi
done
echo "Erase ip.list!!!! please write it next "
echo >ip.list
7.批量检测是否可以ping通
8.监控cpu,内存
#!/bin/bash
ip=$(ifconfig eth1 | grep "inet addr:" | awk -F ":" '{print $2} '
)
time=$(date +%Y%m%d-%H:%M)
log_name="monite.log.${time}"
mailto="xxxxxxx"
function check_cpu()
{
echo "---------------cpu----------------">>${log_name}
us=$(vmstat |tail -n 1 | awk '{print $13}')
sy=$(vmstat |tail -n 1 | awk '{print $14}')
idle=$(vmstat |tail -n 1 | awk '{print $15}')
wait1=$(vmstat |tail -n 1 | awk '{print $16}')
all_use=$(($us+$sy))
if [ ${all_use} -ge 50 ];then
echo "cpu user >50,is ${all_use}" >>${log_name}
echo "idle is ${idle}" >>${log_name}
echo "wait1 is ${wait1}" >>${log_name}
echo "$ip ,cpu use >50 " | mail -s "$ip,CPU >50" ${mailto}
fi
echo "----------------------------------">>${log_name}
}
function check_ram()
{
echo "---------------ram----------------">>${log_name}
all=$( free | grep Mem | awk '{print $2 }')
use=$( free | grep Mem | awk '{print $3 }')
free1=$( free | grep Mem | awk '{print $4 }')
if [ ${free1} -le 1024 ];then
echo "$ip all=${all},use=${use},free ram =${free1}" >>${log_name}
echo "$ip all=${all},use=${use}, free ram =${free1}" | mail -s "ram <1G" ${mailto}
fi
echo "----------------------------------">>${log_name}
}
function main()
{
check_cpu
check_ram
}
main
mailto='xxxx'
day=`date +%Y%m%d`
function isLive
{
ser=(httpd nginx) ####这里可以添加进相关的服务
for i in ${ser[*]}
do
ser_stat=$(ps -ef | grep $i| grep -v grep | wc -l)
if [ ${ser_stat} -eq 0 ];then
now=`date +%Y%m%d-%H:%M`
echo ${now} >>/tmp/ser_restart_${day}.log
/etc/init.d/$i restart 2&>1 >>/tmp/ser_restart_${day}.log
echo "service $i is unnoraml in ${now}" | mail -s $i ${mailto}
fi
done
}
isLive
10. kill 监听指定端口的进程
lsof -i:$1 | grep -v "COMMAND" | awk -F " " '{print $2}' | xargs kill -9