#!/bin/bash
if ! test "a$1" == a; then export P="$1"
elif test -e /dev/urandom; then export P=$(tr -dc _A-Za-z0-9 < /dev/urandom|head -c 15)
elif which md5sum >/dev/null 2>/dev/null; then export P=$((echo $RANDOM;date)|md5sum|head -c 15)
else export P=$(echo "$RANDOM$RANDOM$RANDOM$RANDOM$RANDOM$RANDOM$RANDOM"TroubleGeneratingRandomPassword|head -c 15)
fi
cat /etc/netgear-password >/dev/null || exit 1 # unreadable
export Old="$(cat /etc/netgear-password)"
echo "$P" > /etc/netgear-password || exit 1 # unwritable
echo "Old password: $Old"
echo "New password: $P"
echo "NetgearPassword=$P&NetgearPasswordReEnter=$P&RestoreFactoryNo=0x00" | lynx -source -post-data "-auth=admin:$Old" http://192.168.0.1/goform/RgSecurity >/dev/null 2>/dev/null # (might result in auth errors when tries to fetch the result page)
if lynx -source "-auth=admin:$P" http://192.168.0.1/RgSecurity.asp | grep "value=\"$P\"" >/dev/null; then echo "Password change successful"; else echo "WARNING: password might not have been set successfully (might need to restore /etc/netgear-password if not)"; fi
