1、网络状态判断
#!/bin/bash
#
IP=10.0.0.201
ping -c1 -w1 $IP &> /dev/null && echo "$IP is up" ||
{ echo "$IP is unreachable";exit; }
echo "Script is finished"
2、磁盘空间判断
#!/bin/bash
WARNING=30
SPACE_USED=`df |grep -E "^/dev/(sda*|ma*)"|tr -s ' ' %|cut -d% -f5|sort -nr|head -1`
[ "$SPACE_USED" -ge "$WARNING" ] && echo "disk used is $SPACE_USED.willbe full"
3、体重指数测试
#!/bin/bash
read -p "请输入身高(m为单位):" HIGH
if [[ ! "$HIGH" =~ ^[0-2]\.?[0-9]{,2}$ ]];then
echo "输入错误身高"
exit 1
fi
read -p "请输入体重(kg为单位):" WEIGHT
if [[ ! "$WEIGHT" =~ ^[0-9]{1,3}$ ]];then
echo "输入错误体重"
exit 1
fi
BMI=$(echo "$WEIGHT/$HIGH^2"|bc)
if [ $BMI -le 18 ];then
echo "太瘦了,多吃点"
elif [ $BMI -lt 24 ];then
echo "正好!"
else
echo "太胖了"
fi
4、批量创建用户
#!/bin/bash
if [ $# -eq 0 ];then
echo "Usage: `basename $0` user1 user2 ..."
exit
fi
while [ "$1" ];do
if id $1 &> /dev/null;then
echo $1 is exist
else
useradd $1
echo "$1 is created"
fi
shift
done
echo "All user is created"
5、磁盘监控
#!/bin/bash
#
WARNING=20
df |sed -nr "/^\/dev\/sd|ma/s#^([^ ]+) .* ( [0-9]+)%.*#\1\2#p"|while read DEVICE USE;do
if [ $USE -gt $WARNING ];then
echo "$DEVICE will be full,use:$USE"
fi
done
6、批量禁用iptables
#!/bin/bash
lastb |sed -nr "/ssh:/s#.* ([0-9.]{1,3}{3}[0-9]{1,3}).*#\1#p" |sort |uniq -c |while read count ip;
do
if [ $count -gt 3 ];then
iptables -A INPUT -s $ip -j REJECT
fi
done
7、查看/sbin/nologin的shell类型的用户名和UID
#!/bin/bash
while read line
do
if [[ "$line" =~ /sbin/nologin$ ]];then
echo $line | cut -d: -f1,3
fi
done < /etc/passwd
8、自动安装apache
#!/bin/bash
PACKAGE=httpd-2.4.43.tar.bz2
PACKAGRDIR=/usr/local/src
INSTALLDIR=/apps/httpd
CONFDIR=/etc/httpd
yum install -y gcc make apr-devel apr-util-devel pcre-devel mod_ssl openssl-devel
id apache &> /dev/null || useradd -r -s /sbin/nologin apache
cd $PACKAGRDIR
tar xfv $PACKAGE
cd $PACKAGRDIR/httpd-2.4.43
./configure --prefix=$INSTALLDIR --sysconfdir=$CONFDIR --enable-ssl
make -j 4 && make install
echo 'PATH=/apps/httpd/bin:$PATH' > /etc/profile.d/httpd.sh
source /etc/profile.d/httpd.sh
sed -i.bak '/^User/s/daemon/apache/g' /etc/httpd/httpd.conf
sed -i.bak '/^Group/s/daemon/apache/g' /etc/httpd/httpd.conf
apachectl start
9、系统初始化脚本
#!/bin/bash
function disable_selinux {
sed -i.bak 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
echo "SElinux已禁用,重新启动后生效"
}
function disable_firewalld {
systemctl disable --now firewalld &> /dev/null
echo "防火墙已经禁用"
}
function set_ps1 {
echo "PS1='\[\e[1;35m\][\u@\h \w]\\$\[\e[0m\]'" > /etc/profile.d/reset.sh
echo "提示符已修改成功,请重新登录"
}
function set_eth {
sed -i.bak '/GRUB_CMDLINE_LINUX=/s#"$# net.ifnames=0"#' /etc/default/grub
grub2-mkconfig -o /boot/grub2/grub.cfg &> /dev/null
echo "网络名称已修改成功,请重新启动才能生效"
}
PS3="系统初始化(1-6):"
MENU='禁用SELinux 关防火墙 修改提示符 修改网卡名 以上全部 退出'
select M in $MENU;do
case $REPLY in
1)
disable_selinux
;;
2)
disable_firewalld
;;
3)
set_ps1
;;
4)
set_eth
;;
5)
disable_selinux
disable_firewalld
set_ps1
set_eth
;;
6)
break
;;
*)
echo "请输入正确数字"
esac
done
10、函数调用
#!/bin/bash
. /etc/init.d/functions #系统函数调用action显示美观化
function disable_selinux {
sed -i.bak 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
action "SElinux已禁用,重新启动后生效"
}
function disable_firewalld {
systemctl disable --now firewalld &> /dev/null
action "防火墙已经禁用"
}
function set_ps1 {
echo "PS1='\[\e[1;35m\][\u@\h \w]\\$\[\e[0m\]'" > /etc/profile.d/reset.sh
action "提示符已修改成功,请重新登录"
}
function set_eth {
sed -i.bak '/GRUB_CMDLINE_LINUX=/s#"$# net.ifnames=0"#' /etc/default/grub
grub2-mkconfig -o /boot/grub2/grub.cfg &> /dev/null
action "网络名称已修改成功,请重新启动才能生效"
}
PS3="系统初始化(1-6):"
MENU='禁用SELinux 关防火墙 修改提示符 修改网卡名 以上全部 退出'
select M in $MENU;do
case $REPLY in
1)
disable_selinux
;;
2)
disable_firewalld
;;
3)
set_ps1
;;
4)
set_eth
;;
5)
disable_selinux
disable_firewalld
set_ps1
set_eth
;;
6)
break
;;
*)
echo "请输入正确数字"
esac
done