liunx脚本

98 阅读1分钟

开启掘金成长之旅!这是我参与「掘金日新计划 · 12 月更文挑战」的第16天,点击查看活动详情

检查服务器时间是否同步

#!/bin/bash
rhel=$(cat /etc/redhat-release | awk -F " " '{print $(NF-1)}' | awk -F "." '{print $1}')
ip=$(ifconfig `ip route show | awk '/default/{print $5}'` | grep -v inet6 | awk '/inet/{print $2}'|tr -d "addrs:" |awk -F "." '{print $1"."$2"."$3}')
ip1=$(ifconfig `ip route show | awk '/default/{print $5}'` | grep -v inet6 | awk '/inet/{print $2}' | tr -d "addrs:")
if [ $rhel -eq 7 ];then
	chronyc sources | tail -1 |grep "*" &>/dev/null
	if [ $? -eq 0 ];then
		echo "$ip1,系统版本$rhel,时间同步"
	else
		ntpq -p | tail -1 | grep '*' &>/dev/null
		if [ $? -eq 0 ];then
			echo "$ip1,系统版本$rhel,时间同步"
		else
			echo "$ip1,系统版本$rhel,时间不同步"
		fi
	fi
elif [ $rhel -eq 6 ];then
	ntpq -p | tail -1 | grep '*' &>/dev/null
	if [ $? -eq 0 ];then
		echo "$ip1,系统版本$rhel,时间同步"
	else
		echo "$ip1,系统版本$rhel,时间不同步"
	fi
elif [ $rhel -eq 8 ];then
	chronyc sources | tail -1 |grep "*" &>/dev/null
	if [ $? -eq 0 ];then
		echo "$ip1,系统版本$rhel,时间同步"
	else
		echo "$ip1,系统版本$rhel,时间不同步"
	fi
else
	echo "$ip1,系统版本$rhel,时间不同步"
fi

配置sudo权限

#!/bin/bash
#***********************************************************
#
#脚本名称:sudo_conf.sh
#
#脚本版本:version_7.0
#
#脚本用途:适用于RHEL系统普通用户SUDO权限配置
#
#更新时间:2021-11-4
#
#***********************************************************

#获取当前时间
DATE=`date '+%Y%m%d%H%M%S'`

#判断输入的值是否合规
if [ -z $1 ];then
  echo "!!您输入的用户不存在,执行脚本时请添加用户名作为第一变量!!"
  exit
elif [ `cat /etc/passwd|awk -F ":" '{print $1}'|grep -w $1|wc -l` -eq 0 ];then
  echo "!!您输入的用户不存在,请检查输入是否正确!!"
  exit
elif [ -z $2 ];then
  echo "!!您输入的期限不存在,执行脚本时请添加期限作为第二变量,输入数字0设置永久sudo!!"
  exit
elif [[ ! "$2" =~ ^[0-9]+$ ]];then
  echo "!!您输入的期限不合法,请使用数字!!"
  exit
fi

#判断用户是否已经有sudo权限
user_grep=`ls -lh /etc/sudoers.d/|grep $1|wc -l`
if [ $user_grep -gt 0 ];then
  echo "!!用户已存在sudo权限,无需配置!!"
  exit
else
  if [ "$2" -eq "0" ];then
    echo "$1 ALL=(ALL) NOPASSWD:ALL,!/bin/su,!/sbin/reboot,!/sbin/shutdown,!/sbin/poweroff,!/sbin/init,!/usr/sbin/reboot,!/usr/sbin/shutdown,!/usr/sbin/poweroff,!/usr/sbin/init,!/bin/bash" >> /etc/sudoers.d/withoutday
    echo "!!用户"$1"的sudo权限添加成功,无期限!!"
  else
    echo "$1 ALL=(ALL) NOPASSWD:ALL,!/bin/su,!/sbin/reboot,!/sbin/shutdown,!/sbin/poweroff,!/sbin/init,!/usr/sbin/reboot,!/usr/sbin/shutdown,!/usr/sbin/poweroff,!/usr/sbin/init,!/bin/bash" >> /etc/sudoers.d/$1_$DATE
####创建用户权限回收脚本
    cat > /root/$1.sh << EOF
#!/bin/bash
rm -rf /etc/sudoers.d/$1_$DATE
EOF
    chmod +x /root/$1.sh

####定时回收指定用户权限
    at now + $2 days -f /root/$1.sh
    rm -rf /root/$1.sh
    echo "!!用户"$1"的sudo权限添加成功,期限"$2"天!!"
  fi
fi