centos初始化脚本

centos初始化脚本

扩容

#!/bin/bash
#下述下个变量需要根据实际情况进行修改

disk="/dev/sdb"
partition="$disk"1
num=190

help ()
{
    echo  ' ====================================================================='
    echo  ' The script has three variables: <<disk>>、<<partition>> and <<num>>'
    echo  ' If variables are     defined, select < y > to execute'
    echo  ' If variables are not defined, select < q > to exit'
    echo  " "
    echo  " The disk is $disk;"
    echo  " The part is $partition;"
    echo  " The num  is $num;"
    echo  ' ====================================================================='
}
help

choose() {
read -p "Please enter your choice
    y  executing the script;
    q  exit the script;
       Please input: " choice
    case $choice in
        q)
          echo " "
          echo "No initialize the disk, exit..." ;;
        y)
          echo "Initialize the disk..."
          echo " "
          kuorong  ;;
        *)
          echo " "
          echo " "
          echo "Please input y or q"
          choose ;;
    esac
}
choose

kuorong() {
echo "n
p
1


t
8e
w
" | fdisk /dev/sdb &>/dev/null

#判断分区是否成功
a="`fdisk -l | grep /dev/sdb1 | grep 8e | wc -l`"
if [ $a -ge 1 ]; then
   echo "分区成功,即将扩容;"
   sleep 3
else
   echo "分区失败,即将退出;"
   sleep 3
   exit
fi

#创建物理PV,并进行扩容
#确定VG Name
vgname="`vgdisplay | grep "VG Name" | awk '{print $3}'`"
#定义需要扩容的目录
contents="`df -h | grep /dev/mapper/centos-root | awk '{print $1}'`"

b="`cat /etc/fstab | grep $contents | grep "xfs" | wc -l`"
if [ $b -ge 1 ]; then
   echo "系统格式是xfs,可扩展;"
   sleep 3
else
   echo "系统格式不对,即将退出;"
   sleep 3
   exit
fi

echo "pvcreate $partition
sleep 2
vgextend $vgname $partition
sleep 2
lvextend -L +"$num"G $contents
sleep 2
pvdisplay
quit
" | lvm &>/dev/null
sleep 3
xfs_growfs /dev/mapper/centos-root

echo " "
echo " "

vgdisplay | grep "VG Size"

echo " "
echo " "

df -h
}

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

#!/bin/bash
#

ip=$1
hostname=$2

if [ -z $ip ] || [ -z $hostname ]; then
   echo " "
   echo "Sorry, IP or host is null"
   echo "# The format : bash set.sh ip hostname"
   echo "# For example: bash set.sh 192.168.72.13 node1"
   echo " "
   exit
fi

function check_ip() {
    VALID_CHECK=$(echo $ip|awk -F. '$1<=255&&$2<=255&&$3<=255&&$4<=255{print "yes"}')
    if echo $ip|egrep "^[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}$">/dev/null; then
        if [ ${VALID_CHECK:-no} == "yes" ]; then
            echo "ip $ip available."
        else
            echo "ip $ip not available!"
            exit 1
        fi
    else
        echo "ip format error!"
        exit 1
    fi
}

ndir="/etc/sysconfig/network-scripts/ifcfg-ens192"
ob="`grep "ONBOOT" $ndir`"
ipaddr="`grep "IPADDR" $ndir`"
gateway="`grep "GATEWAY" $ndir`"
gt="`echo ${ip%.*}`"
dns1="`grep "DNS1" $ndir`"
dns2="`grep "DNS2" $ndir`"
nt="`grep "NETMASK" $ndir`"

sed -i "s/$ob/ONBOOT=yes/g" $ndir
sed -i "s/$ipaddr/IPADDR=$ip/g" $ndir
sed -i "s/$gateway/GATEWAY=$gt.1/g" $ndir
sed -i "s/$dns1/DNS1=8.8.8.8/g" $ndir
sed -i "s/$dns2/DNS2=114.114.114.114/g" $ndir
sed -i "s/$nt/NETMASK=255.255.255.0/g" $ndir

systemctl restart network

ping -c 2 -W 2 www.baidu.com &>/dev/null
    if [ $? != 0 ]; then
        echo " "
        echo " "
        echo "The $ip is unavailable, Please check again......"
        echo "The $ip is unavailable, Please check again......"
        echo "The $ip is unavailable, Please check again......"
        echo " "
        echo " "
        exit 1
    else
        echo " "
        echo " "
        echo "The $ip is OK !!!"
        echo "The $ip is OK !!!"
        echo "The $ip is OK !!!"
        echo " "
        echo " "
    fi

hostnamectl --static set-hostname $hostname

