1、編寫腳本實現(xiàn)傳入進程pid,查看對應(yīng)進程/proc下CPU、內(nèi)存指標(biāo)
#!/bin/bash
ps -Lo %cpu,%mem -p `pgrep $1`
2、編寫腳本實現(xiàn)每分鐘檢查一個主機端口是否存活(提示使用namp),如果檢查到端口不在線,sleep10s,如果三次都存在,記錄到日志
#!/bin/bash
i=0
while :;do
jieguo=`nmap -p$2 $1 |sed -nr 's/^[0-9]+.* ([^ ]+) .*/\1/p'`
if [[ "$jieguo" =~ "closed" ]];then
let i++
if [ $i -eq 3 ];then
echo " $2 is closed in `date +%F-%H-%M-%S`" >>chport.log
else
sleep 10
continue
fi
fi
i=0
sleep 60
done
3、編寫腳本/root/bin/excute.sh,判斷參數(shù)文件是否為sh后綴的普通文件,如果是,添加所有人可執(zhí)行權(quán)限,否則提示用戶非腳本文件
#!/bin/bash
if [[ $1 =~ .*\.sh ]];then
if [ -f $1 ];then
chmod +x $1
exit
fi
fi
echo "It's not a shell script file"
4、編寫腳本/root/bin/nologin.sh和login.sh實現(xiàn)禁止和允許普通用戶登錄系統(tǒng)
#!/bin/bash
usermod -s /sbin/nologin $1
#!/bin/bash
usermod -s /bin/bash $1
5、編寫腳本/root/bin/sumid.sh,計算/etc/passwd文件中的第10個用戶和第20個用戶的ID之和
#!/bin/bash
n1=`sed -nr '10s/[^:]*:[^:]:([^:]*):.*/\1/p' /etc/passwd`
n2=`sed -nr '20s/[^:]*:[^:]:([^:]*):.*/\1/p' /etc/passwd`
echo $[$n1+$n2]