码迷,mamicode.com
首页 > 系统相关 > 详细

shell脚本练习->1

时间:2018-09-19 11:33:40      阅读:181      评论:0      收藏:0      [点我收藏+]

标签:输入   i++   ima   ping   使用率   存在   bash   src   info   

->系统当前状态脚本

技术分享图片

 

脚本内容如下:

[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

标签:输入   i++   ima   ping   使用率   存在   bash   src   info   

原文地址:https://www.cnblogs.com/tim1blog/p/9673367.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!