Type //echo -a $ip. Does it give you the correct IP? There might be a way to script a solution.