echo "The name is `hostname`"

echo " "
echo " "

disk="/dev/sdb"
mpath="/opt/data"

Format_disk() {
  echo "n
p
1


w
" | fdisk $disk &>/dev/null && mkfs.ext4 "$disk"1  &>/dev/null

  #mkdir $mpath
  mount "$disk"1 $mpath
  UUID=`blkid "$disk"1 | awk '{print $2}'`
  echo "$UUID $mpath ext4 defaults 0 0" >> /etc/fstab
  source /etc/fstab &>/dev/null
  df -T | grep "$disk"1
  mount | grep "$disk"1
}

Judge() {
  fnum="`fdisk -l | grep /dev/sdb | wc -l`"
  if [ $fnum -ge 1 ]; then
     mkdir -p $mpath
     echo "Initialize the disk $disk, and mount $mpath"
     read -p "Please enter the number
       0  No initialize the disk and exit;
       1  Initialize the disk;
            Please input: " num
     case $num in
         0)
           echo " "
           echo "No initialize the disk, exit..." ;;
         1)
           echo "Initialize the disk"
           sleep 2        
           echo " "
           Format_disk  ;;
         *)
           echo " "
           echo " "
           echo "Please input 0 or 1"
           Judge ;;
     esac
  else
     echo "The server does not have this disk: $disk"
  fi
}

Judge
bash

版本2

<#
    Intro: This function will display a form to communicate with the user.
    Input: -FormText -ButtonText
    Example: MakeForm -FormText "ForInput" -ButtonText "Submit"
    Use: To make the PowerShell program's interactivity better.
