ubuntu1804修改IP地址方式及腳本

sed -rn '/((2(5[0-5]|[0-4][0-9]))|[0-1]?[0-9]{1,2})(\.((2(5[0-5]|[0-4][0-9]))|[0-1]?[0-9]{1,2})){3}/p' 50-cloud-init.yaml正則表達式

其中-rn是指 r是指使用擴展的正則表達式,n是不打印全部行,只打印匹配到的行
關鍵正則是((2(5[0-5]|[0-4][0-9]))|[0-1]?[0-9]{1,2})(\.((2(5[0-5]|[0-4][0-9]))|[0-1]?[0-9]{1,2})){3},他只會匹配合法的ip地址,像999.0.0.0這類的非ip地址不會匹配到bash

修改ip地址命令服務器

sed -ri 's/- ((2(5[0-5]|[0-4][0-9]))|[0-1]?[0-9]{1,2})(\.((2(5[0-5]|[0-4][0-9]))|[0-1]?[0-9]{1,2})){3}\/[0-9]{1,2}/- 1.2.4.6\/23/' 50-cloud-init.yaml

完整腳原本了

#!/bin/bash

if [ "$(id -u)" != "0" ]; then
   echo "只有root用戶才能運行" 1>&2
   exit 1
fi

ipreg="((2(5[0-5]|[0-4][0-9]))|[0-1]?[0-9]{1,2})(\.((2(5[0-5]|[0-4][0-9]))|[0-1]?[0-9]{1,2})){3}"
destfile=/etc/netplan/50-cloud-init.yaml
echo "-----"
echo 請按提示輸入,如不作改變,請直接回車。
echo 設置完畢後,請再次確認配置,按Y開始修改。
echo "-----"
nowipmask=`sed -rn "/- ${ipreg}\/([0-9]$|[1-2][0-9]|3[0-2])/p" $destfile`
nowipmask=${nowipmask:14}
nowip=${nowipmask%/*}
nowmask=${nowipmask#*/}

#sed -ri "s/gateway4: ${ipreg}$/gateway4: ${gateway}/" $destfile
nowgateway=`sed -rn "/gateway4: ${ipreg}/p" $destfile`
nowgateway=${nowgateway:22}

nowdnsip=`sed -rn "/- ${ipreg}$/p" $destfile`
nowdnsip=${nowdnsip:18}
echo $nowdnsip

while true; do
    echo -en "請輸入IP地址(${nowip}): \t"
    read ipaddr
    if [[ $ipaddr = "" ]];then
	ipaddr=$nowip
    fi
    if [[ $ipaddr =~ ^$ipreg$ ]]; then
        break;
    else
        echo "輸入的ip地址不合法"
    fi
done;

while true; do
    echo -en "請輸入子網掩碼(${nowmask}): \t\t"
    read mask
    if [[ $mask == "" ]];then
	mask=$nowmask
    fi
    if [[ $mask =~ ^([8-9]|[1-2][0-9]|3[0-2])$ ]]; then
        break;
    else
        echo "輸入的mask地址不合法"
    fi
done;

while true; do
    echo -en "請輸入網關(${nowgateway}): \t"
    read gateway
    if [[ $gateway = "" ]];then
	gateway=$nowgateway
    fi
    if [[ $gateway =~ ^$ipreg$ ]]; then
        break;
    else
        echo "輸入的gateway地址不合法"
    fi
done;

while true; do
    echo -en "請輸入DNS地址(${nowdnsip}): \t"
    read dnsip
    if [[ $dnsip = "" ]];then
	dnsip=$nowdnsip
    fi
    if [[ $dnsip =~ ^$ipreg$ ]]; then
        break;
    else
        echo "輸入的dnsip地址不合法"
    fi
done;

echo ""
echo -e "修改內容彙總"
echo -e "---------------------------------"
echo -e "IP地址: \t$ipaddr/$mask"
echo -e "網關地址: \t$gateway"
echo -e "DNS服務器地址: \t$dnsip"
echo "若是是遠程連過來修改ip,請千萬當心,配置錯誤會致使與服務器失聯"

check_yes_no(){
    while true; do
        read -p "$1" yn
        if [ "$yn" = "" ]; then
            yn='Y'
        fi
        case "$yn" in
            [Yy] )
                break;;
            [Nn] )
                echo "放棄..."
                exit 1;;
            * )
                echo "請輸入y或n來表明是或否";;
        esac
    done;
}

if check_yes_no "是否開始設置本機靜態IP? [Y/n] ";then
	sed -ri "s/- ${ipreg}\/([0-9]$|[1-2][0-9]|3[0-2])/- ${ipaddr}\/${mask}/" $destfile
	sed -ri "s/gateway4: ${ipreg}$/gateway4: ${gateway}/" $destfile
	sed -ri "s/- ${ipreg}$/- ${dnsip}/" $destfile
	echo "設置完成,退出。"
	echo "若是你是用ssh連過來的話,可能因IP變化致使中斷"
	echo "請使用新IP地址(${ipaddr})再次鏈接"
	# 從新應用新的配置
	sudo netplan apply
fi
相關文章
相關標籤/搜索