Community discussions

MikroTik App
 
User avatar
Vyrtu
newbie
Topic Author
Posts: 45
Joined: Thu Oct 03, 2013 11:49 am
Location: Torrelavega,Spain

Form to connect Mikrotik PEAR2 API PHP

Fri Oct 25, 2013 12:22 pm

hi guys, im here again :S
Im trying create a form to connect a router mikrotik:

php code

<?php
//ConectarAPI

use PEAR2\Net\RouterOS;
// require_once 'pear2\src\PEAR2\Autoload.php';
require_once 'PEAR2_Net_RouterOS-1.0.0b4.phar';


//Formulario
echo "<table><form action='' method='POST'>
<tr>
<td><input type='text' value='". $ip ."' name='ip' /></td>
<td><input type='text' value='". $username ."' name='ip' /></td>
<td><input type='password' value='". $password ."' name='ip' /></td>
<td><button type='submit' value='' name=''>Conectar</button></td></tr>
</form></table>
";

//CONEXION A MIKROTIK
$client = new RouterOS\Client('$ip', '$username', '$password');


?>


Its something like that, but i cant connect, and i have this alert: "Fatal error: Uncaught exception 'PEAR2\Net\Transmitter\SocketException' with message 'stream_socket_client(): php_network_getaddresses: getaddrinfo failed: No such host is known. ' in phar://C:/wamp/www/PEAR2_Net_RouterOS-1.0.0b4.phar/PEAR2_Net_RouterOS-1.0.0b4/src/PEAR2/Net/RouterOS/Communicator.php on line 151"
I think that API is triyng to connect without values :S

Thanks :)
 
User avatar
boen_robot
Forum Guru
Forum Guru
Posts: 2400
Joined: Thu Aug 31, 2006 4:43 pm
Location: europe://Bulgaria/Plovdiv

Re: Form to connect Mikrotik PEAR2 API PHP

Fri Oct 25, 2013 12:29 pm

The single quotes mean the string is passed literally, rather than variables being interpolated.

In other words,
'$ip' 
refers to a hostname called "$ip", which is obviously not a valid IP or domain name.

Remove all apostrophes to pass the actual variable values, i.e.
$client = new RouterOS\Client($ip, $username, $password); 
 
User avatar
Vyrtu
newbie
Topic Author
Posts: 45
Joined: Thu Oct 03, 2013 11:49 am
Location: Torrelavega,Spain

Re: Form to connect Mikrotik PEAR2 API PHP

Fri Oct 25, 2013 12:36 pm

The single quotes mean the string is passed literally, rather than variables being interpolated.

In other words,
'$ip' 
refers to a hostname called "$ip", which is obviously not a valid IP or domain name.

Remove all apostrophes to pass the actual variable values, i.e.
$client = new RouterOS\Client($ip, $username, $password); 
Hmmm i want this form for the last scripts, to choose what router i want list the ppp's connections, and i still having alerts :S


Something like:

php code

<?php
//ConectarAPI
use PEAR2\Net\RouterOS;
// require_once 'pear2\src\PEAR2\Autoload.php';
require_once 'PEAR2_Net_RouterOS-1.0.0b4.phar';
 
 
//Formulario
echo "<table><form action='' method='POST'>
<tr>
<td>IP  <input type='text' value='". $ip ."' name='ip' /></td>
<td>Usuario  <input type='text' value='". $username ."' name='ip' /></td>
<td>Password  <input type='password' value='". $password ."' name='ip' /></td>
<td><button type='submit' value='' name=''>Conectar</button></td></tr>
</form></table>
";

//CONEXION A MIKROTIK
$client = new RouterOS\Client($ip, $username, $password);


.... (Rest of code)
And when i press the button, connect me with the router and print the list

pd. The form should be able alltime, to change the ip and list other router
 
User avatar
boen_robot
Forum Guru
Forum Guru
Posts: 2400
Joined: Thu Aug 31, 2006 4:43 pm
Location: europe://Bulgaria/Plovdiv

Re: Form to connect Mikrotik PEAR2 API PHP

Fri Oct 25, 2013 12:47 pm

That's not how PHP works...

When you first open up a page, PHP runs from the start to end of the file (without any breaks) and it stops. In your browser, you then eventually submit a form that again executes everything from start to end, without any recollection of what happened during any previous requests.

You'd need to use sessions (here's a tutorial and another one) to in essence, create a dedicated login form, which would store the entered credentials into the session. You can then retrieve those session values from $_SESSION, and fill in the constructor with them. It should go without saying that you can of course modify those later, but the point is to store & retrieve them, so that they're remembered for a while once entered.