这是CentOS VM的(可能过于简单)脚本,可以满足您的需求。我建议将此脚本放在/ usr / local / sbin中,因为它不是系统脚本,并且由root运行。
#!/bin/bash
# nettest.sh
# Get the gateway address from the routing table
gateway=$(netstat -rn | awk '/^0.0.0.0/ {print $2}')
# Try and ping the gateway
ping -c 1 ${gateway}
# If the ping succeeds, exit.
if [ $? == 0 ]; then
exit
# Otherwise take down and bring up the network interface
else
ifdown eth0
ifup eth0
exit
fi
完成此操作后,将此条目放置在crontab根文件中或中/etc/cron.d/nettest.cron
,您将被设置为:
# Call nettest.sh every 5 minutes
*/5 * * * * /usr/local/sbin/nettest.sh 2>&1> /dev/null