Quantcast
Channel: VOIP-info.org Wiki Changes
Viewing all articles
Browse latest Browse all 5767

PHP Code for provisioning and rebooting Grandstreams

$
0
0
Based on the current examples with Bash scripts, Perl and LUA code on the site, I decided to contribute some PHP snippets which provide various functions:

Reboot for GXP2120 and GXP2140:



	$pass = "admin";
	// generate URLs
	$login_url = "http://" . $ip . "/cgi-bin/dologin";
	$reboot_url = "http://" . $ip . "/cgi-bin/api-sys_operation";

	// curl object
	$ch = curl_init();
		
	// set curl to use cookie file
	$ckfile = tempnam("/tmp", "CURLCOOKIE");
	curl_setopt($ch, CURLOPT_COOKIEJAR, $ckfile);
	//set the url, number of POST vars, POST data
	curl_setopt($ch, CURLOPT_URL, $login_url);
	curl_setopt($ch, CURLOPT_POST, 1);
	curl_setopt($ch, CURLOPT_POSTFIELDS, "&password=" . $pass);
	curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
	// get 'sid' as a string
	$response = curl_exec($ch);
	preg_match('/sid" : "([A-z0-9]*)/', $response, $sid);
	$sid = $sid[1];
	// now that we have the sid, send the reboot command
	curl_setopt($ch, CURLOPT_URL, $reboot_url);
	curl_setopt($ch, CURLOPT_POST, 1);
	curl_setopt($ch, CURLOPT_POSTFIELDS, "&request=REBOOT&sid=" . $sid);
	$response = curl_exec($ch);
	curl_close($ch);



Reboot for the GXP2000:



	$pass = "admin";
	// generate URLs
	$login_url = "http://" . $ip . "/dologin.htm";
	$reboot_url = "http://" . $ip . "/rs.htm";

	// curl object
	$ch = curl_init();
	
	// set curl to use cookie file
	$ckfile = tempnam("/tmp", "CURLCOOKIE");
	curl_setopt($ch, CURLOPT_COOKIEJAR, $ckfile);
	//set the URL, number of POST vars, POST data
	curl_setopt($ch, CURLOPT_URL, $login_url);
	curl_setopt($ch, CURLOPT_POST, 1);
	curl_setopt($ch, CURLOPT_HEADER, 1);
	curl_setopt($ch, CURLOPT_POSTFIELDS, "P2=" . $pass . "&Login=Login&gnkey=0b82");
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0);
	curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
	curl_setopt($ch, CURLOPT_MAXREDIRS, 3);
	curl_setopt($ch, CURLOPT_REFERER, str_replace('dologin', 'login', $login_url));
	curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.13) Gecko/20101203 Firefox/3.6.13 ( .NET CLR 3.5.30729)");

	// get 'sid' cookie by logging in
	$response = curl_exec($ch);
	// now that we have the sid cookie, send the reboot command
	curl_setopt($ch, CURLOPT_URL, $reboot_url);
	curl_setopt($ch, CURLOPT_POST, false);
	curl_setopt($ch, CURLOPT_HEADER, 1);
	curl_setopt($ch, CURLOPT_COOKIEFILE, $ckfile);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0);
	curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
	curl_setopt($ch, CURLOPT_MAXREDIRS, 3);
	$response = curl_exec($ch);
	curl_close($ch);



Provisioning (plain XML) for GXP2120 and GXP2140:

-- This example assumes you have a database with tables named after the configuration parameters. If not, substitute the database part with reading a textfile of params instead or get creative...


	// clear any previous output
	ob_end_clean();

	// set headers for filetype
	header("Content-type: text/xml");
	header("Content-Disposition: \"inline; filename=cfg" . $mac . ".xml");
	// print XML header gxp21xx
	print "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\r\n";
	print "<gs_provision version=\"1\">\r\n";
	print "\t<mac>" . $mac . "</mac>\r\n";
	print "\t<config version=\"1\">\r\n";

	$offset = 7;
	$total_column = ($sth->columnCount() -1);
	$data = $sth->fetch();

	for ($counter = $offset; $counter <= $total_column; $counter ++) {
		$meta = $sth->getColumnMeta($counter);
		// only print configured parameters
		if (strlen($data[$counter]) > 0) {
			$value = str_replace('\r', '', $data[$counter]);
			$value = str_replace('\n', '', $value);
			print "\t\t<" . $meta['name'] . ">" . $value . "</" . $meta['name'] . ...

Viewing all articles
Browse latest Browse all 5767

Trending Articles