I'd suggest to try:
#!/bin/bash
#
ok="`ping -c 2 google.com 2>&1`"
res="$?"
if [ "$res" != "0" ] ; then
sleep 10
ok2="`ping -c 2 google.com 2>&1`"
res2="$?"
if [ "$res2" != "0" ] ; then
echo "Failed twice ... res=$res res2=$res2 ..."
echo -e "Ping1:\n$ok"
echo -e "Ping2:\n$ok2"
.... your preferred restart ...
fi
fi
#
Just for knowledge, the Network Manager is worse with wifi connections, but you can replace with Wicd that's look work better on wifi connections.