#!/bin/bash
sshs(){
#生成密钥
if [ ! -f ~/.ssh/id_rsa ];then
ssh-keygen -t rsa -P "" -f $HOME/.ssh/id_rsa
fi
#分发密钥
/usr/bin/expect <<EOF
spawn ssh-copy-id $1
expect {
"yes/no" { send "yes\r"; exp_continue }
"password:" { send "$2\r" }
}
expect eof
EOF
}
#sshs master 123456
#判断用户是否是unbtun还是
ubtun_if_cens(){
if [ -f "/etc/os-release" ]; then
# 从文件中提取 ID 字段的值
os_id=$(awk -F'=' '/^ID=/{print $2}' /etc/os-release | tr -d '"')
# 判断 ID 值来确定操作系统类型
if [ "$os_id" = "centos" ]; then
rpm -q redhat-lsb-core > /dev/null 2>&1
# 如果已安装,则输出提示信息
if [ ! $? -eq 0 ]; then
sudo yum install -y redhat-lsb-core
fi
# echo "CentOS"
elif [ "$os_id" = "ubuntu" ]; then
apt-get install redhat-lsb-core > /dev/null 2>&1
sudo apt-get install lsb-core
# echo "Ubuntu"
fi
fi
# Is this ok [y/d/N]
system=`lsb_release -a 2> /dev/null | grep "Distributor ID:" | cut -d ":" -f2`
release=`lsb_release -a 2> /dev/null|grep "Release:"|cut -d ":" -f2`
name=${system^^}
echo "${name}_${release}"
}
set_netmask(){
name_release=$(ubtun_if_cens)
name=$(echo "${name_release}" | cut -d "_" -f1)
release=$(echo "${name_release}" | cut -d "_" -f2)
get_net_file(){
get_ubtun_net_file(){
dirs=""
if [[ "${release}" < "18.04" ]];then
dirs="/etc/network/interfaces"
else
filename=$(ls /etc/netplan/|grep -v ".bak"|head -n 1)
dirs="/etc/netplan/${filename}"
fi
echo $dirs
}
get_centos_net_file(){
filename=$(ls /etc/sysconfig/network-scripts|grep -v ".bak"|head -n 1)
dirs="/etc/sysconfig/network-scripts/${filename}"
echo $dirs
}
dirs=""
if [ "${name}" == "UBUNTU" ];then
dirs=$(get_ubtun_net_file $release)
else
dirs=$(get_centos_net_file $release)
fi
echo $dirs
}
ubtun_net(){
#网卡名称
auto_name=$1
#ip地址
address=$2
#网关地址
gateway=$3
#版本
release=$4
#dir_file
dir_file=$5
the_14_1604(){
cat << EOF > $dir_file
auto ${auto_name}
iface ${auto_name} inet static
address ${address}
netmask 255.255.255.0
gateway ${gateway}
EOF
}
the_16(){
cat << EOF > $dir_file
network:
version: 2
renderer: networkd
ethernets:
${auto_name}:
dhcp4: no
addresses: [${address}/24]
gateway4: ${gateway}
nameservers:
addresses: [8.8.8.8, 8.8.4.4]
EOF
}
if [[ $release < "18.04" ]];then
$(the_14_1604 $dir_file)
sudo /etc/init.d/network restart
echo 2
else
$(the_16 $dir_file)
sudo netplan apply
echo 1
fi
echo $pr
}
centos_net(){
#网卡名称
auto_name=$1
#ip地址
address=$2
#网关地址
gateway=$3
#版本
release=$4
#dir_file
dir_file=$5
centos_6(){
echo "DEVICE=${auto_name}" > $dir_file
echo "TYPE=Ethernet">> $dir_file
echo "ONBOOT=yes">>$dir_file
echo "BOOTPROTO=static">>$dir_file
echo "IPADDR=${address}">>$dir_file
echo "NETMASK=255.255.255.0">>$dir_file
echo "GATEWAY=${gateway}">>$dir_file
echo "DNS1=8.8.8.8">>$dir_file
}
centos_7(){
uuid=$(nmcli c show|grep ${auto_name}|cut -d " " -f4)
echo "TYPE=\"Ethernet\"">$dir_file
echo "BOOTPROTO=\"static\"">>$dir_file
echo "NAME=\"${auto_name}\"">>$dir_file
echo "UUID=\"${uuid}\"">>$dir_file
echo "DEVICE=${auto_name}">>$dir_file
echo "ONBOOT=\"yes\"">>$dir_file
echo "IPADDR=${address}">>$dir_file
echo "NETMASK=255.255.255.0">>$dir_file
echo "GATEWAY=${gateway}">>$dir_file
echo "DNS1=8.8.8.8">>$dir_file
}
if [[ "${release}"<"7" ]];then
centos_6
else
centos_7
fi
service network restart
}
dir_file=$(get_net_file)
auto_name=$(grep -Eo 'eth[0-9]+|ens[0-9]+' "${dir_file}")
address=$(ip a|grep "${auto_name}"|grep inet| awk '{match($0, /[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+/); print substr($0, RSTART, RLENGTH)}'|head -n 1)
address=$1
gateway=$(ip route show |grep default|awk '{match($0, /[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+/); print substr($0, RSTART, RLENGTH)}'|head -n 1)
sudo cp ${dir_file} "${dir_file}.bak"
echo $dir_file
if [ "${name}" == "UBUNTU" ];then
ubtun_net $auto_name $address $gateway $release "${dir_file}"
else
centos_net $auto_name $address $gateway $release "${dir_file}"
fi
}
#静态网卡ip
set_netmask 192.168.12.21
静态ip设计
于 2023-09-12 09:25:24 首次发布