第七週做業

一、編寫腳本實現傳入進程pid,查看對應進程/proc下CPU、內存指標bash

#!/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"`ip

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"`"input

fiit

 

二、編寫腳本實現每分鐘檢查一個主機端口是否存活(提示使用 nmap),若是檢查到端口不在線, sleep 10s,若是三次都不存在,記錄到日誌test

#!/bin/bash登錄

ip=192.168.0.100
port=80 sed

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

 


三、編寫腳本/root/bin/excute.sh ,判斷參數文件是否爲sh後綴的普通文件,若是是,添加全部人可執行權限,不然提示用戶非腳本文件

#!/bin/bash

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

chmod +x $1

else

echo "is not .sh file!"

fi

 

四、編寫腳本/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+id20echo $sum

相關文章
相關標籤/搜索