学习笔记之ubuntu修改固定IP脚本

一、shell脚本编程

二、正则表达式

三、linux修改IP的方法

#!/bin/bash

cd /etc/network/

stty erase '^?'

write_interfaces()
{
echo "auto lo
iface lo inet loopback auto eth0
iface eth0 inet static
address $
netmask 255.255.255.0
gateway $
" > interfaces
} reboot_fun()
{
echo "reboot now?(Y/N)"
read reb_in if [[ ${reb_in} = $'Y' || ${reb_in} = $'y' ]]
then
echo "reboot now"
reboot
else
echo "pls reboot if you can not ping the IP"
fi
} ping_func()
{
ping -c $set_IP && result= || result=
if [ $result -eq ]
then
{
echo "set Ip conflict"
continue
IP_INPUT=
}
fi if [ $result -eq ]
then
{
echo "IP is ok:$set_IP" write_interfaces $set_IP $GateWay
/etc/init.d/networking reload
/etc/init.d/networking restart
echo "set ok"
IP_INPUT=
}
fi
} echo "set IP into the main.cpp"
IP_INPUT= while [ $IP_INPUT -eq ]
do
echo "input IP"
read set_IP -n if [[ $set_IP =~ ^([-]{,}|[-][-]|[-][-]|[-])\.([-]{,}|[-][-]|[-][-]|[-])\.([-]{,}|[-][-]|[-][-]|[-])\.([-]{,}|[-][-]|[-][-]|[-])$ ]]
then
echo "input gateway"
read GateWay arr_IP=${set_IP//./ }
arr_IP=($arr_IP)
arr_gw=${GateWay//./ }
arr_gw=($arr_gw) for arr in
do
if [ ${arr_IP[$arr]} == ${arr_gw[$arr]} ]
then
echo "arr_IP:${arr_IP[$arr]}"
else
echo "error IP :$set_IP"
continue
fi
done ping_func
fi
done reboot_fun
上一篇:ionic3 百度地图插件定位 问题


下一篇:ubuntu修改固定ip