hello
i need to write a script that would give IP or domain name in a html form via PHP from the user and send the ping request for that IP or domain to a mikrotik router os and show its reply to that form again!
would you please help me with that?
thanks
Use api or SSH with DSA key
hi
thanks for your help.
would you please explain more?how can i send a “form” to the Mikrotik and execute commands?
Using the API client from my signature, you should be able to do it with something like:
<?php
namespace PEAR2\Net\RouterOS;
require_once 'PEAR2/Net/RouterOS/Autoload.php';
if (isset($_GET['act'])) {//This is merely to ensure the form was submitted.
$client = new Client('192.168.0.1', 'admin', 'password');//Adjust RouterOS IP, username and password accordingly.
if ($_GET['act'] === 'Ping' && isset($_GET['address'])) {//This is just one approach that allows you to create a multy purpose form, with ping being just one action.
$pingRequest = new Request('/ping count=3');//Ping can run for unlimited time, but for PHP, we need to actually stop it at some point.
$results = $client->sendSync($pingRequest->setArgument('address', $_GET['address']));
}
}
?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Ping someone</title>
</head>
<body>
<div>
<form action="" method="get">
<ul>
<li>
<label for="address">Address:</label>
<input type="text" id="address" name="address" value="<?php if (isset($_GET['address'])) echo htmlspecialchars($_GET['address']); ?>" />
<li>
<li>
<input type="submit" id="act" name="act" value="Ping" />
</li>
</ul>
</form>
</div>
<?php
if (isset($_GET['act'])) {//There's no need to execute this if the form was not submitted yet.
echo '<div>Results:<ul>';
foreach ($results as $result) {
echo '<li>Time:', $result->getArgument('time'), '</li>';//Add whatever you want displayed in this section.
}
echo '</ul></div>';
}
?>
</body>
</html>
Make sure to enable the API service before you try this!!
thanks for your rep
but would you please tell me how can i use that API?should i Import it to router os?
You place it somewhere on your web server (the one where you should currently have PHP installed), and include/require it in the fashion above, replacing the path to Autoload.php as necessary.
The code itself connects to RouterOS, similarly to how PHP’s MySQL extension connects to a MySQL server - over the network - so the client and server don’t have to be the same machine, and in the case of RouterOS, the client and the server can’t be on the same machine (well, except maybe if your RouterOS is a virtual machine).
The only thing you need to do on the router is enable the API service, and connect with a user that has “api” privileges (the default “full”, “read” and “write” groups all have that).
See if the docs are clear enough.