linux基础练习7

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

#!/bin/bash

read -p "input pid :" pid 

a=`ps aux | grep $pid | awk '{print $2}'| grep ^$pid`

if [ "$pid" == "$a" ];then

        cat /proc/$pid/status

else

        echo "pid no "

fi

2.编写脚本实现每分钟检查一个主机端口是否存活(提示使用nmap),如果检查到端口不在线,sleep10s如果3此都不存在,记录到日志当中

a(){

#!/bin/bash

sum=0

while [ $sum -lt 1 ];do

        a=`nmap 192.168.1.47 | grep ^[0-9] | cut -d " " -f 2|head -1`

        if [  "$a" == "open" ];then

                echo "hostname is up"

        elif [ "$a" == "" ];then

                echo "hostname is down"

                sleep 1

#                echo "hostname is down" >> /data/f4

        else

                true

        fi

        let sum+=1

done

}

a | grep "hostname is down" && echo "hostname down" >/data/f4

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

[[ $1 =~ \.*sh$ ]] && [ -f $1 ] && chmod +x $1 || echo fei job file `exit`

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

禁止普通用户登录脚本

#!/bin/bash

sed -i -r 's@(.*[0-9]{4}.*:).*@\1/sbin/nologin@' /etc/passwd

#!/bin/bash

启用普通用户登录脚本

#!/bin/bash

sed -i -r 's@(.*[0-9]{4}.*:).*@\1/bin/bash@' /etc/passwd

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

#!/bin/bash

a=`cat /etc/passwd|head -10|tail -1|cut -d: -f3`

b=`cat /etc/passwd|head -20|tail -1|cut -d: -f3`

# echo `let ${a}+${b}`

let c=a+b

echo $c

unset a b

猜你喜欢

转载自www.cnblogs.com/woaiyitiaochai/p/11757924.html