脚本:Ubuntu18-20.04 ip地址配置补充

    技术2022-07-10  151

    以下脚本更正系统潜迁移时无法找到网卡的情况

    #!/bin/bash #Name: env install #Desc: lazy to install one to one #Usage: env_config #Path: ~/bin #Update: 20200404 check() { dpkg -l | egrep -o "$name" >> temp.log for tmp1 in $(cat temp.log) do if [ $tmp1 = $name ] then echo "$name is OK" break else echo "$name install ... ... ..." sudo apt-get install $name fi done rm temp.log } echo "Ubuntu18~Ubuntu19 config ... ..." read -p "do you want to change ip static (Y/n) : " ack case $ack in y | Y ) name="net-tools" check eth0=$(ifconfig >> ip ; cat ip | egrep -o '\w+:' | sed '2,$d' | cut -d: -f1) #添加一个判断用于系统迁移无法找到网卡的情况发生 if [ "$eth0" == "lo" ] then eth0=$(ip addr | egrep "fq_codel" | egrep -o " \w+:" | cut -d":" -f1) elif [ -z "eth0" ] then echo "please check your netstat !!!" exit 1 fi net_plan=$(ls /etc/netplan) back_up="50-cloud-init.backup" echo "example : 192.168.1.3/24" read -p "please input ip :" ip read -p "please input mask:" mask read -p "please input gateway4:" gateway sed '8s/enp0s3/'"$eth0"'/' $back_up >> temp1.txt sed '10s/192.168.1.4/'"$ip"'/' temp1.txt >> temp2.txt sed '10s/24/'"$mask"'/' temp2.txt >> temp3.txt sed '11s/192.168.1.1/'"$gateway"'/' temp3.txt >> temp4.txt rm temp{1,2,3}.txt rm ip mv temp4.txt $net_plan sudo cp $net_plan /etc/netplan/ rm $net_plan sudo netplan apply echo "config static ip successful ... ..." ;; n | N ) ;; esac
    Processed: 0.036, SQL: 9