you have to do some work with shell scripting i don't know the exact syntax at the moment so i am giving a hint at this time.(just like a psudo code)
while true; do
$var1 = `ping your server`
if var1 eq "time out" then
remove previous ip (using ifconfig command)
add new ip (using ifconfig command)
one thing i want to make clear here is that in shell code if you want the output of command to be saved in a variable then you use ` operator which is present just before 1 on keyboard.