Automatically reboot a TP-LINK TD-W8960N Modem through PHP script

I recently got a bit fed up of the ADSL2+ speeds I was getting and constant line resyncing. I thought I would set up a script to automatically reboot the modem when sleeping.

It turns out that previously it was very simply to reboot TP-LINK modems, but it has got a lot more difficult recently. This page, which I initially found through googling, lead me on a wild goose chase more then anything else. It turns out it was better to start from scratch, with the comments helping me to work out the username/password hashing.

One of the advantages with my script below, is I also capture the sync speeds, and you could use that to determine if the modem should be rebooted.

Naturally in my goto PHP language. Code below, simply modify username, password and modem IP.

	$username = 'admin';
	$password = 'XXXXXX';
	$ip = '192.168.1.1';

	// code below
	$ip = "http://{$ip}/";
	
	$auth_enc =  base64_encode($username.':'.$password);
	
	date_default_timezone_set('Australia/Perth');
	$cookies = dirname(__FILE__).'/cookies-tplink.txt';
	
	//delete the existing cookie file, important
	unlink($cookies);
	
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
	curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
	curl_setopt($ch, CURLOPT_COOKIEFILE, $cookies);
	curl_setopt($ch, CURLOPT_COOKIEJAR, $cookies);
	curl_setopt($ch, CURLOPT_COOKIE, 'Authorization=Basic '.$auth_enc.';path=/');
	curl_setopt($ch, CURLOPT_REFERER, $ip);
	curl_setopt($ch, CURLOPT_URL, $ip);
	curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
	$output = curl_exec($ch);
	
	//grab the connection information
	curl_setopt($ch, CURLOPT_URL, $ip.'info.html');
	$output = curl_exec($ch);

	preg_match_all('%<td class=\\\\"dataStyle\\\\">(\d*?)</td>%im', $output, $result, PREG_PATTERN_ORDER);
	$Upstream = $result[1][0];
	$Downstream = $result[1][1];
	var_dump($Upstream);
	var_dump($Downstream);
	
	//grab session key from reboot router page
	curl_setopt($ch, CURLOPT_URL, $ip.'resetrouter.html');
	$output = curl_exec($ch);
	
	if (preg_match('/var sessionKey=\'(.*?)\';/im', $output, $regs)) {
		$sessionKey = $regs[1];
	}
	var_dump($sessionKey);
	
	//reboot the modem
	if ($sessionKey && false) {
		curl_setopt($ch, CURLOPT_URL, $ip.'rebootinfo.cgi?sessionKey='.$sessionKey);
		$output = curl_exec($ch);
		echo "Modem has been rebooted\r\n";
	}
	
	curl_close($ch);

Posted

in

by

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *