centos初始化脚本

扩容

  1. #!/bin/bash
  2. #下述下个变量需要根据实际情况进行修改
  3.  
  4. disk="/dev/sdb"
  5. partition="$disk"
  6. num=
  7.  
  8. help ()
  9. {
  10. echo ' ====================================================================='
  11. echo ' The script has three variables: <<disk>>、<<partition>> and <<num>>'
  12. echo ' If variables are defined, select < y > to execute'
  13. echo ' If variables are not defined, select < q > to exit'
  14. echo " "
  15. echo " The disk is $disk;"
  16. echo " The part is $partition;"
  17. echo " The num is $num;"
  18. echo ' ====================================================================='
  19. }
  20. help
  21.  
  22. choose() {
  23. read -p "Please enter your choice
  24. y executing the script;
  25. q exit the script;
  26. Please input: " choice
  27. case $choice in
  28. q)
  29. echo " "
  30. echo "No initialize the disk, exit..." ;;
  31. y)
  32. echo "Initialize the disk..."
  33. echo " "
  34. kuorong ;;
  35. *)
  36. echo " "
  37. echo " "
  38. echo "Please input y or q"
  39. choose ;;
  40. esac
  41. }
  42. choose
  43.  
  44. kuorong() {
  45. echo "n
  46. p
  47.  
  48. t
  49. 8e
  50. w
  51. " | fdisk /dev/sdb &>/dev/null
  52.  
  53. #判断分区是否成功
  54. a="`fdisk -l | grep /dev/sdb1 | grep 8e | wc -l`"
  55. if [ $a -ge ]; then
  56. echo "分区成功,即将扩容;"
  57. sleep
  58. else
  59. echo "分区失败,即将退出;"
  60. sleep
  61. exit
  62. fi
  63.  
  64. #创建物理PV,并进行扩容
  65. #确定VG Name
  66. vgname="`vgdisplay | grep "VG Name" | awk '{print $3}'`"
  67. #定义需要扩容的目录
  68. contents="`df -h | grep /dev/mapper/centos-root | awk '{print $1}'`"
  69.  
  70. b="`cat /etc/fstab | grep $contents | grep "xfs" | wc -l`"
  71. if [ $b -ge ]; then
  72. echo "系统格式是xfs,可扩展;"
  73. sleep
  74. else
  75. echo "系统格式不对,即将退出;"
  76. sleep
  77. exit
  78. fi
  79.  
  80. echo "pvcreate $partition
  81. sleep
  82. vgextend $vgname $partition
  83. sleep
  84. lvextend -L +"$num"G $contents
  85. sleep
  86. pvdisplay
  87. quit
  88. " | lvm &>/dev/null
  89. sleep
  90. xfs_growfs /dev/mapper/centos-root
  91.  
  92. echo " "
  93. echo " "
  94.  
  95. vgdisplay | grep "VG Size"
  96.  
  97. echo " "
  98. echo " "
  99.  
  100. df -h
  101. }

添加IP、主机名、挂载/dev/sdb1磁盘

  1. #!/bin/bash
  2. #
  3.  
  4. ip=$
  5. hostname=$
  6.  
  7. if [ -z $ip ] || [ -z $hostname ]; then
  8. echo " "
  9. echo "Sorry, IP or host is null"
  10. echo "# The format : bash set.sh ip hostname"
  11. echo "# For example: bash set.sh 192.168.72.13 node1"
  12. echo " "
  13. exit
  14. fi
  15.  
  16. function check_ip() {
  17. VALID_CHECK=$(echo $ip|awk -F. '$1<=255&&$2<=255&&$3<=255&&$4<=255{print "yes"}')
  18. if echo $ip|egrep "^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$">/dev/null; then
  19. if [ ${VALID_CHECK:-no} == "yes" ]; then
  20. echo "ip $ip available."
  21. else
  22. echo "ip $ip not available!"
  23. exit
  24. fi
  25. else
  26. echo "ip format error!"
  27. exit
  28. fi
  29. }
  30.  
  31. ndir="/etc/sysconfig/network-scripts/ifcfg-ens192"
  32. ob="`grep "ONBOOT" $ndir`"
  33. ipaddr="`grep "IPADDR" $ndir`"
  34. gateway="`grep "GATEWAY" $ndir`"
  35. gt="`echo ${ip%.*}`"
  36. dns1="`grep "DNS1" $ndir`"
  37. dns2="`grep "DNS2" $ndir`"
  38. nt="`grep "NETMASK" $ndir`"
  39.  
  40. sed -i "s/$ob/ONBOOT=yes/g" $ndir
  41. sed -i "s/$ipaddr/IPADDR=$ip/g" $ndir
  42. sed -i "s/$gateway/GATEWAY=$gt.1/g" $ndir
  43. sed -i "s/$dns1/DNS1=8.8.8.8/g" $ndir
  44. sed -i "s/$dns2/DNS2=114.114.114.114/g" $ndir
  45. sed -i "s/$nt/NETMASK=255.255.255.0/g" $ndir
  46.  
  47. systemctl restart network
  48.  
  49. ping -c -W www.baidu.com &>/dev/null
  50. if [ $? != ]; then
  51. echo " "
  52. echo " "
  53. echo "The $ip is unavailable, Please check again......"
  54. echo "The $ip is unavailable, Please check again......"
  55. echo "The $ip is unavailable, Please check again......"
  56. echo " "
  57. echo " "
  58. exit
  59. else
  60. echo " "
  61. echo " "
  62. echo "The $ip is OK !!!"
  63. echo "The $ip is OK !!!"
  64. echo "The $ip is OK !!!"
  65. echo " "
  66. echo " "
  67. fi
  68.  
  69. hostnamectl --static set-hostname $hostname
  70.  
  71. echo "The name is `hostname`"
  72.  
  73. echo " "
  74. echo " "
  75.  
  76. disk="/dev/sdb"
  77. mpath="/opt/data"
  78.  
  79. Format_disk() {
  80. echo "n
  81. p
  82.  
  83. w
  84. " | fdisk $disk &>/dev/null && mkfs.ext4 "$disk"1 &>/dev/null
  85.  
  86. #mkdir $mpath
  87. mount "$disk" $mpath
  88. UUID=`blkid "$disk" | awk '{print $2}'`
  89. echo "$UUID $mpath ext4 defaults 0 0" >> /etc/fstab
  90. source /etc/fstab &>/dev/null
  91. df -T | grep "$disk"
  92. mount | grep "$disk"
  93. }
  94.  
  95. Judge() {
  96. fnum="`fdisk -l | grep /dev/sdb | wc -l`"
  97. if [ $fnum -ge ]; then
  98. mkdir -p $mpath
  99. echo "Initialize the disk $disk, and mount $mpath"
  100. read -p "Please enter the number
  101. No initialize the disk and exit;
  102. Initialize the disk;
  103. Please input: " num
  104. case $num in
  105. )
  106. echo " "
  107. echo "No initialize the disk, exit..." ;;
  108. )
  109. echo "Initialize the disk"
  110. sleep
  111. echo " "
  112. Format_disk ;;
  113. *)
  114. echo " "
  115. echo " "
  116. echo "Please input 0 or 1"
  117. Judge ;;
  118. esac
  119. else
  120. echo "The server does not have this disk: $disk"
  121. fi
  122. }
  123.  
  124. Judge
  125. bash

版本2

  1. <#
  2. Intro: This function will display a form to communicate with the user.
  3. Input: -FormText -ButtonText
  4. Example: MakeForm -FormText "ForInput" -ButtonText "Submit"
  5. Use: To make the PowerShell program's interactivity better.
  6. #>
  7. function MakeForm{
  8. param($FormText,$ButtonText)
  9. $null = [Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
  10. $form = New-Object Windows.Forms.Form
  11. $form.size = New-Object Drawing.Size -Arg ,
  12. $form.StartPosition = "CenterScreen"
  13. $form.Text = $FormText.toString()
  14. $textBox = New-Object Windows.Forms.TextBox
  15. $textBox.Dock = "fill"
  16. $form.Controls.Add($textBox)
  17. $button = New-Object Windows.Forms.Button
  18. $button.Text = $ButtonText
  19. $button.Dock = "Bottom"
  20. $button.add_Click(
  21. {$global:resultText = $textBox.Text;$form.Close()})
  22. $form.Controls.Add($button)
  23. [Void]$form.ShowDialog()
  24. }
  25.  
  26. echo " "
  27. echo "++++++ 添加IP ++++++"
  28. echo " "
  29. MakeForm -FormText "请输入主机IP:" -ButtonText "Submit"
  30. echo "即将配置: $resultText 到此服务器"
  31.  
  32. $wmi = Get-WmiObject win32_networkadapterconfiguration -filter "ipenabled = 'true'"
  33.  
  34. #注意: 此处是子网掩码,根据实际情况进行更改
  35. $wmi.EnableStatic("$resultText", "255.255.255.0") | Out-Null
  36.  
  37. $a = $resultText.split(".",)[]
  38. $b = $resultText.split(".",)[]
  39. $c = $resultText.split(".",)[]
  40. $d = $resultText.split(".",)[]
  41. $gateway = "$a.$b.$c.1"
  42.  
  43. $wmi.SetGateways("$gateway") | Out-Null
  44.  
  45. #注意: 此处是DNS,根据实际情况进行更改
  46. $dns = "10.1.1.1", "10.1.1.1"
  47.  
  48. $wmi.SetDNSSERVERSearchOrder($dns) | Out-Null
  49.  
  50. ipconfig /flushdns | Out-Null
  51. sleep
  52. ipconfig /flushdns | Out-Null
  53. #$CurrentyDir = Split-Path -Parent $MyInvocation.MyCommand.Definition
  54. #C:\opstools\Init_file\ChangeName.vbs
  55. netsh interface ip show config | Out-Null
  56. sleep
  57. #ping www.baidu.com
  58.  
  59. echo " "
  60. echo " "
  61.  
  62. #注意:此处需要根据情况修改主机名
  63. echo "++++++ 更改主机名 ++++++"
  64. echo " "
  65. #rename-computer -newname "BX-A-$c-$d"
  66. $newname="BX-A-$c-$d"
  67. $computer=Get-WMIObject Win32_ComputerSystem
  68. $computer.Rename( $NewName ) | out-null
  69.  
  70. #C:\opstools\Init_file\ChangeName.vbs
  71. #sleep
  72. #echo "+++++ 主机名未生成 +++++"
  73. #$result = Test-Path C:\opstools\Init_file\ComputerName.txt
  74. #while ($result -cnotcontains "False")
  75. # {
  76. # $result = Test-Path C:\opstools\Init_file\ComputerName.txt
  77. # echo " 等待输入 ComputerName"
  78. # sleep
  79. # }
  80.  
  81. echo " "
  82. echo "+++++ 获取更改后的主机名 +++++"
  83. #$CName = Get-Content C:\opstools\Init_file\ComputerName.txt
  84. #echo $CName
  85. #echo " "
  86. #echo " "
  87. #sleep
  88.  
  89. echo "+++++ 部署 Salt +++++"
  90. Copy-Item "\\10.192.2.1\d$\opstools\salt-package" -Destination "d:\" -recurse
  91. cmd.exe /c "d:\salt-package\Salt-Minion-2016.3.0-AMD64-Setup.exe /S /master=10.3.2.2 /minion-name=$newname"
  92. #return
  93.  
  94. Get-Service -Name salt-minion
  95. sleep
  96. #Remove-Item 'C:\opstools\Init_file\ComputerName.txt'
  97. #sleep
  98.  
  99. netsh interface ip show config
  100. sleep
  101. ping www.baidu.com
  102. Shutdown.exe -r -t

第三版

  1. <#
  2. Intro: This function will display a form to communicate with the user.
  3. Input: -FormText -ButtonText
  4. Example: MakeForm -FormText "ForInput" -ButtonText "Submit"
  5. Use: To make the PowerShell program's interactivity better.
  6. #>
  7. function MakeForm{
  8. param($FormText,$ButtonText)
  9. $null = [Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
  10. $form = New-Object Windows.Forms.Form
  11. $form.size = New-Object Drawing.Size -Arg ,
  12. $form.StartPosition = "CenterScreen"
  13. $form.Text = $FormText.toString()
  14. $textBox = New-Object Windows.Forms.TextBox
  15. $textBox.Dock = "fill"
  16. $form.Controls.Add($textBox)
  17. $button = New-Object Windows.Forms.Button
  18. $button.Text = $ButtonText
  19. $button.Dock = "Bottom"
  20. $button.add_Click(
  21. {$global:resultText = $textBox.Text;$form.Close()})
  22. $form.Controls.Add($button)
  23. [Void]$form.ShowDialog()
  24. }
  25.  
  26. echo " "
  27. echo "++++++ 添加IP ++++++"
  28. echo " "
  29. MakeForm -FormText "请输入主机IP:" -ButtonText "Submit"
  30. echo "即将配置: $resultText 到此服务器"
  31.  
  32. $wmi = Get-WmiObject win32_networkadapterconfiguration -filter "ipenabled = 'true'"
  33.  
  34. #注意: 此处是子网掩码,根据实际情况进行更改
  35. $wmi.EnableStatic("$resultText", "255.255.255.0") | Out-Null
  36.  
  37. $a = $resultText.split(".",)[]
  38. $b = $resultText.split(".",)[]
  39. $c = $resultText.split(".",)[]
  40. $d = $resultText.split(".",)[]
  41. $gateway = "$a.$b.$c.1"
  42.  
  43. $wmi.SetGateways("$gateway") | Out-Null
  44.  
  45. #注意: 此处是DNS,根据实际情况进行更改
  46. $dns = "10.23.110.201", "10.23.110.200"
  47.  
  48. $wmi.SetDNSSERVERSearchOrder($dns) | Out-Null
  49.  
  50. ipconfig /flushdns | Out-Null
  51. sleep
  52. ipconfig /flushdns | Out-Null
  53. #$CurrentyDir = Split-Path -Parent $MyInvocation.MyCommand.Definition
  54. #C:\opstools\Init_file\ChangeName.vbs
  55. netsh interface ip show config | Out-Null
  56. sleep
  57.  
  58. echo " "
  59. echo " "
  60.  
  61. echo "++++++ 检测 IP 是否连通 ++++++"
  62. ping www.baidu.com | Out-Null
  63. $rw=$?
  64. while ($rw -cnotcontains "False")
  65. {
  66. ping www.baidu.com | Out-Null
  67. $rw=$?
  68. if ("$rw" -eq "False")
  69. {
  70. echo " "
  71. echo " IP 不通,请稍后..."
  72. sleep
  73. continue
  74. }
  75. else
  76. {
  77. echo " "
  78. echo " IP is ok"
    #exit
  79. }
  80. }
  81.  
  82. echo " "
  83. echo " "
  84.  
  85. #注意:此处需要根据情况修改主机名
  86. echo "++++++ 主机名 ++++++"
  87. echo " "
  88. #rename-computer -newname "BX-AP-$c-$d"
  89.  
  90. $newname="BX-A-$c-$d"
  91. $computer=Get-WMIObject Win32_ComputerSystem
  92. $computer.Rename( $NewName ) | out-null
  93. echo $newname
  94.  
  95. #C:\opstools\Init_file\ChangeName.vbs
  96. #sleep
  97. #echo "+++++ 主机名未生成 +++++"
  98. #$result = Test-Path C:\opstools\Init_file\ComputerName.txt
  99. #while ($result -cnotcontains "False")
  100. # {
  101. # $result = Test-Path C:\opstools\Init_file\ComputerName.txt
  102. # echo " 等待输入 ComputerName"
  103. # sleep
  104. # }
  105.  
  106. echo " "
  107. echo " "
  108. #echo "+++++ 获取更改后的主机名 +++++"
  109. #$CName = Get-Content C:\opstools\Init_file\ComputerName.txt
  110. #echo $CName
  111. #echo " "
  112. #echo " "
  113. #sleep
  114.  
  115. echo "+++++ 部署 Salt +++++"
  116. Copy-Item "\\10.1.1.1\d$\opstools\salt-package" -Destination "d:\" -recurse
  117. cmd.exe /c "d:\salt-package\Salt-Minion-2016.3.0-AMD64-Setup.exe /S /master=10.1.1.1 /minion-name=$newname"
  118. #return
  119.  
  120. Get-Service -Name salt-minion
  121. sleep
  122. #Remove-Item 'C:\opstools\Init_file\ComputerName.txt'
  123. #sleep
  124.  
  125. netsh interface ip show config
  126. sleep
  127. #ping www.baidu.com
  128. Shutdown.exe -r -t

centos初始化脚本的更多相关文章

  1. CentOS 初始化脚本

    #!/bin/bash # curl https://yiyingcanfeng.github.io/centos-init.sh | bash # 可选参数base kernel python ph ...

  2. CentOS初始化脚本(未完)

    #!/bin/bash release=`` .shutdown selinux [ -e "/etc/selinux/config" ] && sed -i 's ...

  3. centos7初始化脚本(转)

    #!/bin/bash # 描述: CentOS 初始化脚本 # 加载配置文件 if [ -n "${1}" ];then /bin/} fi # 可接受配置(shell 变量格式 ...

  4. centos 7 初始化脚本

    #!/bin/bash # 时间: 2018-11-21 # 作者: HuYuan # 描述: CentOS 7 初始化脚本 # 加载配置文件 if [ -n "${1}" ];t ...

  5. bash-1 初始化CentOS系统的初始化脚本

    初始化CentOS系统的初始化脚本 #!/bin/bash # #******************************************************************* ...

  6. Centos7初始化脚本

    今天分享一个自己写的一个初始化的小脚本. 编写初始化系统要考虑到系统的版本问题,现在用的比较多的就是centos6和centos7,所以首先要判断一下系统的版本. cat /etc/redhat-re ...

  7. centos7 初始化脚本

    #!/bin/bash # 时间: 2018-11-21 # 作者: HuYuan # 描述: CentOS 7 初始化脚本 # 加载配置文件 if [ -n "${1}" ];t ...

  8. liunx用户环境初始化脚本

          liunx用户环境初始化脚本 编写生成脚本基本格式,包括作者,联系方式,版本,时间,描述等 [root@magedu ~]# vim .vimrc set ignorecase set c ...

  9. Linux初始化脚本

    以下脚本用于linux系统的初始化脚本,可以在服务器系统安装完毕之后立即执行.脚本结合各位大牛一些参数,已经在CentOS 5下通过. 使用方法:将其复制,保存为一个shell文件,比如init.sh ...

随机推荐

  1. n个点的最长公共子串(别人的模板) poj 3080

    没有理解代码.单纯记模板 题意:找最长的公共字串,长度相同就找字典序最小的(这一点wa了我13遍!!!)题解:kmp或者直接暴力列举当公共子串长度小于3时,有特判 #include<map> ...

  2. 左偏树(p4431)

    难得不是左偏树,而是思维: 这道题在做得时候,有两个性质 1.如果a是一个不下降序列,那么b[i]==a[i]时取得最优解. 2.如果a是一个严格递减序列,则取a序列的中位数x,令b[1]=b[2]= ...

  3. 【Python】 运算符

    什么是运算符? 本章节主要说明Python的运算符.举个简单的例子 4 +5 = 9 . 例子中,4 和 5 被称为操作数,"+" 称为运算符. 接下来让我们一个个来学习Pytho ...

  4. blog主题——樱花

    贮存一下,blog代码 QAQ 页脚html <!--live2d--> <script src="https://blog-static.cnblogs.com/file ...

  5. Analog power pin UPF defination

    在一个analog macro端口上,有些pin是always on的,有些是shut down的,如何描述这些pin的power属性?这是一个常见问题,驴就此机会大致描述一下常见的做法.对于这个问题 ...

  6. 读书笔记, Python - python-tricks-buffet-awesome-features

    To be a Pythonista 1. assert syntax: assert expression1 [",", expression2] 大致相当于 if __debu ...

  7. JavaScript - what is "this"? this是什么?

    https://fangyinghang.com/this-in-js/ Core func(p1, p2) // 等同于 func.call(undefined, p1, p2) this 就是ca ...

  8. jQuery的ajax方法的属性以及jsonp的随机数问题

    原文地址[ http://www.cnblogs.com/know/archive/2011/10/09/2204005.html ] <!DOCTYPE html PUBLIC "- ...

  9. Presto入门介绍

    (一)背景 MapReduce不能满足大数据快速实时adhoc查询计算的性能要求,Facebook2012年开发,2013年开源 (二)是什么 基于内存的并行计算,Facebook推出的分布式SQL交 ...

  10. UVA10600 ACM Contest and Blackout

    用prim算法求最小生成树和次小生成树~ #include<cstdio> #include<algorithm> #include<cstring> using ...