shell脚本批量配置多台主机静态ip

关于脚本

服务器使用之前,都需要先配置静态IP,那就将这种简单重复的工作,交给脚本来处理吧,让我们运维有更多的时间喝茶看报刷微博

脚本使用

  1. sh ssh.sh ip.txt
    1. ssh.sh 为脚本的名称,自行设定,不是固定项
    2. ip.txt 为ip、密码、主机名的记录文件,文件名称可以自行设定,不是固定项,内容格式如下:
192.168.72.46 123.com k8s-01
192.168.72.47 123.com k8s-02
192.168.72.48 123.com k8s-03
192.168.72.49 123.com k8s-04
192.168.72.50 123.com k8s-05
  • 注:ip、密码、主机名之间的间距为一个空格,脚本内的切割命令,指定的分隔符也是一个空格,请注意文本格式

注意项目

  1. 此脚本配置了单向免密
  2. 此脚本配置了hosts解析
  3. 此脚本修改了hostname
  4. 此脚本会重启网卡服务
  5. 此脚本会用到 expect ,请提前安装
  6. 此脚本目前适用于 susecentosredhat发行版,其他发行版,后面继续更新(我的测试环境是suse 12 sp3、centos 7.7、redhat 7、fedora 32)
  7. 此脚本会删除脚本所在目录下的所有.template结尾的文件,建议脚本放到干净的目录执行
  8. 此脚本内定义了以下固定变量,请根据自身情况进行修改:
    1. CARDNAME为网卡名称( ip a 或者 ifconfig 查看)
    2. GATEWAY 为网关,CentOS网卡配置文件内的DNS1默认为和网关IP一致
    3. NETMAST 为子网掩码前缀
    4. domain_template 函数内,写了几个常用的国内dns服务器,可以自行修改或删减

脚本正文

#!/usr/bin/env bash
PWD=$(cd $(dirname $0) ; pwd)
FILENAME=$1
CARDNAME='eth0'
GATEWAY='192.168.72.2'
NETMASK='24'

# 检查linux发行版,suse和centos的网卡配置文件所在目录是不一样的
function check_os (){
RELEASE=$(grep PRETTY_NAME /etc/os-release | awk -F '"' '{print $2}' | awk '{print $1}')
if [ ${RELEASE} == "SUSE" ]
then
    printf "[\e[0;35mINFO\e[0m]\e[0;35mYour system is ${RELEASE}\e[0m\n"
    ifcfg_dir='/etc/sysconfig/network'
fi
if [ ${RELEASE} == "CentOS" ] || [ ${RELEASE} == "Red" ] || [ ${RELEASE} == "Fedora" ]
then
    printf "[\e[0;35mINFO\e[0m]\e[0;35mYour system is ${RELEASE}\e[0m\n"
    ifcfg_dir='/etc/sysconfig/network-scripts/'
fi
}

