shell腳本練習->1
->系統當前狀態腳本
腳本內容如下:
[root@jumpserver-70 scripts]# cat system.sh
#!/bin/bash
#系統信息
system_version=$(hostnamectl | awk -F ‘:‘ ‘/Operating System/{print $2}‘)
system_kernel=$(hostnamectl | awk -F ‘:‘ ‘/Kernel/{print $2}‘)
system_network=$(hostname -I)
system_host=$(hostnamectl |awk -F ‘:‘ ‘/Static hostname/{print $2}‘)
network_out=$(curl -s icanhazip.com)
#cpu
cpu_load=$(w|awk -F ‘:‘ ‘NR==1{print $NF}‘)
#內存
mem_total=$(free -h | awk ‘/^Mem/{print $2}‘)
mem_use=$(free -h | awk ‘/^Mem/{print $3}‘)
mem_B=$(free -m | awk ‘/^Mem/{print int($3*100/$2)}‘)
echo "-------------------------------system info-----------------------------------------"
echo "當前系統的版本:$system_version
當前系統的內核:$system_kernel
當前系統的虛擬化平臺:
當前系統的主機名:$system_network
當前系統的內網ip:$system_host
當前系統的外網ip:$network_out
"
echo "-------------------------------system cpu-----------------------------------------"
echo "當前cpu負載情況 : 1分鐘,5分鐘,15分鐘使用:$cpu_load"
echo "-------------------------------system mem---------------------------------------"
echo "當前總內存大小:$mem_total
當前內存使用率:$mem_use
當前內存使用百分比:$mem_B%
"
echo "-------------------------------system disk---------------------------------------"
for ((i=2;i<=8;i++))
do
df -h |awk ‘NR==‘$i‘{print "磁盤分區:"$NF,"使用了"$(NF-1)}‘
done
echo "--------------------------------------------------------------------------------------"
實現效果如下:
->查看系統當前內存狀態
[root@jumpserver-70 scripts]# cat mem.sh
#!/bin/bash
Mem_Total=$(free -m|grep "^M"|awk ‘{print $2}‘)
Mem_Use=$(free -m|grep "^M"|awk ‘{print $3}‘)
Mem_B=$((($Mem_Use*100)/$Mem_Total))
if [ $Mem_B -ge 30 ];then
echo -e "\033[31m Memory Is Err ${Mem_B}% \033[0m"
else
echo -e "\033[32m Memory Is OK ${Mem_B}% \033[0m"
fi
實現效果如下:
[root@jumpserver-70 scripts]# sh mem.sh
Memory Is OK 9%
->查看ip 是否暢通
[root@jumpserver-70 scripts]# cat ping.sh
#!/bin/bash
read -p "input ip: " ip
ping -c2 $ip &>/dev/null
if [ $? -eq 0 ];then
echo "host $ip is ok"
else
echo "host $ip is error"
fi
->創建用戶並設置密碼
[root@jumpserver-70 scripts]# cat user.sh
#!/bin/bash
#1.判斷用戶輸入是否為空
read -p "請輸入要創建用戶的數量 :" num
if [[ -z $num ]];then
echo "輸入的用戶名不能為空"
exit 1
fi
#2.判斷用戶輸入的是否為數字
if [[ ! "$num" =~ ^[0-9]+$ ]];then
echo "你輸入的不是數字"
exit 1
fi
#3.判斷用戶輸入的是否為空
read -p "請輸入要創建用戶的名稱 :" name
if [[ -z $name ]];then
echo "用戶名不能為空值"
exit 1
fi
#4.判斷用戶輸入的是否為數字
if [[ ! "$name" =~ ^[a-z]+$ ]];then
echo "你輸入的不能是小寫字母"
exit 1
fi
#5.遍歷用戶輸入的數字
for i in $(seq $num);do
useradd $name$i # 創建用戶
echo "123" |passwd --stdin $name$i &>/dev/null #給新創建的用戶設置密碼123
echo "$name$i用戶創建成功,密碼為:123"
done
->安裝nginx->查看狀態
echo "===============================System Repo============================="
Repos=$(yum repolist |grep nginx|wc -l)
if [ $Repos -eq 0 ];then
echo "沒有發現Nginx的yum倉庫...嘗試中"
cat >/etc/yum.repos.d/nginx.repo <<-EOF #使用cat方法導入
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/7/x86_64/
gpgcheck=0
enabled=1
EOF
yum makecache &>/dev/null #清空緩存
Repos2=$(yum repolist |grep nginx|wc -l) #再次yum嘗試安裝
if [ $Repos2 -eq 1 ];then
echo "yum倉庫已經安裝完成...."
else
echo "請檢查你的網絡環境...."
exit 1
fi
else
echo "已存在Nginx的Repos倉庫文件..."
fi
echo "===============================System Nginx Install======================="
Rpm_Nginx=$(rpm -qa nginx | wc -l)
if [ $Rpm_Nginx -gt 0 ];then
echo "Nginx 已經安裝....."
else
echo "嘗試安裝Nginx...."
yum install nginx -y &>/dev/null
fi
echo "=======================System Nginx Status ======================"
Nginx_Status=$(systemctl status nginx|grep running|wc -l)
if [ $Nginx_Status -eq 1 ];then
echo "Nginx已經啟動"
else
echo "Nginx嘗試啟動"
pkill httpd &>/dev/null
pkill nginx &>/dev/null
systemctl start nginx &>/dev/null
if [ $? -eq 0 ];then
Nginx_Status2=$(systemctl status nginx|grep Active|awk ‘{print $2 $3}‘)
echo "nginx啟動完成, 當前的狀態是: $Nginx_Status2"
else
echo "啟動失敗, 請手動排查......"
fi
fi
->nginx狀態管理
[root@jumpserver-70 scripts]# cat nginx_status.sh
#!/bin/bash
echo "-----------------------"
echo "1:開啟"
echo "2:停止"
echo "3:重載"
echo "4:重啟"
echo "5:狀態"
echo "-----------------------"
read -p "請輸入您要執行的命令:" command
case $command in
1)
status_start=$(systemctl status nginx | egrep "running" |wc -l)
if [ $status_start -eq 1 ];then
echo "nginx已經啟動,不需要執行啟動操作"
else
systemctl start nginx &>/dev/null
echo "nginx已經啟動"
fi
;;
2)
systemctl stop nginx &>/dev/null
echo "nginx已經停止"
;;
3)
status_start=$(systemctl status nginx | egrep "running" |wc -l)
if [ $status_start -eq 1 ];then
systemctl reload nginx &>/dev/null
echo "nginx已經重載"
fi
;;
4)
systemctl restart nginx &>/dev/null
echo "nginx已經重啟"
;;
5)
systemctl status nginx
;;
*)
echo "請您選擇正確的命令"
exit 1
esac
->選擇php版本安裝
[root@jumpserver-70 scripts]# cat php.sh
#!/bin/bash
echo ‘1=(php-5.0)‘
echo ‘2=(php-6.0)‘
echo ‘3=(php-7.0)‘
read -p "請輸入要安裝的版本:" num
if [ $num -eq 1 ];then
echo "正在安裝php5.0"
elif [ $num -eq 2 ];then
echo "正在安裝php6.0"
elif [ $num -eq 3 ];then
echo "正在安裝7.0"
else
echo "請選擇安裝版本"
fi
->根據系統版本安裝yum源
[root@jumpserver-70 scripts]# cat repo.sh
#!/bin/bash
os_name=$(cat /etc/redhat-release )
os_version=$(cat /etc/redhat-release |awk -F " " ‘{print $4}‘| awk -F "." ‘{print $1}‘)
if [ $os_version -eq 7 ];then
echo "這是$os_name的系統,請安裝centos7的yum源"
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo &>/dev/null
echo "$os_name:yum源安裝完成"
elif [ $os_version -eq 6 ];then
echo "這是$os_name系統,請安裝centos6的yum源"
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo &>/dev/null
echo "$os_name:yum源安裝完成"
elif [ $os_version -eq 5 ];then
echo "這是$os_name系統,請安裝centos6的yum源"
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-5.repo &>/dev/null
echo "$os_name:yum源安裝完成"
else
echo "請檢查系統的版本"
fi
shell腳本練習->1