1. 程式人生 > >樹莓派檢測ip指令碼

樹莓派檢測ip指令碼

在樹莓派上執行web伺服器,但有時候開機完網路沒有啟動成功,需要重啟networking服務,所以寫了一個指令碼來檢測IP是否為192.168.x.x,如果不是則重啟networking

#!/bin/bash

while true
do
        IP="$(ip addr show eth0|grep "inet\b"|awk '{print $2}')"
        VAL_CHK=$(echo $IP|awk -F"." '{if(($1==191)&&($2==168)&&($3==1)){ printf "T"}else{printf "F";}}')
        echo $IP
        echo $VAL_CHK
        if [ "$VAL_CHK" = "T" ]
        then
                echo "IP is available."
                exit 0
        else
                echo "IP is error!"
                systemctl stop networking
                sleep 5
                systemctl start networking
                sleep 5
        fi
done