服务热线

136 0244 7383

Linux自动获取ip

2021-02-12 18:52:30 8230

#!/bin/bash
ping -c 2 192.168.1.111 > /dev/null

if [$? -ne 0 ] ;then
 
   service network restart

   case $? in
0)
 
ipadress = ifconfig eth0 |grep "inet a " |cut -d: -f 2 |cut -d: " " -f 1

   echo "success You get ip is ~$ipadress~" |mail root -s "success"
   

*)

   echo "failed" |mail root -s "failed"
   
esac

else

echo "You have ipaddress " |mail root -s "You have ipaddress"
fi

 

备注:可以把此脚本放在linux开几启动运行,在运行此脚本前必要的要对dhcp自动获得ip进行适当的配置