Linux脚本

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

 1 #!/bin/bash
 2 read -p "Please input PID to see CPU&MEM infos: " pid
 3 checkpid=`ps aux | sed -nr "1! p" | tr -s " " | cut -d " " -f 2 | grep "$pid"`
 4 if [ ! $checkpid ];then
 5         echo "$pid" 'does not exit! Please Check and input a exit PID.'
 6 else
 7         echo "Memory Usage :"
 8         echo "`cat /proc/$pid/status | grep ^Vm`"
 9         echo "Cpu Usage :"
10         echo "`cat /proc/$pid/status | grep ^Cpu`"
11         echo "Right Now %CPU is`ps -p $pid -o pcpu | sed -nr "2p"`"
12 fi

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

 1 #!/bin/bash
 2 #ip=`ip addr|awk -F '[ /]+' 'NR==9 {print $3}'`
 3 read -p 'please input the host ip:' ip
 4 count=`nmap $ip| grep ^[[:digit:]]|wc -l`
 5 i=0
 6 while true;
 7 do
 8         if [ $count -eq 0 ];then
 9                 let i=i+1
10 #               echo $i
11                 if [ $i -ge 3 ];then
12                         echo $ip >> /data/nmap.log
13                         echo `nmap $ip`>> /data/nmap.log
14                         i=0
15 #                       echo $i
16                 fi
17                 sleep 10
18         else
19                 echo 'Running'
20                 sleep 60
21         fi
22 done

判断参数文件是否为一个以.sh为后缀的文件,如果是,加执行权限,如果不是,提醒用户

 1 ##excute.sh
 2 
 3 #!/bin/bash
 4 
 5 if [ $# -lt 1 ];then
 6     echo please input a filename...
 7     exit 3
 8 elif [ -f $1 ];then
 9     if echo $1 | grep .sh &> /dev/null ;then
10             chmod +x $1
11             echo "modify $1 jurisdiction finished..."
12     else
13             echo "$1 not is script file..."
14     fi
15 else
16     echo "$1 not is common file..."
17 fi

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

 1 #!/bin/bash
 2 
 3 if [ ! -f "/etc/nologin" ];then
 4     touch /etc/nologin
 5     #echo "ALL User is not allow to login!"
 6 fi
 7 #!/bin/bash
 8 
 9 if [ -f "/etc/nologin" ];then
10     rm -f /etc/nologin
11     #echo "ALL User is allowed to login!"
12 fi

计算/etc/passwd 中第10行和第20行用户的UID之和

 1 ## sumid.sh
 2 
 3 #!/bin/bash
 4 
 5 id=`sed -nr '10,20s#.*:([0-9]+):[0-9].*#\1#p' /etc/passwd`
 6 num=(`echo $id`)
 7 for i in `seq 0 10`;do
 8     let sum+=${num[$i]}
 9 done
10 echo $sum

 

你可能感兴趣的:(Linux脚本)