• Shell脚本一键部署小服务


    目录

    Shell脚本配置DHCP

    Shell脚本配置DNS

    Shell脚本配置LVM

    Shell脚本配置RAID

    Shell脚本搭建yum

    Shell脚本磁盘分区


     提示:脚本仅供参考 如有不足 欢迎交流学习😀

    Shell脚本配置DHCP

    1. #!/bin/bash
    2. read -p "Please Enter Your IP ADDRESS: " net
    3. yum install -y dhcp &>/dev/null
    4. cp -p /usr/share/doc/dhcp*/dhcpd.conf.example /etc/dhcp/dhcpd.conf
    5. echo "ddns-update-style none;
    6. subnet `echo $net | cut -d '.' -f 1-3`.0 netmask 255.255.255.0 {
    7. range `echo $net | cut -d '.' -f 1-3`.40 `echo $net | cut -d '.' -f 1-3`.50;
    8. option routers $net;
    9. option domain-name-servers 114.114.114.114;
    10. next-server $net;
    11. filename \"pexlinux.0\";
    12. }">> /etc/dhcp/dhcpd.conf
    13. systemctl enable --now dhcpd
    14. systemctl status dhcpd | sed -n "3p" | awk '{print "DHCP服务状态:"$1,$2,$3}'

    Shell脚本配置DNS

    1. #!/bin/bash
    2. read -p "输入域名(form:###.com):" dn
    3. read -p "输入IP:" p
    4. yum install bind bind-utils.x86_64 -y &>/dev/null
    5. sed -i '/listen-on/s/any/$p/' /etc/named.conf
    6. sed -i '/allow-query/s/any/`echo $p | cut -d'.' -f1-3`.0/24/' /etc/named.conf
    7. sed -i 's/localhost.localdomain/$p' /etc/named.rfc1912.zones
    8. sed -i '1,named.localhost/{s/named.localhost/$dn.zone/}' /etc/named.rfc1912.zones
    9. touch /var/named/$dn.zone
    10. cp -p /named.localhost ./$dn.zone
    11. cat >> /var/named/$dn.com.zone <<EOF
    12. $TTL 1D
    13. @ IN SOA $dn. admin.$dn. (
    14. 0 ; serial
    15. 1D ; refresh
    16. 1H ; retry
    17. 1W ; expire
    18. 3H) ; minimum
    19. NS $dn.
    20. A $p
    21. AAAA ::1
    22. IN MAX 10 mail.$dn.
    23. WWW IN A $p
    24. mail IN A $p1
    25. ftp IN A CHAME www
    26. * IN A $p0
    27. EOF
    28. systemctl start named
    29. echo "DNS服务状态:`systemctl status named | sed -n '3p'`"
    30. echo "--------------------------------------------------------------"
    31. echo "DNS服务已配置完成!"
    32. echo "--------------------------------------------------------------"

    Shell脚本配置LVM

    1. #!/bin/bash
    2. lsblk
    3. read -p "请输入磁盘 如sdb 只能输入一个:" device
    4. fdisk /dev/$device << EOF
    5. n
    6. p
    7. t
    8. 8e
    9. w
    10. EOF
    11. disk="/dev/${device}1"
    12. read -p "请选择扩展或创建 'extend' or 'create' :" i
    13. if [ $i == create ] ;then
    14. read -p "vgname:" vgname
    15. read -p "lvname:" lvname
    16. read -p "请选择挂载目录位置,可以自动创建:" dir
    17. pvcreate $disk
    18. vgcreate $vgname $disk
    19. lvcreate -l 100%FREE -n $lvname $vgname
    20. mkfs.ext4 /dev/$vgname/$lvname
    21. mkdir -p $dir
    22. echo "/dev/$vgname/$lvname $dir ext4 defaults 1 1" >> /etc/fstab
    23. mount -a
    24. df -Th
    25. elif [ $i == extend ] ;then
    26. vgdisplay | grep -e 'VG Name' -e 'VG Size'
    27. read -p "vgname:" vgname
    28. lvdisplay | grep -e 'LV Path' -e 'LV Size'
    29. read -p "lvname:" lvname
    30. vgextend $vgname $disk
    31. lvextend -l +100%FREE /dev/$vgname/$lvname
    32. resize2fs /dev/$vgname/$lvname
    33. df -Th
    34. fi

    Shell脚本配置RAID

    1. #!/bin/bash
    2. echo "-------------------------------------------------------------"
    3. lsblk
    4. echo "-------------------------------------------------------------"
    5. echo -e "这是一个创建RAID的脚本程序\n1、RAID 1\n2、RAID 5\n3、RAID 10\n4、没有合适的设备,我还是先去做磁盘分区吧"
    6. read -p "请输入选择:" i
    7. yum -y install mdadma &>>/dev/null
    8. RAID1(){
    9. read -p "开始创建RAID1,请输入要操作的磁盘分区:(见以上信息,输入/dev/sd** /dev/sd**)" disks1 disks2
    10. read -p "请输入要创建的RAID名称:" md
    11. mdadm -C -v /dev/$md -l1 -n2 $disks1 $disks2
    12. read -p "输入RAID要挂载的目录:" dir
    13. read -p "RAID格式化的文件系统为:" fs
    14. mkdir /$dir/$md
    15. mkfs.$fs /dev/$md
    16. echo "/dev/$md /$dir $fs defaults 0 0 " >>/etc/fstab
    17. mount -a
    18. df -Th | grep "$dir" &>/dev/null
    19. if [ $? -eq 0 ];then
    20. echo "RAID 1创建完成,挂载目录为$dir"
    21. else
    22. echo "RAID 1挂载失败!"
    23. fi
    24. }
    25. RAID5(){
    26. read -p "开始创建RAID5,请输入要操作的磁盘分区:(见以上信息,输入/dev/sd** /dev/sd** /dev/sd** /dev/sd**)" disks1 disks2 disks3
    27. read -p "请输入要创建的RAID名称:" md
    28. mdadm -C -v /dev/$md -l5 -n3 $disks1 $disks2 $disks3
    29. read -p "输入RAID要挂载的目录:" dir
    30. read -p "RAID格式化的文件系统为:" fs
    31. mkdir /$dir/$md
    32. mkfs.$fs /dev/$md
    33. echo "/dev/$md /$dir $fs defaults 0 0 " >>/etc/fstab
    34. mount -a
    35. df -Th | grep "$dir" &>/dev/null
    36. if [ $? -eq 0 ];then
    37. echo "RAID 5创建完成,挂载目录为$dir"
    38. else
    39. echo "RAID 5挂载失败!"
    40. fi
    41. }
    42. RAID10()
    43. {
    44. read -p "开始创建RAID10,请输入要操作的磁盘分区:(见以上信息,输入/dev/sd** /dev/sd** /dev/sd** /dev/sd**)" disks1 disks2 disks3 disks4
    45. read -p "请输入要创建的RAID名称:" md
    46. mdadm -C -v /dev/$md -l10 -n3 $disks1 $disks2 $disks3 $disks4
    47. read -p "输入RAID要挂载的目录:" dir
    48. read -p "RAID格式化的文件系统为:" fs
    49. mkdir /$dir/$md
    50. mkfs.$fs /dev/$md
    51. echo "/dev/$md /$dir $fs defaults 0 0 " >>/etc/fstab
    52. mount -a
    53. df -Th | grep "$dir" &>/dev/null
    54. if [ $? -eq 0 ];then
    55. echo "RAID 10创建完成,挂载目录为$dir"
    56. else
    57. echo "RAID 10挂载失败!"
    58. fi
    59. }
    60. echo "-----------------------------------------------------------------------------------"
    61. lsblk
    62. echo "-----------------------------------------------------------------------------------"
    63. case $i in
    64. 1)
    65. RAID1
    66. ;;
    67. 2)
    68. RAID5
    69. ;;
    70. 3)
    71. RAID10
    72. ;;
    73. 4)
    74. exit
    75. ;;
    76. *)
    77. echo "erro"
    78. ;;
    79. esac

    Shell脚本搭建yum

    1. #!/bin/bash
    2. mkdir -p /etc/yum.repos.d/bak/
    3. mv /etc/yum.repos.d/*.repo /etc/yum.repos.d/bak/
    4. touch /etc/yum.repos.d/ali.repo
    5. touch /etc/yum.repos.d/epel.repo
    6. touch /etc/yum.repos.d/epel-testing.repo
    7. echo "[ali]
    8. name=ali
    9. baseurl=https://mirrors.aliyun.com/centos/7/os/x86_64/
    10. gpgcheck=0
    11. [epel]
    12. name=epel
    13. baseurl=https://mirrors.aliyun.com/centos/7/extras/x86_64/
    14. gpgcheck=0
    15. [update]
    16. name=update
    17. baseurl=https://mirrors.aliyun.com/centos/7/updates/x86_64/
    18. gpgcheck=0" >>/etc/yum.repos.d/ali.repo
    19. echo "[epel]
    20. name=Extra Packages for Enterprise Linux 7 - $basearch
    21. #baseurl=http://download.fedoraproject.org/pub/epel/7/$basearch
    22. metalink=https://mirrors.fedoraproject.org/metalink?repo=epel-7&arch=$basearch
    23. failovermethod=priority
    24. enabled=1
    25. gpgcheck=1
    26. gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7
    27. [epel-debuginfo]
    28. name=Extra Packages for Enterprise Linux 7 - $basearch - Debug
    29. #baseurl=http://download.fedoraproject.org/pub/epel/7/$basearch/debug
    30. metalink=https://mirrors.fedoraproject.org/metalink?repo=epel-debug-7&arch=$basearch
    31. failovermethod=priority
    32. enabled=0
    33. gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7
    34. gpgcheck=1
    35. [epel-source]
    36. name=Extra Packages for Enterprise Linux 7 - $basearch - Source
    37. #baseurl=http://download.fedoraproject.org/pub/epel/7/SRPMS
    38. metalink=https://mirrors.fedoraproject.org/metalink?repo=epel-source-7&arch=$basearch
    39. failovermethod=priority
    40. enabled=0
    41. gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7
    42. gpgcheck=1">>/etc/yum.repos.d/epel.repo
    43. echo "[epel-testing]
    44. name=Extra Packages for Enterprise Linux 7 - Testing - $basearch
    45. #baseurl=http://download.fedoraproject.org/pub/epel/testing/7/$basearch
    46. metalink=https://mirrors.fedoraproject.org/metalink?repo=testing-epel7&arch=$basearch
    47. failovermethod=priority
    48. enabled=0
    49. gpgcheck=1
    50. gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7
    51. [epel-testing-debuginfo]
    52. name=Extra Packages for Enterprise Linux 7 - Testing - $basearch - Debug
    53. #baseurl=http://download.fedoraproject.org/pub/epel/testing/7/$basearch/debug
    54. metalink=https://mirrors.fedoraproject.org/metalink?repo=testing-debug-epel7&arch=$basearch
    55. failovermethod=priority
    56. enabled=0
    57. gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7
    58. gpgcheck=1
    59. [epel-testing-source]
    60. name=Extra Packages for Enterprise Linux 7 - Testing - $basearch - Source
    61. #baseurl=http://download.fedoraproject.org/pub/epel/testing/7/SRPMS
    62. metalink=https://mirrors.fedoraproject.org/metalink?repo=testing-source-epel7&arch=$basearch
    63. failovermethod=priority
    64. enabled=0
    65. gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7
    66. gpgcheck=1">>/etc/yum.repos.d/epel-testing.repo

    Shell脚本磁盘分区

    1. #!/bin/bash
    2. echo "已存在分区有:"
    3. echo "------------------------------------------------"
    4. lsblk | awk -F " " '{print $1"\t"$4}'
    5. echo "------------------------------------------------"
    6. echo "提示:目前功能不完善,仅支持默认大小及分区创建"
    7. read -p "请输入您要创建的分区格式为(sda):" A
    8. read -p "请输入创建的分区大小(单位自定义MB、G):" B
    9. fdisk /dev/$A <<EOF >/dev/null
    10. n
    11. p
    12. +$B
    13. wq
    14. EOF
    15. lsblk | grep $A
    16. echo "创建结果请自行查看^-^"

  • 相关阅读:
    如何实现ElementUI动态表头?
    机器学习——特征工程
    什么是堡垒机
    【算法基础】双指针
    数据多维分析 - 派可数据商业智能BI可视化分析平台
    WPS ppt怎么设置自动播放?wps ppt如何设置自动放映?
    CSDN程序设计精品课程——Java程序设计(Java语言概述·Java语言基础·Java基本数据类型)
    Parameter estimation for text analysis (上)
    深度优先搜索dfs算法刷题笔记【蓝桥杯】
    Servlet的基础详解与架构解析
  • 原文地址:https://blog.csdn.net/weixin_71429839/article/details/126480192