#>
function MakeForm{
    param($FormText,$ButtonText)
    $null = [Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
    $form = New-Object Windows.Forms.Form
    $form.size = New-Object Drawing.Size -Arg 400,80
    $form.StartPosition = "CenterScreen"
    $form.Text = $FormText.toString()
    $textBox = New-Object Windows.Forms.TextBox
    $textBox.Dock = "fill"
    $form.Controls.Add($textBox)
    $button = New-Object Windows.Forms.Button
    $button.Text = $ButtonText
    $button.Dock = "Bottom"
    $button.add_Click(
    {$global:resultText = $textBox.Text;$form.Close()})
    $form.Controls.Add($button)
    [Void]$form.ShowDialog()
}

echo " "
echo "++++++ 添加IP ++++++"
echo " "
MakeForm -FormText "请输入主机IP:" -ButtonText "Submit"
echo "即将配置: $resultText 到此服务器"

$wmi = Get-WmiObject win32_networkadapterconfiguration -filter "ipenabled = 'true'"

#注意: 此处是子网掩码,根据实际情况进行更改
$wmi.EnableStatic("$resultText", "255.255.255.0")  | Out-Null

$a = $resultText.split(".",4)[0]
$b = $resultText.split(".",4)[1]
$c = $resultText.split(".",4)[2]
$d = $resultText.split(".",4)[3]
$gateway = "$a.$b.$c.1"

$wmi.SetGateways("$gateway") | Out-Null

#注意: 此处是DNS,根据实际情况进行更改
$dns = "10.1.1.1", "10.1.1.1"

$wmi.SetDNSSERVERSearchOrder($dns) | Out-Null

ipconfig /flushdns  | Out-Null
sleep 1
ipconfig /flushdns  | Out-Null
#$CurrentyDir = Split-Path -Parent $MyInvocation.MyCommand.Definition
#C:opstoolsInit_fileChangeName.vbs
netsh interface ip show config  | Out-Null
sleep 1
#ping www.baidu.com

echo " "
echo " "

#注意:此处需要根据情况修改主机名
echo "++++++ 更改主机名 ++++++"
echo " "
#rename-computer -newname "BX-A-$c-$d"

$newname="BX-A-$c-$d"
$computer=Get-WMIObject Win32_ComputerSystem
$computer.Rename( $NewName ) | out-null

#C:opstoolsInit_fileChangeName.vbs
#sleep 5
#echo "+++++ 主机名未生成 +++++"
#$result = Test-Path C:opstoolsInit_fileComputerName.txt
#while ($result -cnotcontains "False")
#    {
#       $result = Test-Path C:opstoolsInit_fileComputerName.txt
#       echo "    等待输入 ComputerName"
#       sleep 3
#    }

echo " "
echo "+++++ 获取更改后的主机名 +++++"
#$CName = Get-Content C:opstoolsInit_fileComputerName.txt
#echo $CName
#echo " "
#echo " "
#sleep 1


echo "+++++ 部署 Salt +++++"
Copy-Item "\10.192.2.1d$opstoolssalt-package" -Destination "d:" -recurse
cmd.exe /c "d:salt-packageSalt-Minion-2016.3.0-AMD64-Setup.exe /S /master=10.3.2.2 /minion-name=$newname"
#return

Get-Service -Name salt-minion
sleep 1
#Remove-Item 'C:opstoolsInit_fileComputerName.txt'
#sleep 1

netsh interface ip show config
sleep 1
ping www.baidu.com
Shutdown.exe -r -t 1

 第三版

<#
    Intro: This function will display a form to communicate with the user.
    Input: -FormText -ButtonText
    Example: MakeForm -FormText "ForInput" -ButtonText "Submit"
    Use: To make the PowerShell program's interactivity better.
#>
function MakeForm{
    param($FormText,$ButtonText)
    $null = [Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
    $form = New-Object Windows.Forms.Form
    $form.size = New-Object Drawing.Size -Arg 400,80
    $form.StartPosition = "CenterScreen"
    $form.Text = $FormText.toString()
    $textBox = New-Object Windows.Forms.TextBox
    $textBox.Dock = "fill"
    $form.Controls.Add($textBox)
    $button = New-Object Windows.Forms.Button
    $button.Text = $ButtonText
    $button.Dock = "Bottom"
    $button.add_Click(
    {$global:resultText = $textBox.Text;$form.Close()})
    $form.Controls.Add($button)
    [Void]$form.ShowDialog()
}

echo " "
echo "++++++ 添加IP ++++++"
echo " "
MakeForm -FormText "请输入主机IP:" -ButtonText "Submit"
echo "即将配置: $resultText 到此服务器"

$wmi = Get-WmiObject win32_networkadapterconfiguration -filter "ipenabled = 'true'"

#注意: 此处是子网掩码,根据实际情况进行更改
$wmi.EnableStatic("$resultText", "255.255.255.0")  | Out-Null

$a = $resultText.split(".",4)[0]
$b = $resultText.split(".",4)[1]
$c = $resultText.split(".",4)[2]
$d = $resultText.split(".",4)[3]
$gateway = "$a.$b.$c.1"

$wmi.SetGateways("$gateway") | Out-Null

#注意: 此处是DNS,根据实际情况进行更改
$dns = "10.23.110.201", "10.23.110.200"

$wmi.SetDNSSERVERSearchOrder($dns) | Out-Null

ipconfig /flushdns  | Out-Null
sleep 1
ipconfig /flushdns  | Out-Null
#$CurrentyDir = Split-Path -Parent $MyInvocation.MyCommand.Definition
#C:opstoolsInit_fileChangeName.vbs
netsh interface ip show config  | Out-Null
sleep 1

echo " "
echo " "

echo "++++++ 检测 IP 是否连通 ++++++"
ping www.baidu.com | Out-Null
$rw=$?
while ($rw -cnotcontains "False")
    {
       ping www.baidu.com | Out-Null
       $rw=$?
       if ("$rw" -eq "False")
       {
           echo " "
           echo "    IP 不通,请稍后..."
           sleep 2
           continue
       }
       else
       {
           echo " "
           echo "    IP is ok"
#exit } } echo " " echo " " #注意:此处需要根据情况修改主机名 echo "++++++ 主机名 ++++++" echo " " #rename-computer -newname "BX-AP-$c-$d" $newname="BX-A-$c-$d" $computer=Get-WMIObject Win32_ComputerSystem $computer.Rename( $NewName ) | out-null echo $newname #C:opstoolsInit_fileChangeName.vbs #sleep 5 #echo "+++++ 主机名未生成 +++++" #$result = Test-Path C:opstoolsInit_fileComputerName.txt #while ($result -cnotcontains "False") # { # $result = Test-Path C:opstoolsInit_fileComputerName.txt # echo " 等待输入 ComputerName" # sleep 3 # } echo " " echo " " #echo "+++++ 获取更改后的主机名 +++++" #$CName = Get-Content C:opstoolsInit_fileComputerName.txt #echo $CName #echo " " #echo " " #sleep 1 echo "+++++ 部署 Salt +++++" Copy-Item "\10.1.1.1d$opstoolssalt-package" -Destination "d:" -recurse cmd.exe /c "d:salt-packageSalt-Minion-2016.3.0-AMD64-Setup.exe /S /master=10.1.1.1 /minion-name=$newname" #return Get-Service -Name salt-minion sleep 2 #Remove-Item 'C:opstoolsInit_fileComputerName.txt' #sleep 1 netsh interface ip show config sleep 2 #ping www.baidu.com Shutdown.exe -r -t 1
原文地址:https://www.cnblogs.com/hanshanxiaoheshang/p/12377095.html