一键修改ip shell脚本

点击查看隐藏内容🎁
#!/bin/bash
#by fffmo.com
#change network ip  
NET_FILE="/etc/sysconfig/network-scripts"
NETWORK_DIR="ifcfg-eth0"
cd $NET_FILE/
#change ip static for static:
grep "dhcp" $NETWORK_DIR
if [ $? -ne 0 ];then
sed -i s/^IPADDR/#IPADDR/g $NETWORK_DIR
#read -p "Please enter ip Address,example 192.168.1.18 ip:"  IPADDR
echo "IPADDR=$10.30.8.80">>$NETWORK_DIR
echo "NETMASK=255.255.255.0">>$NETWORK_DIR
 echo "GATEWAY=192.168.1.254">>$NETWORK_DIR
service network restart
else
#change ip dhcp for static
sed -i s/dhcp/static/g $NETWORK_DIR
sed -i s/ONBOOT=no/ONBOOT=yes/g $NETWORK_DIR
read -p "Please enter ip Address,example 192.168.1.18 ip:"   IPADDR
cat>>$NET_FILE/$NETWORK_DIR <<EOF
IPADDR=$IPADDR
NETMASK=255.255.255.0
GATEWAY=192.168.1.1
EOF
service network restart
fi