1. 編寫腳本實現(xiàn)傳入進程pid,查看對應進程/proc下CPU返敬、內(nèi)存指標
#!/bin/bash
#查看proc對應進程下的CPU、內(nèi)存指標
read -p "please input pid : " pid
pid_exist=`ps aux | awk '{print $2}' | grep $pid`
if [ ! $pid_exist ];then
echo "$pid is not exist"
else
echo 'memory usage :'
cat /proc/$pid/status | grep ^Vm
echo 'cpu usage :'
ps -Lo pid,lwp,pcpu -p $pid
fi
2. 編寫腳本實現(xiàn)每分鐘檢查一個主機端口是否存活(提示使用nmap),如果檢查到端口不在線寥院,sleep 10s,如果三次不存在劲赠,記錄到日志
#!/bin/bash
read -p "please input a ip address : " ip_addr
read -p "please input a port : " port
port_stat=`nmap $ip_addr -p $port | grep "^[0-9]" | awk '{print $2}'`
i=1
while [ "$i" -le 3 ];do
if [ "$port_stat" != "open" ];then
sleep 10s
else
break
fi
let i++
if [ $i -eq 3 ];then
echo "host:$ip_addr port:$port is not open" >> /var/log/host_port.log
fi
done
3. 編寫腳本/root/bin/execute.sh,判斷參數(shù)文件是否為sh后綴的普通文件,如果是,添加所有人可執(zhí)行權限凛澎,否則提示用戶非腳本文件
#!/bin/bash
file=`echo "$1"|sed -nr 's/.*\.(sh$)/\1/p'`
if [ "$file" = "sh" -a -f "$1" ];then
chmod a+x $1
echo "已添加所有人執(zhí)行權限"
else
echo "$1 非腳本文件"
fi
4. 編寫腳本/root/bin/nologin.sh和login.sh,實現(xiàn)禁止和允許普通用戶登錄系統(tǒng)
1 #!/bin/bash
2 # 禁止普通用戶登錄系統(tǒng)
4
5 if [ ! -f "/etc/nologin" ];then
6 touch "/etc/nologin"
7 fi
1 #!/bin/bash
2 #允許普通用戶登錄系統(tǒng)
4
5 [ -f "/etc/nologin" ] && rm -f /etc/nologin
5. 編寫腳本/root/bin/sumid.sh,計算/etc/passwd文件中的第10個用戶和第20個用戶的ID之和
#!/bin/bash
nl /etc/passwd|awk -F '[[:space:]]+|:' '$2==10||$2==20{i+=$5}END{print i}'