第七周作业

1.编写脚本实现传入进程pid,查看对应进程/proc下cpu、内存指标

#!/bin/bash

read -p "please input pid:" pid
ps_pid=`ps aux|awk '{print $2}'|grep $pid`
if [ ! $ps_pid ];then
    echo "Please ensure pid:$pid is exist!"
    exit 1
else
    echo "The MEM is `grep -i vmsize /proc/$pid/status |awk '{print $2}'` kB"
    echo "The CPU is `ps -Lo %cpu -p $ps_pid|sed -n 2p` %"
fi

2. 编写脚本实现每分钟检查一个主机端口是否存活(提示使用nmap),如果检查到端口不在线,sleep 10s,如果三次不存在,记录到日志

#!/bin/bash

read -p "Please input Ip:(eg:192.168.1.0): " ip_add
read -p "Please input Port:(eg:3306):  " ip_port
if [ ! $ip_add ];then
    echo "Please input Ip!"
    exit 1
elif [ ! $ip_port ];then
    echo "Please input Port!"
    exit 1
fi
i=1
while true;do
    port_status=`nmap $ip_add -p $ip_port|grep $ip_port|awk '{print $2}'`
    if [ "$prot_status" != "open" ];then
        sleep 10
        let i++
        if [ $i == 3 ];then
            echo "$ip_add:$ip_port is down at $(date +%F_%T)" >> /var/log/ip_port.log
            break
        fi
    else
        break
    fi
    sleep 60
done

3. 编写脚本/root/bin/execute.sh,判断参数文件是否为sh后缀的普通文件,如果是,添加所有人可执行权限,否则提示用户非脚本文件

#!/bin/bash

file=`echo "$1"|sed -nr 's#.*\.(sh$)#\1#p'`
if [ "$file" = "sh" -a -f "$1" ];then
        chmod a+x $1
        echo "Permission has been added!"
else
        echo "$1 is not scripts file!"
fi

4. 编写脚本/root/bin/nologin.sh和login.sh,实现禁止和允许普通用户登录系统

#!/bin/bash

if [ ! -f "/etc/nologin" ];then
    touch /etc/nologin
    #echo "ALL User is not allow to login!"
fi
#!/bin/bash

if [ -f "/etc/nologin" ];then
    rm -f /etc/nologin
    #echo "ALL User is allowed to login!"
fi

5. 编写脚本/root/bin/sumid.sh,计算/etc/passwd文件中的第10个用户和第20个用户的ID之和

#!/bin/bash

L10=`sed -n 10p /etc/passwd|awk -F: '{print$3}'`
L20=`sed -n 20p /etc/passwd|awk -F: '{print$3}'`
echo $(($L10+$L20))

 

posted @ 2019-08-06 10:35  李卓航  阅读(201)  评论(0编辑  收藏  举报