centos初始化脚本
添加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
原文:https://www.cnblogs.com/hanshanxiaoheshang/p/12377095.html