Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- //----------------------------------------------------------------------------------
- //
- // Script de commande du WiFi 2,4Ghz (ON ou OFF) sur routeur ASUS
- //
- // Principe :
- // 1 - Les infos de connexion sont enregistrées dans un fichier texte sur ce serveur
- // 2 - Récupération IP, port, identifiants et connexion au routeur en SSH
- // 3 - Utilisation des commandes :
- // nvram get wl0_radio
- // nvram set wl0_radio=1 ou nvram set wl0_radio=0
- // nvram commit
- // service restart_wireless
- // 4 - Note : le 5Ghz se gère de la même façon en remplacant 'wl0' par 'wl1'
- //
- //----------------------------------------------------------------------------------
- $Verbose = false;
- /* Validation du code action */
- $Action = $_GET["action"];
- if (strlen($Action) == 0) {
- exit('Missing action code');
- } else {
- if ($Action != 'ON' and $Action != 'OFF') {
- exit('Invalid action code');
- }
- }
- if ($Verbose) {echo 'Code action ----> ', $Action, '<br>';}
- /* Ouverture du fichier des infos de connexion au routeur et retourne un tableau contenant une ligne par élément */
- $lines = file('AsusCredentials.txt');
- /* ----> Pour les tests */
- if ($Verbose) {
- foreach ($lines as $lineNumber => $lineContent)
- {
- echo $lineNumber,' ',$lineContent;
- echo "<br>";
- }
- }
- /* Recherche des 4 variables de connexion */
- $ASUS_IP = "";
- $ASUS_Port = "";
- $ASUS_User = "";
- $ASUS_Psw = "";
- /* ----> Adresse IP */
- $mystring = trim($lines[0]);
- $findme = 'IP=';
- $pos = strpos($mystring, $findme);
- if ($pos === false) {
- echo "La chaîne '$findme' ne se trouve pas dans la chaîne '$mystring'", '<br>';
- exit('Error retrieving IP address');
- } else {
- if ($pos <> 0) {
- echo 'La chaîne ', $findme, ' n\'est pas au début de ', $mystring, '<br>';
- exit('Error retrieving IP address');
- } else {
- if ($Verbose) {echo "La chaine '$findme' a été trouvée dans la chaîne '$mystring' et débute à la position $pos", '<br>';}
- $ASUS_IP = substr($mystring, 3);
- }
- }
- /* ----> Port */
- $mystring = trim($lines[1]);
- $findme = 'Port=';
- $pos = strpos($mystring, $findme);
- if ($pos === false) {
- echo "La chaîne '$findme' ne se trouve pas dans la chaîne '$mystring'", '<br>';
- exit('Error retrieving Port number');
- } else {
- if ($pos <> 0) {
- echo 'La chaîne ', $findme, ' n\'est pas au début de ', $mystring, '<br>';
- exit('Error retrieving Port number');
- } else {
- if ($Verbose) {echo "La chaine '$findme' a été trouvée dans la chaîne '$mystring' et débute à la position $pos", '<br>';}
- $ASUS_Port = substr($mystring, 5);
- }
- }
- /* ----> User */
- $mystring = trim($lines[2]);
- $findme = 'User=';
- $pos = strpos($mystring, $findme);
- if ($pos === false) {
- echo "La chaîne '$findme' ne se trouve pas dans la chaîne '$mystring'", '<br>';
- exit('Error retrieving Username');
- } else {
- if ($pos <> 0) {
- echo 'La chaîne ', $findme, ' n\'est pas au début de ', $mystring, '<br>';
- exit('Error retrieving Username');
- } else {
- if ($Verbose) {echo "La chaine '$findme' a été trouvée dans la chaîne '$mystring' et débute à la position $pos", '<br>';}
- $ASUS_User = substr($mystring, 5);
- }
- }
- /* ----> Password */
- $mystring = trim($lines[3]);
- $findme = 'Password=';
- $pos = strpos($mystring, $findme);
- if ($pos === false) {
- echo "La chaîne '$findme' ne se trouve pas dans la chaîne '$mystring'", '<br>';
- exit('Error retrieving Password');
- } else {
- if ($pos <> 0) {
- echo 'La chaîne ', $findme, ' n\'est pas au début de ', $mystring, '<br>';
- exit('Error retrieving Password');
- } else {
- if ($Verbose) {echo "La chaine '$findme' a été trouvée dans la chaîne '$mystring' et débute à la position $pos", '<br>';}
- $ASUS_Psw = substr($mystring, 9);
- }
- }
- if ($Verbose) {
- echo 'Adresse IP = ', $ASUS_IP, '<br>';
- echo 'Port = ', $ASUS_Port, '<br>';
- echo 'User = ', $ASUS_User, '<br>';
- echo 'Password = ', $ASUS_Psw, '<br>';
- }
- // Dialogue avec le routeur
- /* Connexion */
- $AsusSSH = ssh2_connect ($ASUS_IP, $ASUS_Port);
- $AsusLogin = ssh2_auth_password ($AsusSSH , $ASUS_User, $ASUS_Psw);
- /* Vérification de la connexion */
- if ($AsusLogin) {
- if ($Verbose) {echo 'Connexion SSH effectuée', '<br>';}
- } else {
- echo 'Connexion SSH échouée', '<br>';
- exit('Unable to connect to the router');
- }
- /* On récupère l'état du WiFi (ON ou OFF) */
- $Order = "nvram get wl0_radio";
- $Flux = ssh2_exec($AsusSSH, $Order);
- $Flux_Err = ssh2_fetch_stream($Flux, SSH2_STREAM_STDERR);
- $Flux_IO = ssh2_fetch_stream($Flux, SSH2_STREAM_STDIO);
- stream_set_blocking($Flux_Err, true);
- stream_set_blocking($Flux_IO, true);
- $Val_Flux_Err = stream_get_contents($Flux_Err);
- $Val_Flux_IO = stream_get_contents($Flux_IO);
- if ($Verbose) {
- echo '----> Récupération du status du 2,4Ghz', '<br>';
- echo 'Code retour = ', $Val_Flux_Err, '<br>';
- echo 'Valeur retournée = ', $Val_Flux_IO, '<br>';
- }
- if (empty($Val_Flux_Err) == false) {
- echo 'Erreur de récupération du status du 2,4Ghz : ', $Val_Flux_Err, '<br>';
- ByeByeAsus($AsusSSH, $Verbose);
- exit('Error retrieving WiFi status');
- }
- if ($Val_Flux_IO != 0 and $Val_Flux_IO != 1) {
- echo 'Valeur incorrecte du status du 2,4Ghz : ', $Val_Flux_IO, '<br>';
- ByeByeAsus($AsusSSH, $Verbose);
- exit('Invalid WiFi status');
- }
- if (($Val_Flux_IO == 1 and $Action == 'ON') or ($Val_Flux_IO == 0 and $Action == 'OFF')) {
- echo 'Changement du status du 2,4Ghz inutile - On arrête', '<br>';
- ByeByeAsus($AsusSSH, $Verbose);
- exit('WiFi already set');
- }
- /* On change l'état du WiFi */
- if ($Action == 'ON') {
- $Order = 'nvram set wl0_radio=1';
- } else {
- $Order = 'nvram set wl0_radio=0';
- }
- $Flux = ssh2_exec($AsusSSH, $Order);
- $Flux_Err = ssh2_fetch_stream($Flux, SSH2_STREAM_STDERR);
- $Flux_IO = ssh2_fetch_stream($Flux, SSH2_STREAM_STDIO);
- stream_set_blocking($Flux_Err, true);
- stream_set_blocking($Flux_IO, true);
- $Val_Flux_Err = stream_get_contents($Flux_Err);
- $Val_Flux_IO = stream_get_contents($Flux_IO);
- if ($Verbose) {
- echo '----> Changement du status du WiFi : ', "'", $Order, "'", '<br>';
- echo 'Code retour = ', $Val_Flux_Err, '<br>';
- echo 'Valeur retournée = ', $Val_Flux_IO, '<br>';
- }
- if (empty($Val_Flux_Err) == false) {
- echo 'Erreur de changement du status du WiFi : ', $Val_Flux_Err, '<br>';
- ByeByeAsus($AsusSSH, $Verbose);
- exit('Error changing WiFi status');
- }
- /* On commit le changement */
- $Order = 'nvram commit';
- $Flux = ssh2_exec($AsusSSH, $Order);
- $Flux_Err = ssh2_fetch_stream($Flux, SSH2_STREAM_STDERR);
- $Flux_IO = ssh2_fetch_stream($Flux, SSH2_STREAM_STDIO);
- stream_set_blocking($Flux_Err, true);
- stream_set_blocking($Flux_IO, true);
- $Val_Flux_Err = stream_get_contents($Flux_Err);
- $Val_Flux_IO = stream_get_contents($Flux_IO);
- if ($Verbose) {
- echo '----> Commit du changement d\'état', '<br>';
- echo 'Code retour = ', $Val_Flux_Err, '<br>';
- echo 'Valeur retournée = ', $Val_Flux_IO, '<br>';
- }
- if (empty($Val_Flux_Err) == false) {
- echo 'Erreur de commit du changement d\'état', $Val_Flux_Err, '<br>';
- ByeByeAsus($AsusSSH, $Verbose);
- exit('Error commiting WiFi status change');
- }
- /* On relance le service */
- $Order = 'service restart_wireless';
- $Flux = ssh2_exec($AsusSSH, $Order);
- $Flux_Err = ssh2_fetch_stream($Flux, SSH2_STREAM_STDERR);
- $Flux_IO = ssh2_fetch_stream($Flux, SSH2_STREAM_STDIO);
- stream_set_blocking($Flux_Err, true);
- stream_set_blocking($Flux_IO, true);
- $Val_Flux_Err = stream_get_contents($Flux_Err);
- $Val_Flux_IO = stream_get_contents($Flux_IO);
- if ($Verbose) {
- echo '----> Redémarrage du service', '<br>';
- echo 'Code retour = ', $Val_Flux_Err, '<br>';
- echo 'Valeur retournée = ', $Val_Flux_IO, '<br>';
- }
- if (empty($Val_Flux_Err) == false) {
- echo 'Erreur de redémarrage du service', $Val_Flux_Err, '<br>';
- ByeByeAsus($AsusSSH, $Verbose);
- exit('Error restarting service');
- }
- if (trim($Val_Flux_IO) != 'Done.') {
- echo 'Code retour incorrect au redémarrage du service : ', $Val_Flux_IO, '<br>';
- ByeByeAsus($AsusSSH, $Verbose);
- exit('Invalid return code restarting service');
- }
- /* C'est fini, tout le monde descend */
- ByeByeAsus($AsusSSH, $Verbose);
- if ($Action == 'ON') {
- exit('Wifi switched ON');
- } else {
- exit('Wifi switched OFF');
- }
- function ByeByeAsus($AsusSSH, $Verbose)
- {
- $AsusLogout = ssh2_disconnect ($AsusSSH);
- /* Vérification de la déconnexion */
- if ($AsusLogout) {
- if ($Verbose) {echo 'Déconnexion SSH effectuée', '<br>';}
- } else {
- echo 'Déconnexion SSH échouée', '<br>';
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement