{"id":233,"date":"2010-03-18T01:06:52","date_gmt":"2010-03-17T23:06:52","guid":{"rendered":"http:\/\/grummfy.be\/blog\/?p=233"},"modified":"2010-03-18T01:16:12","modified_gmt":"2010-03-17T23:16:12","slug":"activation-desactivation-du-wifi-sur-un-d-link-dsl-2640b-via-php","status":"publish","type":"post","link":"https:\/\/grummfy.be\/blog\/233","title":{"rendered":"Activation, d\u00e9sactivation du wifi sur un D-Link DSL-2640B via PHP"},"content":{"rendered":"<p>Un script PHP en ligne de commande pour activer \/\/ d\u00e9sactiver le wifi sur un modem D-Link DSL-2640B. Cela permet d&rsquo;illustrer un peu l&rsquo;int\u00e9r\u00eat de faire du scripting &#8230;<\/p>\n<p>Je ne ferais pas de commentaires, mais n&rsquo;h\u00e9sitez pas \u00e0 poser des questions &#8230;<\/p>\n<pre lang=\"php\">\r\n#!\/usr\/bin\/php\r\n<?php\r\n\r\n\/\/Variable definition\r\n\/\/\r\n\/\/router\r\n$login = 'admin';\r\n$password = 'admin';\r\n$ip = '192.168.1.1';\r\n\r\n\/\/\r\n\/\/wifi\r\n$wifi = array(\r\n\t\/\/ \/!\\ don't touch this \/!\\\r\n\t'wlSsidIdx'\t=> 0,\r\n\t'wlEnbl'\t=> 1,\r\n\t'wlCountry'\t=> 'GB',\r\n\t'wlBasicRate'\t=> '',\r\n\r\n\t\/\/ssid name\r\n\t'wlSsid'\t=> 'monSuperR\u00e9seauxWifi',\r\n\t\/\/0 = visible, 1 = hide\r\n\t'wlHide'\t=> 0,\r\n\t\/\/number of chanel or 0 for auto selection\r\n\t'wlChannel'\t=> 1,\r\n\t\/\/Transmission Speed\r\n\t'wlRate'\t=> 0,\r\n\t\/\/wifi mode 0=802.11b only, 1=Mixed 802.11g and 802.11b, 4=802.11g only\r\n\t'wlgMode'\t=> 4,\r\n\r\n\t'wlWpaGTKRekey'\t=> 750,\r\n\t'wlWpa'\t\t\t=> 'aes',\r\n\t'wlAuthMode'\t=> 'psk2',\r\n\t\/\/authentification mode : 0 = none, 1 = wep, 2 = auto (wpa or wpa2), 3 = wpa2 only, 4 = wpa only\r\n\t'wlAuth'\t=> 0,\r\n\t'wlWpaPsk'\t=> 'masuperclefdelamortquitue',\r\n\t'wlWep'\t\t=> 'disabled',\r\n\t'wlPreauth'\t=> 0\r\n);\r\n\r\n\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\r\n\r\nclass cli\r\n{\r\n\tconst LINE_BREAK = \"\\r\\n\";\r\n\tconst LINE_NO_BREAK = '';\r\n\tconst LINE_RETURN = \"\\r\";\r\n\tconst LINE_NOTHING = ' ';\r\n\r\n\tprotected $_TEMP = 250000;\/\/1\/4 of seconds\r\n\tprotected $_MULTIPLICATOR = 4; \/\/1\/4 -> 4\r\n\r\n\tprotected $_LENGTH = 60;\r\n\r\n\tprotected function _write_empty_space($lng)\r\n\t{\r\n\t\twhile($lng < $this->_LENGTH)\r\n\t\t{\r\n\t\t\techo ' ';\r\n\t\t\t$lng++;\r\n\t\t}\r\n\t}\r\n\r\n\tprotected function _echo($message, $break)\r\n\t{\r\n\t\techo $message, $break;\r\n\t\tif ($break == self::LINE_NO_BREAK)\r\n\t\t{\r\n\t\t\t$this->_write_empty_space(strlen($message));\r\n\t\t}\r\n\t}\r\n\r\n\tprotected function _sleep($second)\r\n\t{\r\n\t\t$second = $second * $this->_MULTIPLICATOR;\r\n\t\t$i = 0;\r\n\t\t$j = 0;\r\n\t\twhile ($i < $second)\r\n\t\t{\r\n\t\t\tusleep($this->_TEMP);\r\n\r\n\t\t\tif ($i % 4)\r\n\t\t\t{\r\n\t\t\t\t$this->_echo('.', self::LINE_NOTHING);\r\n\t\t\t\t$i++;\r\n\t\t\t}\r\n\t\t\telse\r\n\t\t\t{\r\n\t\t\t\tif($j % 2)\r\n\t\t\t\t{\r\n\t\t\t\t\t$this->_echo('', self::LINE_RETURN);\r\n\t\t\t\t\t$this->_write_empty_space(0);\r\n\t\t\t\t\t$this->_echo('', self::LINE_RETURN);\r\n\t\t\t\t}\r\n\t\t\t\telse\r\n\t\t\t\t{\r\n\t\t\t\t\t$this->_echo('Please wait', self::LINE_NOTHING);\r\n\t\t\t\t\t$i++;\r\n\t\t\t\t}\r\n\t\t\t\t$j++;\r\n\t\t\t}\r\n\t\t}\r\n\t\t$this->_echo('', self::LINE_RETURN);\r\n\t}\r\n}\r\n\r\nclass wifi extends cli\r\n{\r\n\tprotected $_ch;\r\n\tprotected $_baseUrl;\r\n\r\n\tpublic function __construct($wifi, $ip, $login, $password, $activation)\r\n\t{\r\n\t\t$this->_baseUrl = 'http:\/\/' . $ip . '\/';\r\n\r\n\t\t$this->_ch = curl_init();\r\n\t\tcurl_setopt($this->_ch, CURLOPT_HEADER, false);\r\n\t\tcurl_setopt($this->_ch, CURLOPT_RETURNTRANSFER, true);\r\n\r\n\t\t$this->_auth($login, $password);\r\n\r\n\t\t$activation = intval($activation);\r\n\r\n\t\tif ($activation == 1)\r\n\t\t{\r\n\t\t\t$this->_activation($wifi);\r\n\t\t}\r\n\t\telse\r\n\t\t{\r\n\t\t\t$this->_disactivation($wifi);\r\n\t\t}\r\n\t}\r\n\r\n\tpublic function __destruct()\r\n\t{\r\n\t\tcurl_close($this->_ch);\r\n\t}\r\n\r\n\tprotected function _activation($wifi)\r\n\t{\r\n\t\t$this->_echo('Activation ...', self::LINE_NO_BREAK);\r\n\r\n\t\t$wifi['wlBasicRate'] = ($wifi['wlgMode'] == 4) ? 'wifi2' : 'default';\r\n\r\n\t\t$this->_doQuery($this->_baseUrl . 'wirelesssetting.wl?' . http_build_query($wifi, '', '&'));\r\n\t\t$this->_sleep(5);\r\n\t}\r\n\r\n\tprotected function _disactivation($wifi)\r\n\t{\r\n\t\t$this->_echo('Disactivation ...', self::LINE_NO_BREAK);\r\n\r\n\t\t$wifiDown = array(\r\n\t\t\t'wlEnbl'\t=> 0,\r\n\t\t\t'wlSsidIdx'\t=> $wifi['wlSsidIdx'],\r\n\t\t\t'wlCountry'\t=> $wifi['wlCountry'],\r\n\t\t);\r\n\r\n\t\t$this->_doQuery($this->_baseUrl . 'wirelesssetting.wl?' . http_build_query($wifiDown, '', '&'));\r\n\t\t$this->_sleep(5);\r\n\t}\r\n\r\n\tprotected function _doQuery($url, $post = '')\r\n\t{\r\n\t\tcurl_setopt($this->_ch, CURLOPT_URL, $url);\r\n\r\n\t\tif (!empty($post))\r\n\t\t{\r\n\t\t\tcurl_setopt($this->_ch, CURLOPT_POST, true);\r\n\t\t\tcurl_setopt($this->_ch, CURLOPT_POSTFIELDS, $post);\r\n\t\t}\r\n\t\telse\r\n\t\t{\r\n\t\t\tcurl_setopt($this->_ch, CURLOPT_HTTPGET, true);\r\n\t\t}\r\n\r\n\t\tif(curl_exec($this->_ch) === false)\r\n\t\t{\r\n\t\t\t$this->_echo('', self::LINE_BREAK);\r\n\t\t\t$this->_echo('Error Curl : ' . curl_error($this->ch), self::LINE_BREAK);\r\n\t\t}\r\n\t\telse\r\n\t\t{\r\n\t\t\t$this->_echo('DONE', self::LINE_BREAK);\r\n\t\t}\r\n\t}\r\n\r\n\tprotected function _auth($login, $password)\r\n\t{\r\n\t\t$this->_echo('Authentification ...', self::LINE_NO_BREAK);\r\n\r\n\t\t$post = array(\r\n\t\t\t'username'\t=> $login,\r\n\t\t\t'password'\t=> $password\r\n\t\t);\r\n\r\n\t\t$this->_doQuery($this->_baseUrl . 'index.html', http_build_query($post, '', '&'));\r\n\t}\r\n}\r\n\r\nif ($argc != 2)\r\n{\r\n\techo ' synthaxe : scriptname.php 1 for activation or 0 for desactivation ';\r\n\texit();\r\n}\r\n\r\nnew wifi($wifi, $ip, $login, $password, $argv[1]);\r\n\r\n# EOF\r\n<\/pre>\n<p>Certes, le script ne g\u00e8re pas tout, mais le but \u00e9tait de jouer avec la ligne de commande tout en n\u2019allant pas dans les extensions exotiques de gestion de celle-ci &#8230;.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Un script PHP en ligne de commande pour activer \/\/ d\u00e9sactiver le wifi sur un modem D-Link DSL-2640B. Cela permet d&rsquo;illustrer un peu l&rsquo;int\u00e9r\u00eat de faire du scripting &#8230; Je ne ferais pas de commentaires, mais n&rsquo;h\u00e9sitez pas \u00e0 poser des questions &#8230; #!\/usr\/bin\/php<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"webmentions_disabled_pings":false,"webmentions_disabled":false,"footnotes":""},"categories":[9,12,10,88],"tags":[90,110,24,92,42,91],"class_list":["post-233","post","type-post","status-publish","format-standard","hentry","category-dev","category-logiciel","category-php","category-reflexion-du-jour","tag-cli","tag-php","tag-programmation","tag-reseaux","tag-script","tag-wifi"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/grummfy.be\/blog\/wp-json\/wp\/v2\/posts\/233","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/grummfy.be\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/grummfy.be\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/grummfy.be\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/grummfy.be\/blog\/wp-json\/wp\/v2\/comments?post=233"}],"version-history":[{"count":5,"href":"https:\/\/grummfy.be\/blog\/wp-json\/wp\/v2\/posts\/233\/revisions"}],"predecessor-version":[{"id":238,"href":"https:\/\/grummfy.be\/blog\/wp-json\/wp\/v2\/posts\/233\/revisions\/238"}],"wp:attachment":[{"href":"https:\/\/grummfy.be\/blog\/wp-json\/wp\/v2\/media?parent=233"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/grummfy.be\/blog\/wp-json\/wp\/v2\/categories?post=233"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/grummfy.be\/blog\/wp-json\/wp\/v2\/tags?post=233"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}