init.sh

    1. #!/bin/bash
    2. echo -e "\n===========\e[32mset hostname\e[0m=============="
    3. # 设置主机名
    4. read -p "Enter hostname(`hostname`):" hostname
    5. if [[ $hostname ]];then
    6. hostnamectl set-hostname $hostname
    7. echo -e "\e[32mset hostname:$hostname success!\e[0m"
    8. else
    9. echo -e "\e[32mset hostname:`hostname` success!\e[0m"
    10. fi
    11. echo -e "\n===========\e[32mset network\e[0m==============="
    12. # 正则
    13. cidr_reg="^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])/([1-9]|[1-2][0-9]|3[12])$"
    14. ip_reg="^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$"
    15. # 设置网络ip
    16. read -p "set ip(eg:192.168.1.1/24):" address
    17. address=`echo $address | grep -Eoe ${cidr_reg}`
    18. # 设置网关
    19. read -p "set gateway(eg:10.10.1.254):" gateway
    20. gateway=`echo $gateway | grep -Eoe ${ip_reg}`
    21. if [[ $address && $gateway ]];then
    22. nmcli connection modify eth0 \
    23. connection.autoconnect yes \
    24. ipv4.method manual \
    25. ipv4.addresses $address \
    26. ipv4.gateway $gateway
    27. # 激活网卡
    28. nmcli connection up eth0
    29. else
    30. echo -e "\e[31mInvalid parameter!\e[0m"
    31. fi
    32. # 是否关闭防火墙
    33. echo -e "\n===========\e[32mset firewalld\e[0m============="
    34. read -p "Are you sure you want to close firewalld?[y/n]:" action
    35. case $action in
    36. y|Y|YES|yes)
    37. systemctl disable firewalld && systemctl stop firewalld && echo -e "\e[32msuccess!\e[0m"
    38. ;;
    39. n|N|NO|no)
    40. systemctl enable firewalld && systemctl restart firewalld && echo -e "\e[32msuccess!\e[0m"
    41. ;;
    42. esac
    43. # 是否关闭selinux
    44. echo -e "\n===========\e[32mset selinux\e[0m==============="
    45. read -p "Are you sure you want to close selinux?[y/n]:" action
    46. case $action in
    47. y|Y|YES|yes)
    48. setenforce 0 && sed -i 's/^SELINUX=.*/SELINUX=disabled/' /etc/selinux/config && echo -e "\e[32msuccess!\e[0m"
    49. ;;
    50. n|N|NO|no)
    51. setenforce 1 && sed -i 's/^SELINUX=.*/SELINUX=enforcing/' /etc/selinux/config && echo -e "\e[32msuccess!\e[0m"
    52. ;;
    53. esac
    54. echo -e "\n=============\e[32mset yum\e[0m================="
    55. read -p "set ip of the mirror repository(default:192.100.3.221):" yum_repository
    56. yum_repository=`echo $yum_repository | grep -Eoe ${ip_reg}`
    57. if [[ ! $yum_repository ]];then
    58. yum_repository=10.10.1.221
    59. fi
    60. if [[ `grep "yum_repository" /etc/hosts` ]];then
    61. sed -ri "/yum_repository/"c"$yum_repository yum_repository" /etc/hosts
    62. else
    63. echo "$yum_repository yum_repository" >> /etc/hosts
    64. fi
    65. echo -e "\n======\e[32mTest the network connectivity of the mirror repository\e[0m======"
    66. ping yum_repository -w 3