# 检查ip.txt文件是否创建了,以及执行方式是否正确
function check_file (){
if [ ! -n "${FILENAME}" ]; then
    printf "[\e[0;31mERROR\e[0m]\e[0;35m   No host ip address account file supplied !!!\e[0m\n"
    printf "[\e[0;35mUsage\e[0m]   $0 [\e[0;35mip.txt\e[0m]\n"
    exit 1
fi

IPADDRESS=()
PASSWORDS=()
HOSTNAMES=()

while read line
do
    ip_addres=$(echo ${line} | cut -d " " -f1)
    pass_word=$(echo ${line} | cut -d " " -f2)
    host_name=$(echo ${line} | cut -d " " -f3)

    IPADDRESS[${#IPADDRESS[*]}]=${ip_addres}
    PASSWORDS[${#PASSWORDS[*]}]=${pass_word}
    HOSTNAMES[${#HOSTNAMES[*]}]=${host_name}

done < ${FILENAME}
}

# 配置ssh免密
function ssh_auth () {
[ -d /root/.ssh ] && mv /root/.ssh{,.bak.`date +%F`}
ssh-keygen -t rsa -P "" -f /root/.ssh/id_rsa -q > /dev/null 2>&1

for ((i = 0; i < ${#IPADDRESS[@]}; i++))
do
    ip_addres=${IPADDRESS[$i]}
    pass_word=${PASSWORDS[$i]}

    printf "[\e[0;35mINFO\e[0m]\e[0;35m${ip_addres}\e[0m\n"
    expect -c "
    spawn ssh-copy-id -i /root/.ssh/id_rsa.pub root@${ip_addres}
        expect {
                \"*yes/no*\" {send \"yes\r\"; exp_continue}
                \"*assword*\" {send \"${pass_word}\r\"; exp_continue}
                \"*assword*\" {send \"${pass_word}\r\";}
               }"
done
}

# 创建hosts文件模板
function hosts_template () {
for ((i = 0; i < ${#IPADDRESS[@]}; i++))
do
    ip_addres=${IPADDRESS[$i]}
    host_name=${HOSTNAMES[$i]}

    echo "${ip_addres} ${host_name}" >> ${PWD}/hosts.template
done
printf "[\e[0;35mINFO\e[0m]\e[0;35mhosts_template Completed\e[0m\n"
}

# 生成网卡配置文件模板
function ifcfg_template () {
if [ ${RELEASE} == "SUSE" ]
then
    for ((i = 0; i < ${#IPADDRESS[@]}; i++))
    do
        ip_addres=${IPADDRESS[$i]}

        sed -e "s%^IPADDR=.*%IPADDR=\'${ip_addres}/${NETMASK}\'%g" \
            /etc/sysconfig/network/ifcfg-${CARDNAME} \
            > ${PWD}/ifcfg-${CARDNAME}-${ip_addres}.template
    done
    printf "[\e[0;35mINFO\e[0m]\e[0;35mifcfg_template Completed\e[0m\n"
fi

if [ ${RELEASE} == "CentOS" ] || [ ${RELEASE} == "Red" ] || [ ${RELEASE} == "Fedora" ]
then
cat > ${PWD}/ifcfg-${CARDNAME}.template <<EOF
TYPE="Ethernet"
PROXY_METHOD="none"
BROWSER_ONLY="no"
BOOTPROTO="none"
DEFROUTE="yes"
IPV4_FAILURE_FATAL="no"
NAME="${CARDNAME}"
DEVICE="${CARDNAME}"
ONBOOT="yes"
IPADDR=""
PREFIX=""
GATEWAY=""
DNS1=""
EOF
    for ((i = 0; i < ${#IPADDRESS[@]}; i++))
    do
        ip_addres=${IPADDRESS[$i]}

        sed -e "s%^IPADDR=.*%IPADDR=\"${ip_addres}\"%g" \
            -e "s%^PREFIX=.*%PREFIX=\"${NETMASK}\"%g" \
            -e "s%^GATEWAY=.*%GATEWAY=\"${GATEWAY}\"%g" \
            -e "s%^DNS1=.*%DNS1=\"${GATEWAY}\"%g" \
            -e "s%^DEVICE=.*%DEVICE=\"${CARDNAME}\"%g" \
            -e "s%^NAME=.*%NAME=\"${CARDNAME}\"%g" ${PWD}/ifcfg-${CARDNAME}.template \
            > ${PWD}/ifcfg-${CARDNAME}-${ip_addres}.template
    done
    printf "[\e[0;35mINFO\e[0m]\e[0;35mifcfg_template Completed\e[0m\n"
fi
}

# 生成网关配置文件模板
function ifroute_template () {
if [ ${RELEASE} == "SUSE" ]
then
    echo "default ${GATEWAY} - ${CARDNAME}" > ${PWD}/ifroute-${CARDNAME}.template
fi
printf "[\e[0;35mINFO\e[0m]\e[0;35mifroute_template Completed\e[0m\n"
}

# 生成DNS配置文件模板
function domain_template () {
cat > ${PWD}/resolv.conf.template <<EOF
nameserver 101.6.6.6
nameserver 223.5.5.5
nameserver 119.29.29.29
nameserver 180.76.76.76
nameserver 114.114.114.114
EOF
printf "[\e[0;35mINFO\e[0m]\e[0;35mdomain_template Completed\e[0m\n"
}

# 分发模板文件到其他节点
function scp_template () {
for ((i = 0; i < ${#IPADDRESS[@]}; i++))
do
    ip_addres=${IPADDRESS[$i]}
    host_name=${HOSTNAMES[$i]}

    if [ ${RELEASE} == "SUSE" ]
    then
        scp ${PWD}/hosts.template ${ip_addres}:/etc/ > /dev/null
        scp ${PWD}/ifcfg-${CARDNAME}-${ip_addres}.template ${ip_addres}:${ifcfg_dir}/ifcfg-${CARDNAME} > /dev/null
        scp ${PWD}/ifroute-${CARDNAME}.template ${ip_addres}:${ifcfg_dir}/ifroute-${CARDNAME} > /dev/null
        scp ${PWD}/resolv.conf.template ${ip_addres}:/etc/ > /dev/null
        ssh root@${ip_addres} "cat /etc/hosts.template >> /etc/hosts && rm -f /etc/hosts.template"
        ssh root@${ip_addres} "cat /etc/resolv.conf.template >> /etc/resolv.conf && rm -f /etc/resolv.conf.template"
        ssh root@${ip_addres} "hostnamectl set-hostname --static ${host_name}"
        printf "[\e[0;35mINFO\e[0m]\e[0;35m${ip_addres} scp_template Completed\e[0m\n"
    fi

        if [ ${RELEASE} == "CentOS" ] || [ ${RELEASE} == "Red" ] || [ ${RELEASE} == "Fedora" ]
    then
        scp ${PWD}/hosts.template ${ip_addres}:/etc/ > /dev/null
        scp ${PWD}/ifcfg-${CARDNAME}-${ip_addres}.template ${ip_addres}:${ifcfg_dir}/ifcfg-${CARDNAME} > /dev/null
        scp ${PWD}/resolv.conf.template ${ip_addres}:/etc/ > /dev/null
        ssh root@${ip_addres} "cat /etc/hosts.template >> /etc/hosts && rm -f /etc/hosts.template"
        ssh root@${ip_addres} "cat /etc/resolv.conf.template >> /etc/resolv.conf && rm -f /etc/resolv.conf.template"
        ssh root@${ip_addres} "hostnamectl set-hostname --static ${host_name}"
        printf "[\e[0;35mINFO\e[0m]\e[0;35m${ip_addres} scp_template Completed\e[0m\n"
    fi
done

}

# 重启network服务
function restart_network () {
if [ ${RELEASE} == "SUSE" ]
then
    for ((i = 0; i < ${#IPADDRESS[@]}; i++))
    do
        ip_addres=${IPADDRESS[$i]}

        ssh root@${ip_addres} "systemctl restart network && \
                               ping www.baidu.com -c 1 | awk 'NR == 2'"
        printf "[\e[0;35mINFO\e[0m]\e[0;35m${ip_addres} restart_network Completed\e[0m\n"
    done
fi

if [ ${RELEASE} == "CentOS" ] || [ ${RELEASE} == "Red" ] || [ ${RELEASE} == "Fedora" ]
then
    for ((i = 0; i < ${#IPADDRESS[@]}; i++))
    do
        ip_addres=${IPADDRESS[$i]}

        ssh root@${ip_addres} "systemctl restart NetworkManager && \
                               ping www.baidu.com -c 1 | awk 'NR == 2'"
        printf "[\e[0;35mINFO\e[0m]\e[0;35m${ip_addres} restart_network Completed\e[0m\n"
    done
fi
}

function main () {
    check_os
    check_file
    hosts_template
    ifcfg_template
    ifroute_template
    domain_template
    ssh_auth
    scp_template
    restart_network
    rm -f ${PWD}/*.template
}

main
posted @ 2021-02-25 22:57  月巴左耳东  阅读(562)  评论(0编辑  收藏  举报