Controlling TP-Link 941ND router.

v1.0 2016-01-29 14:49 UTC

README

Overview

Control TP-Link 940/941ND router using PHP. This has been tested with TP-Link 940/941ND only but might work with tp link's other routers running similar software.

This package will not work if you have updated your router's firmware to the latest version as all the vulnerabilities have been patched.

Installation

composer require nikhil-pandey/tp-link

Or add the following to your composer.json

"nikhil-pandey/tp-link": "^1.0"

###Usage The basic usage is shown below.

// Create new instance
// Accepts 3 parameters. [ Host, Router Username, Router Password ]
// All the parameters are optional, you can set them later
$router = new \NikhilPandey\TpLink\Router($routerHost);
// Setting the Router Username and password if you already haven't
$router->setAuth('router_username', 'router_password');
// Connecting to the WAN connection
$router->connect();
// Waits for specified seconds
$router->wait($seconds);
// Disconnecting the WAN connection
$router->disconnect();
// The current configuration of the router
$router->getConfig();
// The current configuration of the router as an associative array
$router->getConfigAssoc();

You can also use static method to instantiate the router and chain methods.

use NikhilPandey\TpLink\Router;

$router = Router::at($routerAddress)
    ->setAuth($loginUsername, $loginPassword)
    ->connect()
    ->disconnect()
    ->connect($newWanUsername, $newWanPassword)
    ->wait()
    ->disconnect($newWanUsername, $newWanPassword)
    ->reconnect();
$configuration = $router->getConfigAssoc();
if($configuration['status'] == Router.CONNECTED){
    // ...
}

For more information about the constants and other stuffs, dive into the code :)

Exceptions

Four types of exceptions might occur. InvalidAuthException, UndefinedAuthException and UnknownResponseException, UndefinedHostException. You can catch those and take appropriate actions.

use NikhilPandey\TpLink\Router;
use NikhilPandey\TpLink\Exceptions\InvalidAuthException
// ...
try{
    $router = Router::at($routerAddress)
        ->setAuth($loginUsername, $loginPassword)
        ->connect()
        ->disconnect()
        ->connect($newWanUsername, $newWanPassword);
} catch(InvalidAuthException $e){
    // Handle the exception
} catch(Exception $e){
    // ...
}

Laravel

If you are using it with laravel you can set the service provider and the facade.

Service Provider

'providers' => [
    NikhilPandey\TpLink\TpLinkServiceProvider::class,
]

Facade

'facades'   => [
    'MyRouter'     => NikhilPandey\TpLink\Facades\TpLink::class,
]

After doing so, you can use the program as follows.

app('tplink')
    ->setHost($routerHost)
    ->setAuth($routerUsername, $routerPassword)
    ->setUsername($newWanUsername)
    ->setPassword($newWanPassword)
    ->connect();

MyRouter::at($routerHost)
    ->setAuth($routerUsername, $routerPassword)
    ->setUsername($newWanUsername)
    ->setPassword($newWanPassword)
    ->connect();

License

This package is open-sourced software licensed under the MIT license