脚本练习一,实现查看/proc/pid下CPU内存指标、检查端口存活、检查文件后缀、允许和禁止用户登录

传入PID查看指标脚本

#!/bin/bash
#************************************************
#Author:                R
#Date:          2019-09-14 20:04:34
#File           pidchack.sh    
#************************************************
Rpid=`echo "$1"|sed -nr '/^[0-9]*$/p'`
if [ -z "$Rpid" ];then
    echo "Please input num!"
    exit 33
fi
ls /proc/$1 &>/dev/null
if [ $? -ne 0 ];then
    echo "PID is not exist"
    exit 34
fi
Rstatus=`cat /proc/$1/status`
echo "$Rstatus"|grep -i vm
echo "$Rstatus"|grep -i cpu

每分钟检查端口存活
脚本

#!/bin/bash
#************************************************
#Author:                R
#Date:          2019-09-14 21:19:21
#File           nmapchack.sh    
#************************************************
for i in {1..3};do
str=`nmap -sS 192.168.1.1`
str2=`echo $str|grep -i "is up"`
if [ ! -z "$str2" ];then
        exit 0
else
        if [ "$i" -ne 3 ];then
                sleep 10
        fi
fi
unset str
done
echo "host is dowm" >> nmaphost.log

添加每分钟执行一次

crontab –e
*/1 * * * * /data/bin/nmapchack.sh

编写脚本判断后缀,是sh则增加执行权限

#!/bin/bash
#************************************************
#Author:                R
#Date:          2019-09-17 16:39:03
#File           excute.sh        
#************************************************
if [ "$#" -eq 0 ];then
    echo "Please input file"
    exit 1
fi
cat $1 &>/dev/null
if [ "$?" -ne 0 ]; then
    echo "File is not exist"
    exit 2
fi
nam=`echo $1|sed -nr 's/.*\.(.*)/\1/p'`
if [ "$nam" == "sh" ];then
chmod +x $1
echo "Success"
fi
unset nam

编写脚本实现允许和禁止用户登录

允许用户登录

passwd方式

#!/bin/bash
#************************************************
#Author:                R
#Date:          2019-09-17 16:56:22
#File           login.sh    
#************************************************
if [ "$#" -eq 0 ];then
    echo "Please input user name"
    exit 1
fi
user=`getent passwd $1 |cut -d: -f3`
if [ -z $user ];then
    echo "user is not exist"
    exit 2
fi
if [ "$user" -lt 1000 ];then
    echo "Can not change system user"
    exit 3
fi
passwd -u $1

改shell类型方式

#!/bin/bash
#************************************************
#Author:                R
#Date:          2019-09-17 16:56:22
#File           login.sh    
#************************************************
if [ "$#" -eq 0 ];then
    echo "Please input user name"
    exit 1
fi
user=`getent passwd $1 |cut -d: -f3`
if [ -z $user ];then
    echo "user is not exist"
    exit 2
fi
if [ "$user" -lt 1000 ];then
    echo "Can not change system user"
    exit 3
fi
usermod -s /bin/bash $1
禁止用户脚本

passwd方式

#!/bin/bash
#************************************************
#Author:                R
#Date:          2019-09-17 16:56:22
#File           nologin.sh    
#************************************************
if [ "$#" -eq 0 ];then
    echo "Please input user name"
    exit 1
fi
user=`getent passwd $1 |cut -d: -f3`
if [ -z $user ];then
    echo "user is not exist"
    exit 2
fi
if [ "$user" -lt 1000 ];then
    echo "Can not change system user"
    exit 3
fi
passwd -l $1

改shell类型方式

#!/bin/bash
#************************************************
#Author:                R
#Date:          2019-09-17 16:56:22
#File           nologin.sh    
#************************************************
if [ "$#" -eq 0 ];then
    echo "Please input user name"
    exit 1
fi
user=`getent passwd $1 |cut -d: -f3`
if [ -z $user ];then
    echo "user is not exist"
    exit 2
fi
if [ "$user" -lt 1000 ];then
    echo "Can not change system user"
    exit 3
fi
usermod -s /sbin/nologin $1

计算第10个用户和第20个用户ID之和

#!/bin/bash
#************************************************
#Author:                R
#Date:          2019-09-17 17:17:31
#File           sumid.sh      
#************************************************
user1=`getent passwd|sed -n '10p' |cut -d: -f3`
user2=`getent passwd|sed -n '20p' |cut -d: -f3`
echo "$(($user1+$user2))"
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值