1、编写脚本实现传入进程pid,查看对应进程/proc下CPU、内存指标
#!/bin/bash
ps -Lo %cpu,%mem -p `pgrep $1`
2、编写脚本实现每分钟检查一个主机端口是否存活(提示使用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后缀的普通文件,如果是,添加所有人可执行权限,否则提示用户非脚本文件
#!/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实现禁止和允许普通用户登录系统
#!/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]