Activation, désactivation du wifi sur un D-Link DSL-2640B via PHP

Un script PHP en ligne de commande pour activer // désactiver le wifi sur un modem D-Link DSL-2640B. Cela permet d’illustrer un peu l’intérêt de faire du scripting …

Je ne ferais pas de commentaires, mais n’hésitez pas à poser des questions …

#!/usr/bin/php
<?php
 
//Variable definition
//
//router
$login = 'admin';
$password = 'admin';
$ip = '192.168.1.1';
 
//
//wifi
$wifi = array(
	// /!\ don't touch this /!\
	'wlSsidIdx'	=> 0,
	'wlEnbl'	=> 1,
	'wlCountry'	=> 'GB',
	'wlBasicRate'	=> '',
 
	//ssid name
	'wlSsid'	=> 'monSuperRéseauxWifi',
	//0 = visible, 1 = hide
	'wlHide'	=> 0,
	//number of chanel or 0 for auto selection
	'wlChannel'	=> 1,
	//Transmission Speed
	'wlRate'	=> 0,
	//wifi mode 0=802.11b only, 1=Mixed 802.11g and 802.11b, 4=802.11g only
	'wlgMode'	=> 4,
 
	'wlWpaGTKRekey'	=> 750,
	'wlWpa'			=> 'aes',
	'wlAuthMode'	=> 'psk2',
	//authentification mode : 0 = none, 1 = wep, 2 = auto (wpa or wpa2), 3 = wpa2 only, 4 = wpa only
	'wlAuth'	=> 0,
	'wlWpaPsk'	=> 'masuperclefdelamortquitue',
	'wlWep'		=> 'disabled',
	'wlPreauth'	=> 0
);
 
/////////////////////
 
class cli
{
	const LINE_BREAK = "\r\n";
	const LINE_NO_BREAK = '';
	const LINE_RETURN = "\r";
	const LINE_NOTHING = ' ';
 
	protected $_TEMP = 250000;//1/4 of seconds
	protected $_MULTIPLICATOR = 4; //1/4 -> 4
 
	protected $_LENGTH = 60;
 
	protected function _write_empty_space($lng)
	{
		while($lng < $this->_LENGTH)
		{
			echo ' ';
			$lng++;
		}
	}
 
	protected function _echo($message, $break)
	{
		echo $message, $break;
		if ($break == self::LINE_NO_BREAK)
		{
			$this->_write_empty_space(strlen($message));
		}
	}
 
	protected function _sleep($second)
	{
		$second = $second * $this->_MULTIPLICATOR;
		$i = 0;
		$j = 0;
		while ($i < $second)
		{
			usleep($this->_TEMP);
 
			if ($i % 4)
			{
				$this->_echo('.', self::LINE_NOTHING);
				$i++;
			}
			else
			{
				if($j % 2)
				{
					$this->_echo('', self::LINE_RETURN);
					$this->_write_empty_space(0);
					$this->_echo('', self::LINE_RETURN);
				}
				else
				{
					$this->_echo('Please wait', self::LINE_NOTHING);
					$i++;
				}
				$j++;
			}
		}
		$this->_echo('', self::LINE_RETURN);
	}
}
 
class wifi extends cli
{
	protected $_ch;
	protected $_baseUrl;
 
	public function __construct($wifi, $ip, $login, $password, $activation)
	{
		$this->_baseUrl = 'http://' . $ip . '/';
 
		$this->_ch = curl_init();
		curl_setopt($this->_ch, CURLOPT_HEADER, false);
		curl_setopt($this->_ch, CURLOPT_RETURNTRANSFER, true);
 
		$this->_auth($login, $password);
 
		$activation = intval($activation);
 
		if ($activation == 1)
		{
			$this->_activation($wifi);
		}
		else
		{
			$this->_disactivation($wifi);
		}
	}
 
	public function __destruct()
	{
		curl_close($this->_ch);
	}
 
	protected function _activation($wifi)
	{
		$this->_echo('Activation ...', self::LINE_NO_BREAK);
 
		$wifi['wlBasicRate'] = ($wifi['wlgMode'] == 4) ? 'wifi2' : 'default';
 
		$this->_doQuery($this->_baseUrl . 'wirelesssetting.wl?' . http_build_query($wifi, '', '&'));
		$this->_sleep(5);
	}
 
	protected function _disactivation($wifi)
	{
		$this->_echo('Disactivation ...', self::LINE_NO_BREAK);
 
		$wifiDown = array(
			'wlEnbl'	=> 0,
			'wlSsidIdx'	=> $wifi['wlSsidIdx'],
			'wlCountry'	=> $wifi['wlCountry'],
		);
 
		$this->_doQuery($this->_baseUrl . 'wirelesssetting.wl?' . http_build_query($wifiDown, '', '&'));
		$this->_sleep(5);
	}
 
	protected function _doQuery($url, $post = '')
	{
		curl_setopt($this->_ch, CURLOPT_URL, $url);
 
		if (!empty($post))
		{
			curl_setopt($this->_ch, CURLOPT_POST, true);
			curl_setopt($this->_ch, CURLOPT_POSTFIELDS, $post);
		}
		else
		{
			curl_setopt($this->_ch, CURLOPT_HTTPGET, true);
		}
 
		if(curl_exec($this->_ch) === false)
		{
			$this->_echo('', self::LINE_BREAK);
			$this->_echo('Error Curl : ' . curl_error($this->ch), self::LINE_BREAK);
		}
		else
		{
			$this->_echo('DONE', self::LINE_BREAK);
		}
	}
 
	protected function _auth($login, $password)
	{
		$this->_echo('Authentification ...', self::LINE_NO_BREAK);
 
		$post = array(
			'username'	=> $login,
			'password'	=> $password
		);
 
		$this->_doQuery($this->_baseUrl . 'index.html', http_build_query($post, '', '&'));
	}
}
 
if ($argc != 2)
{
	echo ' synthaxe : scriptname.php 1 for activation or 0 for desactivation ';
	exit();
}
 
new wifi($wifi, $ip, $login, $password, $argv[1]);
 
# EOF

Certes, le script ne gère pas tout, mais le but était de jouer avec la ligne de commande tout en n’allant pas dans les extensions exotiques de gestion de celle-ci ….