• Linux实战(12):Centos装机常用脚本-进阶版


    #!/bin/bash
    #shell菜单演示
    function menu()
    {
    echo -e `date`
    cat <<EOF
    -----------------------------------
    >>>菜单主页:
    `echo -e "33[35m 1)系统状态33[0m"`
    `echo -e "33[35m 2)服务管理33[0m"`
    `echo -e "33[35m 3)主菜单33[0m"`
    `echo -e "33[35m Q)退出33[0m"`
    EOF
    read -p "请输入对应序列号:" num1
    case $num1 in
        1)
        echo -e "33[32m >>>系统状态-> 33[0m"
        system_menu
        ;;
        2)
        echo -e "33[32m >>>服务管理-> 33[0m"
        server_menu
        ;;
        3)
        echo -e "33[32m >>>返回主菜单-> 33[0m"
        menu
        ;;
        Q|q)
        echo -e "33[32m--------退出--------- 33[0m"
        exit 0
        ;;
        *)
        echo -e "33[31m err:请输入正确的编号33[0m"
        menu
    esac
    }
    function system_menu()
    {
    cat<<EOF
    ------------------------
    ********系统状态********
    ------------------------
    1)docker 状态
    2)selinux 状态
    3)firewall 状态
    X)返回上一级目录
    ------------------------
    EOF
    read -p "请输入编号:" num2
    case $num2 in
        1)
        `echo -e "systemctl status docker"`
        system_menu
        ;;
        2)
         `echo -e "getenforce"`
        system_menu
        ;;
        3)
         `echo -e "systemctl status firewalld"`
        system_menu
        ;;
        x|X)
        echo -e "33[32m---------返回上一级目录------->33[0m"
        menu
        ;;
        *)
        echo -e "请输入正确编号"
        system_menu
    esac
    }
    function server_menu()
    {
    cat<<EOF
    ------------------------
    ********服务管理********
    ------------------------
    1)开启服务
    2)停止服务
    3)升级配置服务
    X)返回上一级目录
    ------------------------
    EOF
    read -p "请输入编号:" num3
    case $num3 in
            1)
            op_menu
            ;;
            2)
            op_menu1
            ;;
            3)
            op_menu2
            ;;        
            x|X)
            echo -e "33[32m-- -----返回上一级目录---------> 33[0m"
            menu
            ;;
            *)
            echo -e "请输入正确编号"
            system_menu
    esac
    }
    
    function op_menu()
    {
    cat<<EOF
    ------------------------
    1)开启docker服务
    2)开启selinux服务
    3)开启firewall服务
    X)返回上一级目录
    ------------------------
    EOF
    read -p "请输入编号:" num4
    case $num4 in
            1)
        `echo -e "systemctl start docker"`
        op_menu
            ;;
            2)
        echo "注意此操作为临时开启,重启失效"
        setenforce 1
            op_menu
        ;;
        3)
        `echo -e "systemctl start firewalld"`
            op_menu
            ;;
            x|X)
            echo -e "33[32m--------返回上一级目录------->33[0m"
            server_menu
            ;;
            *)
            echo -e "请输入正确编号"
        op_menu
    esac
    }
    function op_menu1()
    {
    cat<<EOF
    ------------------------
    1)停止docker服务
    2)临时关闭selinux
    3)停止firewall服务
    X)返回上一级目录
    ------------------------
    EOF
    read -p "请输入编号:" num5
    case $num5 in
            1)
            `echo -e "systemctl stop docker"`
            op_menu1
            ;;
            2)
            echo "注意此操作为临时关闭,重启失效"
             `echo -e "setenforce 0"`
            op_menu1
            ;;
            3)
            `echo -e "systemctl stop firewalld"`
            op_menu1
            ;;
            x|X)
            echo -e "33[32m >>>返回上一级目录---> 33[0m"
            server_menu
            ;;
            *)
            echo -e "请输入正确编号"
            op_menu1
    esac
    }
    function op_menu2()
    {
    cat<<EOF
    ------------------------
    1)更改yum源
    2)配置docker加速
    3)升级python到3
    4) 升级python3后配置
    X)返回上一级目录
    ------------------------
    EOF
    read -p "请输入编号:" num6
    case $num6 in
            1)
            yum install wget -y #先安装wget命令
            echo "------------------------安装wget命令------------------------"
            mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.bak
            echo 备份完毕
            releasetmp=`cat /etc/redhat-release | awk '{match($0,"release ")
    print substr($0,RSTART+RLENGTH)}' | awk -F '.' '{print $1}'`
            wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-$releasetmp.repo
            if [[ $? -eq 0 ]];then
              echo -e "33[32m# yum 源已成功更新为 aliyun_repo #33[0m";
              sleep 3;
            else
              echo -e "33[31m# yum 源未成功更新为 aliyun_repo #
    3s 后退出...33[0m";
              exit;
            fi
          yum clean all && yum makecache  && yum -y update
          echo "------------------------更新完毕------------------------"
            op_menu2
            ;;
            2)
            sudo tee /etc/docker/daemon.json <<-'EOF'
    {
      "registry-mirrors": ["https://0khgh4v2.mirror.aliyuncs.com"]
    }
    EOF
            sudo systemctl daemon-reload && sudo systemctl restart docker
          echo "------------------------Docker加速配置完毕------------------------"
            op_menu2
            ;;
            3)
            yum install libffi-devel zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gcc make -y && wget https://www.python.org/ftp/python/3.7.0/Python-3.7.0.tgz -O $PWD/Python-3.7.0.tgz && tar -zxvf Python-3.7.0.tgz 
            if [! -d "/usr/local/python3"];then
                mkdir /usr/local/python3 && cd $PWD/Python-3.7.0 &&  ./configure --prefix=/usr/local/python3
                make && make install
                    elif [! -d "$PWD/Python-3.7.0.tgz"];then
                        wget https://www.python.org/ftp/python/3.7.0/Python-3.7.0.tgz -O $PWD/Python-3.7.0.tgz
    
            else
                cd $PWD/Python-3.7.0 &&  ./configure --prefix=/usr/local/python3
                make && make install
            fi
            op_menu3
            ;;
            4)
            echo -e "33[32m >>>进入目录---> 33[0m"
            op_menu3
            ;;
            x|X)
            echo -e "33[32m >>>返回上一级目录---> 33[0m"
            server_menu
            ;;
            *)
            echo -e "请输入正确编号"
            op_menu2
    esac
    }
    function op_menu3()
    {
    cat<<EOF
    ------------------------
    1)配置Python,pip默认
    2)配置yum
    3)配置防火墙
    X)返回上一级目录
    ------------------------
    EOF
    read -p "请输入编号:" num7
    case $num7 in
            1)
            python2_path=`ls -al /usr/bin | grep -w "python -> python2" | awk '{print $9,$10,$11}' | wc -l`
            #python_path=`ls -l /usr/bin/python* | grep -w "python -> python2" | awk '{print $9}'`       
            if [ x"$python2_path" != x"0" ];then
                echo -e "33[32m <---------------未修改,现在开始修改默认python,pip--------------> 33[0m"
                mv /usr/bin/python /usr/bin/python.bak && rm -rf /usr/bin/python && rm -rf /usr/bin/pip && ln -s /usr/local/python3/bin/python3 /usr/bin/python  && ln -s /usr/local/python3/bin/pip3  /usr/bin/pip
                echo -e "33[32m <---------------已更改结束--------------> 33[0m"
                else
                    echo -e "33[32m <---------------已经修改无需配置--------------> 33[0m"
            fi
            op_menu3
            ;;
            2)
            yum_path=`cat /usr/bin/yum | grep -w "/usr/bin/python" | wc -l`
            yum_path1=`cat /usr/libexec/urlgrabber-ext-down | grep -w "/usr/bin/python" | wc -l`
            if [ x"$yum_path" != x"0" ]
            then
                echo -e "33[32m <---------------修改第一步--------------> 33[0m"
                sed -i "1c #!/usr/bin/python2" /usr/bin/yum
                echo -e "33[32m <---------------第一步已更改结束--------------> 33[0m"
            else
                echo -e "33[32m <---------------第一步已改动--------------> 33[0m"
            fi
            if [ x"$yum_path1" != x"0" ]
            then
                    echo -e "33[32m <---------------修改第二步--------------> 33[0m"
                    sed -i '1c #!/usr/bin/python2' /usr/libexec/urlgrabber-ext-down
                    echo -e "33[32m <---------------第二步已更改结束--------------> 33[0m"
            else
                    echo "------------------------第二步已改动------------------------"
            fi
            op_menu3
            ;;
            3)
            firewall_path=`cat /usr/sbin/firewalld | grep -w "/usr/bin/python" | wc -l`
            if [ x"$firewall_path" != x"0" ];then
                echo -e "33[32m <---------------未修改,现在开始修改默认firewall--------------> 33[0m"
                sed -i "1c #!/usr/bin/python2" /usr/sbin/firewalld
                echo -e "33[32m <---------------已更改结束--------------> 33[0m"
            else
                echo -e "33[32m <---------------已经修改无需配置--------------> 33[0m"
            fi
            op_menu3
            ;;
            x|X)
            echo -e "33[32m >>>返回上一级目录---> 33[0m"
            server_menu
            ;;
            *)
            echo -e "请输入正确编号"
            op_menu3
    esac
    }
    menu
    
  • 相关阅读:
    疫情控制
    2020 CSP-J 多校赛 Day 2 T2 题解
    三校联考-水题狂欢信心赛
    QBXT 提高组储备营 2020.夏 游记
    【题解】(我出的题)XM捡面筋
    【题解】P4025 [PA2014]Bohater
    【题解】1644:【例 4】佳佳的 Fibonacci
    【题解】CF1165F2 Microtransactions (hard version)
    P2261 [CQOI2007]余数求和
    OVO——扶咕咕的20道CF
  • 原文地址:https://www.cnblogs.com/98record/p/13648579.html
Copyright © 2020-2023  润新知