About the script
Before the server is used, it needs to be configured first 静态IP
, so leave this kind 简单重复
of work to the script to handle it, let us have more time for operation and maintenance喝茶看报刷微博
Script usage
sh ssh.sh ip.txt
ssh.sh
The name of the script, set by yourself, not a fixed itemip.txt
It is a record file of ip, password, and host name. The file name can be set by yourself and is not a fixed item. The content format is as follows:
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
- Note: The spacing between ip, password, and host name is
一个空格
the cutting command in the script, which is分隔符
also specified一个空格
, please注意文本格式
Attention item
- This script configures
单向免密
- This script configures
hosts解析
- This script modified
hostname
- This script will
重启网卡
serve - This script will be used
expect
, please install in advance - This script is currently available for
suse
,centos
,redhat
release, other distributions,后面继续更新
(my test environment is suse 12 sp3, centos 7.7, redhat 7, fedora 32) - This script will be
删除脚本所在目录下
all.template结尾的文件
, it is recommended that the script be干净的目录
executed - The following is defined in this script
固定变量
, please modify it according to your own situation:CARDNAME
For the network card name (ip a
orifconfig
view)GATEWAY
Is the gateway, theDNS1
default in the CentOS network card configuration file is and网关IP一致
NETMAST
Prefix for the subnet maskdomain_template
In the function, several commonly used ones are written国内dns服务器
, which can be modified or deleted by yourself
Script body
#!/usr/bin/env bash
PWD=$(cd $(dirname $0) ; pwd)
FILENAME=$1
CARDNAME='eth0'
GATEWAY='192.168.72.2'
NETMAST='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}/${NETMAST}\'%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=\"${NETMAST}\"%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
rm -f ${PWD}/*.template
}
# 重启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
}
main