|
#如果当前控制台$LANG=en_US.UTF-8,请先设置 LANG=zh_CN.GB2312或GB18030,否则复制脚本可能会出错。 (已在Centos5.3测试)
#!/bin/bash
if [ $UID -ne 0 ];then
echo error: You are not root,and Please run it by using root !
exit 1
fi
ifi=`lsattr -d /mnt | awk '{print substr($0,5,1)}'`
for ((i=4;i>=1;i--))
do
if [ "$ifi" == "i" ];then
echo -e '目录"/mnt"受保护,是否继续进行:(y/n):'
read -t 10 CHOICE
case $CHOICE in
y|Y|yes)
chattr -i /mnt
break
;;
n|N|no)
echo
echo "ok,exiting......"
sleep 3
echo "Bye!!"
exit 0
;;
*)
export I=`expr $i - 1`
[ $I != 0 ]&& echo -e "Please choice "y" or "n" !!\nYou still have $I times!!"
if [ $I == 0 ];then
echo Exiting.....
sleep 2
echo Bye!!
exit 2
fi
continue
;;
esac
fi
break
done
LANG=zh_CN.GB2312
FUNC(){
num_user=`netstat -tn | grep -i 22 | awk '{print $5}'|wc -l`
w | awk '$2~/^pts/{print $1"\t\t"$2"\t\t"$3"\t\t"$4}' >/mnt/.1
cat /mnt/.1 |awk '{print $1"\t"$2}'|sed 's/\t/@/g'>/mnt/.2
if [ -e /mnt/.3 ];then
read -t 5 -p "因程序需要,需删除"/mnt/.3"文件,删除?(y/n):" CH
case $CH in
y|yes|Y)
rm -fr /mnt/.3
;;
n|no|N)
BACKOFF
;;
*)
rm -fr /mnt/.3
;;
esac
fi
while read LINE
do
ps aux | grep sshd| grep $LINE |awk '{print $2}'>>/mnt/.3
done</mnt/.2
paste -d"\t\t" /mnt/.1 /mnt/.3 \
|awk 'BEGIN{print "\nSerNum\tNAME\t\tTTY\t\tIP\t\t\tLOGTIME\tPID"}{print "["NR"]\t" $0}'\
|sed '/[a-zA-Z].*/s/\(.*\)/\1 \n___________________________________________________________________________________/g' \
|tee /mnt/.4
num_user=`netstat -tn | grep -i 22 | awk '{print $5}'|wc -l`
echo 一共$num_user个用户在线!!!
echo 现在时间:`date`
}
BACKOFF(){
echo
echo "Exiting...."
sleep 2
echo "Bye...."
exit 0
}
DOALLTIME(){
echo "是否让该脚本在后台监视?(y/n)"
read -t 15 YN
case $YN in
y|yes|Y)
while :
do
echo -e "\n\n\n"
echo '*********************************************************************************************'
echo "当前进程为:$! 结束脚本请使用 Kill -9 $!"
FUNC
echo '*********************************************************************************************'
echo -e "\n\n\n"
sleep 5
done &
;;
*)
BACKOFF
;;
esac
}
GetNumber(){
echo -n " 请选择序列号:【1,2,3....】:"
read CHS
}
FUNC
sleep 1
read -t 20 -p "是否想删除个别用户?(y/n):" DEL
case $DEL in
y|yes|Y)
for ((i=4;i>=1;i--))
do
GetNumber
if [ "$CHS" -gt "$num_user" ];then
[ $i -eq 1 ]&&echo Exiting....&&sleep 2&&echo Bye......&&exit 3
echo "该序列号对应的进程不存在,请重新输入【还有`expr $i - 1`次机会】:"
continue
fi
break
done
nr=`expr $CHS + $CHS + 2`
pid=`head -$nr /mnt/.4|tail -1|awk '{print $6}'`
read -t 15 -p "要结束的进程为:$pid,确定删除?(y/n)" Choice
case $Choice in
y|yes|Y)
kill -9 $pid
[ $? == 0 ]&&echo -e "成功删除进程!\nCongratulation......"
DOALLTIME
;;
*)
DOALLTIME
;;
esac
;;
*)
DOALLTIME
;;
esac
|
|