第七周作业

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

#!/bin/bash

read -p "Input pid to see CPU&Memory info: " pid

Input_pid=`ps aux | sed -nr "1! p" | tr -s " " | cut -d " " -f 2 | grep "$pid"`

if [ ! $Input_pid ];then

echo "$pid" 'does not exit! Check and input again.'

else

echo "Memory Usage :"

echo "`cat /proc/$pid/status | grep ^Vm`"

echo "Cpu Usage :"

echo "`cat /proc/$pid/status | grep ^Cpu`"

echo "%CPU is`ps -p $pid -o pcpu | sed -nr "2p"`"

fi

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

#!/bin/bash

ip=192.168.0.100
port=80

for ((i=1;i<=3;i++));do

nmap -p $port $ip | tail -n3 | head -n1 | grep -o open &> /dev/null

test=$(echo $?)

if [ "$test" -gt 0 ];then

sleep 10

else

break

fi

done

if [ "$test" -gt 0 ];then

echo "port not alive" >> /data/nmap.log

fi


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

#!/bin/bash

if [[ $1 =~ .sh$ ]];then

chmod +x $1

else

echo "is not .sh file!"

fi

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

nologin.sh :

#!/bin/bash

touch /etc/nologin

login.sh :

#!/bin/bash

rm -f /etc/nologin


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

#!/bin/bash

id10=$(cat /etc/passwd | cut -d: -f3 | head -$1 | tail -1)
id20=$(cat /etc/passwd | cut -d: -f3 | head -$2 | tail -1)
let sum=id10+id20
echo $sum

原文地址:https://www.cnblogs.com/n39-nash/p/11357363.html