Advertisement
jpr105

WiFiOnOff.php / PHP to manage 2,4Ghz Wifi on ASUS RT-AC68U

Nov 26th, 2018
450
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 8.83 KB | None | 0 0
  1. <?php
  2.  
  3. //----------------------------------------------------------------------------------
  4. //
  5. // Script de commande du WiFi 2,4Ghz (ON ou OFF) sur routeur ASUS
  6. //
  7. // Principe :
  8. // 1 - Les infos de connexion sont enregistrées dans un fichier texte sur ce serveur
  9. // 2 - Récupération IP, port, identifiants et connexion au routeur en SSH
  10. // 3 - Utilisation des commandes :
  11. //     nvram get wl0_radio
  12. //     nvram set wl0_radio=1 ou nvram set wl0_radio=0
  13. //     nvram commit
  14. //     service restart_wireless
  15. // 4 - Note : le 5Ghz se gère de la même façon en remplacant 'wl0' par 'wl1'
  16. //
  17. //----------------------------------------------------------------------------------
  18.  
  19. $Verbose = false;
  20.  
  21. /* Validation du code action */
  22.  
  23. $Action = $_GET["action"];
  24.  
  25. if (strlen($Action) == 0) {
  26.     exit('Missing action code');
  27.     } else {
  28.     if ($Action != 'ON' and $Action != 'OFF') {
  29.     exit('Invalid action code');
  30.     }
  31. }
  32.  
  33. if ($Verbose) {echo 'Code action ----> ', $Action, '<br>';}
  34.  
  35. /* Ouverture du fichier des infos de connexion au routeur et retourne un tableau contenant une ligne par élément */
  36.  
  37. $lines = file('AsusCredentials.txt');
  38.  
  39. /* ----> Pour les tests */
  40.  
  41. if ($Verbose) {
  42.     foreach ($lines as $lineNumber => $lineContent)
  43.     {
  44.     echo $lineNumber,' ',$lineContent;
  45.     echo "<br>";
  46.     }
  47. }
  48.  
  49. /* Recherche des 4 variables de connexion */
  50.  
  51. $ASUS_IP = "";
  52. $ASUS_Port = "";
  53. $ASUS_User = "";
  54. $ASUS_Psw = "";
  55.  
  56. /* ----> Adresse IP */
  57.  
  58. $mystring = trim($lines[0]);
  59. $findme   = 'IP=';
  60. $pos = strpos($mystring, $findme);
  61.  
  62. if ($pos === false) {
  63.     echo "La chaîne '$findme' ne se trouve pas dans la chaîne '$mystring'", '<br>';
  64.     exit('Error retrieving IP address');
  65.     } else {
  66.     if ($pos <> 0) {
  67.     echo 'La chaîne ', $findme, ' n\'est pas au début de ', $mystring, '<br>';
  68.     exit('Error retrieving IP address');
  69.     } else {
  70.     if ($Verbose) {echo "La chaine '$findme' a été trouvée dans la chaîne '$mystring' et débute à la position $pos", '<br>';}
  71.     $ASUS_IP = substr($mystring, 3);
  72.     }
  73. }
  74.  
  75. /* ----> Port */
  76.  
  77. $mystring = trim($lines[1]);
  78. $findme   = 'Port=';
  79. $pos = strpos($mystring, $findme);
  80.  
  81. if ($pos === false) {
  82.     echo "La chaîne '$findme' ne se trouve pas dans la chaîne '$mystring'", '<br>';
  83.     exit('Error retrieving Port number');
  84.     } else {
  85.     if ($pos <> 0) {
  86.     echo 'La chaîne ', $findme, ' n\'est pas au début de ', $mystring, '<br>';
  87.     exit('Error retrieving Port number');
  88.     } else {
  89.     if ($Verbose) {echo "La chaine '$findme' a été trouvée dans la chaîne '$mystring' et débute à la position $pos", '<br>';}
  90.     $ASUS_Port = substr($mystring, 5);
  91.     }
  92. }
  93.  
  94. /* ----> User */
  95.  
  96. $mystring = trim($lines[2]);
  97. $findme   = 'User=';
  98. $pos = strpos($mystring, $findme);
  99.  
  100. if ($pos === false) {
  101.     echo "La chaîne '$findme' ne se trouve pas dans la chaîne '$mystring'", '<br>';
  102.     exit('Error retrieving Username');
  103.     } else {
  104.     if ($pos <> 0) {
  105.     echo 'La chaîne ', $findme, ' n\'est pas au début de ', $mystring, '<br>';
  106.     exit('Error retrieving Username');
  107.     } else {
  108.     if ($Verbose) {echo "La chaine '$findme' a été trouvée dans la chaîne '$mystring' et débute à la position $pos", '<br>';}
  109.     $ASUS_User = substr($mystring, 5);
  110.     }
  111. }
  112.  
  113. /* ----> Password */
  114.  
  115. $mystring = trim($lines[3]);
  116. $findme   = 'Password=';
  117. $pos = strpos($mystring, $findme);
  118.  
  119. if ($pos === false) {
  120.     echo "La chaîne '$findme' ne se trouve pas dans la chaîne '$mystring'", '<br>';
  121.     exit('Error retrieving Password');
  122.     } else {
  123.     if ($pos <> 0) {
  124.     echo 'La chaîne ', $findme, ' n\'est pas au début de ', $mystring, '<br>';
  125.     exit('Error retrieving Password');
  126.     } else {
  127.     if ($Verbose) {echo "La chaine '$findme' a été trouvée dans la chaîne '$mystring' et débute à la position $pos", '<br>';}
  128.     $ASUS_Psw = substr($mystring, 9);
  129.     }
  130. }
  131.  
  132. if ($Verbose) {
  133.     echo 'Adresse IP = ', $ASUS_IP, '<br>';
  134.     echo 'Port = ', $ASUS_Port, '<br>';
  135.     echo 'User = ', $ASUS_User, '<br>';
  136.     echo 'Password = ', $ASUS_Psw, '<br>';
  137. }
  138.  
  139. // Dialogue avec le routeur
  140.  
  141. /* Connexion */
  142.  
  143. $AsusSSH = ssh2_connect ($ASUS_IP, $ASUS_Port);
  144. $AsusLogin = ssh2_auth_password ($AsusSSH , $ASUS_User, $ASUS_Psw);
  145.    
  146. /* Vérification de la connexion */
  147.    
  148. if ($AsusLogin) {
  149.     if ($Verbose) {echo 'Connexion SSH effectuée', '<br>';}
  150.     } else {
  151.     echo 'Connexion SSH échouée', '<br>';
  152.     exit('Unable to connect to the router');
  153. }
  154.  
  155. /* On récupère l'état du WiFi (ON ou OFF) */
  156.  
  157. $Order = "nvram get wl0_radio";
  158.  
  159. $Flux = ssh2_exec($AsusSSH, $Order);
  160. $Flux_Err = ssh2_fetch_stream($Flux, SSH2_STREAM_STDERR);
  161. $Flux_IO = ssh2_fetch_stream($Flux, SSH2_STREAM_STDIO);
  162.  
  163. stream_set_blocking($Flux_Err, true);
  164. stream_set_blocking($Flux_IO, true);
  165.  
  166. $Val_Flux_Err = stream_get_contents($Flux_Err);
  167. $Val_Flux_IO = stream_get_contents($Flux_IO);
  168.  
  169. if ($Verbose) {
  170.     echo '----> Récupération du status du 2,4Ghz', '<br>';
  171.     echo 'Code retour = ', $Val_Flux_Err, '<br>';
  172.     echo 'Valeur retournée = ', $Val_Flux_IO, '<br>';
  173. }
  174.  
  175. if (empty($Val_Flux_Err) == false) {
  176.     echo 'Erreur de récupération du status du 2,4Ghz : ', $Val_Flux_Err, '<br>';
  177.     ByeByeAsus($AsusSSH, $Verbose);
  178.     exit('Error retrieving WiFi status');
  179. }    
  180.    
  181. if ($Val_Flux_IO != 0 and $Val_Flux_IO != 1) {
  182.     echo 'Valeur incorrecte du status du 2,4Ghz : ', $Val_Flux_IO, '<br>';
  183.     ByeByeAsus($AsusSSH, $Verbose);
  184.     exit('Invalid WiFi status');
  185. }
  186.        
  187. if (($Val_Flux_IO == 1 and $Action == 'ON') or ($Val_Flux_IO == 0 and $Action == 'OFF')) {
  188.     echo 'Changement du status du 2,4Ghz inutile - On arrête', '<br>';
  189.     ByeByeAsus($AsusSSH, $Verbose);
  190.     exit('WiFi already set');
  191. }
  192.  
  193. /* On change l'état du WiFi */
  194.  
  195. if ($Action == 'ON') {
  196.     $Order = 'nvram set wl0_radio=1';
  197.     } else {
  198.     $Order = 'nvram set wl0_radio=0';
  199. }
  200.    
  201. $Flux = ssh2_exec($AsusSSH, $Order);
  202. $Flux_Err = ssh2_fetch_stream($Flux, SSH2_STREAM_STDERR);
  203. $Flux_IO = ssh2_fetch_stream($Flux, SSH2_STREAM_STDIO);
  204.  
  205. stream_set_blocking($Flux_Err, true);
  206. stream_set_blocking($Flux_IO, true);
  207.  
  208. $Val_Flux_Err = stream_get_contents($Flux_Err);
  209. $Val_Flux_IO = stream_get_contents($Flux_IO);
  210.  
  211. if ($Verbose) {
  212.     echo '----> Changement du status du WiFi : ', "'", $Order, "'", '<br>';
  213.     echo 'Code retour = ', $Val_Flux_Err, '<br>';
  214.     echo 'Valeur retournée = ', $Val_Flux_IO, '<br>';
  215. }
  216.  
  217. if (empty($Val_Flux_Err) == false) {
  218.     echo 'Erreur de changement du status du WiFi : ', $Val_Flux_Err, '<br>';
  219.     ByeByeAsus($AsusSSH, $Verbose);
  220.     exit('Error changing WiFi status');
  221. }    
  222.  
  223. /* On commit le changement */
  224.  
  225. $Order = 'nvram commit';
  226.    
  227. $Flux = ssh2_exec($AsusSSH, $Order);
  228. $Flux_Err = ssh2_fetch_stream($Flux, SSH2_STREAM_STDERR);
  229. $Flux_IO = ssh2_fetch_stream($Flux, SSH2_STREAM_STDIO);
  230.  
  231. stream_set_blocking($Flux_Err, true);
  232. stream_set_blocking($Flux_IO, true);
  233.  
  234. $Val_Flux_Err = stream_get_contents($Flux_Err);
  235. $Val_Flux_IO = stream_get_contents($Flux_IO);
  236.  
  237. if ($Verbose) {
  238.     echo '----> Commit du changement d\'état', '<br>';
  239.     echo 'Code retour = ', $Val_Flux_Err, '<br>';
  240.     echo 'Valeur retournée = ', $Val_Flux_IO, '<br>';
  241. }
  242.  
  243. if (empty($Val_Flux_Err) == false) {
  244.     echo 'Erreur de commit du changement d\'état', $Val_Flux_Err, '<br>';
  245.     ByeByeAsus($AsusSSH, $Verbose);
  246.     exit('Error commiting WiFi status change');
  247. }    
  248.  
  249. /* On relance le service */
  250.  
  251. $Order = 'service restart_wireless';
  252.    
  253. $Flux = ssh2_exec($AsusSSH, $Order);
  254. $Flux_Err = ssh2_fetch_stream($Flux, SSH2_STREAM_STDERR);
  255. $Flux_IO = ssh2_fetch_stream($Flux, SSH2_STREAM_STDIO);
  256.  
  257. stream_set_blocking($Flux_Err, true);
  258. stream_set_blocking($Flux_IO, true);
  259.  
  260. $Val_Flux_Err = stream_get_contents($Flux_Err);
  261. $Val_Flux_IO = stream_get_contents($Flux_IO);
  262.  
  263. if ($Verbose) {
  264.     echo '----> Redémarrage du service', '<br>';
  265.     echo 'Code retour = ', $Val_Flux_Err, '<br>';
  266.     echo 'Valeur retournée = ', $Val_Flux_IO, '<br>';
  267. }
  268.  
  269. if (empty($Val_Flux_Err) == false) {
  270.     echo 'Erreur de redémarrage du service', $Val_Flux_Err, '<br>';
  271.     ByeByeAsus($AsusSSH, $Verbose);
  272.     exit('Error restarting service');
  273. }    
  274.  
  275. if (trim($Val_Flux_IO) != 'Done.') {
  276.     echo 'Code retour incorrect au redémarrage du service : ', $Val_Flux_IO, '<br>';
  277.     ByeByeAsus($AsusSSH, $Verbose);
  278.     exit('Invalid return code restarting service');
  279. }
  280.  
  281. /* C'est fini, tout le monde descend */
  282.  
  283. ByeByeAsus($AsusSSH, $Verbose);
  284.  
  285. if ($Action == 'ON') {
  286.     exit('Wifi switched ON');
  287.     } else {
  288.     exit('Wifi switched OFF');
  289. }
  290.  
  291. function ByeByeAsus($AsusSSH, $Verbose)
  292.  
  293. {
  294.     $AsusLogout = ssh2_disconnect ($AsusSSH);
  295.    
  296.     /* Vérification de la déconnexion */
  297.    
  298.     if ($AsusLogout) {
  299.         if ($Verbose) {echo 'Déconnexion SSH effectuée', '<br>';}
  300.         } else {
  301.         echo 'Déconnexion SSH échouée', '<br>';
  302.     }
  303. }
  304.  
  305. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement