Example Usage Reading and Writing Data - ShaneMcC/phprouter GitHub Wiki
This article attempts to give some examples on how to use this library for your own purposes.
Getting Data
The most simple use case, is getting data from a device.
<?php
// Include the init file, this includes any other files required.
require_once(dirname(__FILE__) . "/PHPRouter.php");
use ShaneMcC\PhpRouter\Implementations\HPProcurve;
// Create a new instance of the HPProcurve device.
// Specify the host, username, password and connection type
//
// The connection type can be "ssh" or "telnet"
$r = new HPProcurve('192.168.102.7', 'operator', 'password', 'ssh');
try {
// Try to connect and log in.
$r->connect();
// Get some basic data that any user has access to.
$r->exec('show version', true);
$r->exec('show system-information', true);
// Get running-config data, this requires admin access, so first
// we need to use "enable" to upgrade to manager level.
//
// HP Procurve may require a username for this, so provide one.
$r->enable('enablepassword', 'manager');
$r->exec('show run', true);
// Done
$r->disconnect();
} catch (Exception $e) {
// There was some kind of error during the process.
// connect() and exec() can both throw exceptions for various reasons.
echo 'There was an error: ', $e->getMessage(), "\n";
die(1);
}
?>
Writing Data
Although this library is primarily designed for retrieving data, Writing data is also possible, though currently not quite as elegant...
<?php
require_once(dirname(__FILE__) . "/PHPRouter.php");
use ShaneMcC\PhpRouter\Implementations\HPProcurve;
// Create a new instance of the HPProcurve device.
// Specify the host, username, password and connection type
$r = new HPProcurve('192.168.102.7', 'operator', 'password', 'ssh');
try {
// Try to connect and log in.
$r->connect();
// Enable mode
$r->enable('enablepassword', 'manager');
// Enter Config Mode
$r->writeln('conf t');
// Create a new vlan
$r->writeln('vlan 11');
$r->writeln('name "Test VLAN"');
// End config mode.
$r->writeln('end');
// Swallow the output generated by the above commands.
//
// This function looks for the next time the switch shows the default
// prompt, which we know will happen after the 'end' above (prior to
// that, the (config) prompt will be shown which getNextStreamData will
// not halt on.)
$r->getNextStreamData();
// Now check that our vlan was created.
$showVlan = $r->exec('show vlan');
if (preg_match('#^\s+11\s+Test VLAN\s+#imS', $showVlan)) {
echo 'VLAN Created Successfully.', "\n";
} else {
echo 'VLAN was not created.', "\n";
}
// Done
$r->disconnect();
} catch (Exception $e) {
// There was some kind of error during the process.
// connect() and exec() can both throw exceptions for various reasons.
echo 'There was an error: ', $e->getMessage(), "\n";
die(1);
}
?>
It is important to understand how the CLI generates output when using writeln()
commands. You must use getNextStreamData()
to make sure that the internal state is consistent and up to date, otherwise exec()
commands will not function as expected.
If your writeln output causes the normal prompt to be shown multiple times, you must call getNextStreamData()
the right number of times. Calling it too many times will result in the library hanging waiting until such time as there is matching output.
Writing Data - Advanced Usage
Sometimes, the switch may-or-may-not prompt during certain tasks, and you might want to handle this, for example, what if we want to do the following:
PROCURVE# conf t
PROCURVE(config)# vlan 11 name "Test VLAN 1"
PROCURVE(config)# vlan 12 name "Test VLAN 2"
PROCURVE(config)# vlan 12 untagged 28
PROCURVE(config)# end
PROCURVE#
PROCURVE# conf t
PROCURVE(config)# no vlan 11
PROCURVE(config)# no vlan 12
The following ports will be moved to the default VLAN:
28
Do you want to continue? [y/n] y
PROCURVE(config)# end
PROCURVE#
This can be handled as well:
<?php
require_once(dirname(__FILE__) . "/PHPRouter.php");
use ShaneMcC\PhpRouter\Implementations\HPProcurve;
$r = new HPProcurve('192.168.102.7', 'operator', 'password', 'ssh');
try {
$r->connect();
$r->enable('enablepassword', 'manager');
// Create the VLANs
$r->writeln('conf t');
$r->writeln('vlan 11 name "Test VLAN 1"');
$r->writeln('vlan 12 name "Test VLAN 2"');
$r->writeln('vlan 12 untagged 28');
$r->writeln('end');
$r->getNextStreamData();
// Now check that our vlans were created.
$showVlan = $r->exec('show vlan');
$vlan11 = preg_match('#^\s+11\s+#imS', $showVlan);
$vlan12 = preg_match('#^\s+12\s+#imS', $showVlan);
if (!$vlan11 || !$vlan12) {
echo 'VLANs were not created.', "\n";
die(1);
}
// Enter config mode.
$r->writeln('conf t');
// Remove the VLANs
foreach (array(11, 12) as $vlan) {
$r->writeln('no vlan ' . $vlan);
// Swallow the stream data up-to the line of config we have just
// entered.
$r->getStreamData("\nno vlan " . $vlan . "\n");
// Switch might prompt us, or might not...
//
// If it prompts us, it will ask "Do you want to continue?"
// If it does not, it will show us a prompt containing "(config)#"
//
// We can use getStreamData() to look for either of these and then
// return once it finds it.
$r->getStreamData(array('Do you want to continue?', '(config)#'));
// Now we can check which it was, and answer the question if it
// was posed.
if ($r->getLastBreakString() == 'Do you want to continue?') {
$r->write('Y');
}
}
// Exit config mode.
$r->writeln('end');
// Look for default prompt.
$r->getNextStreamData();
// Get the vlan list, and disconnect.
$showVlan = $r->exec('show vlan');
$r->disconnect();
// Now check if the VLANs were removed.
$vlan11 = preg_match('#^\s+11\s+#imS', $showVlan);
$vlan12 = preg_match('#^\s+12\s+#imS', $showVlan);
if ($vlan11 || $vlan12) {
echo 'VLANs were not removed.', "\n";
die(1);
} else {
echo 'VLANs were removed successfully.', "\n";
die(0);
}
} catch (Exception $e) {
// There was some kind of error during the process.
// connect() and exec() can both throw exceptions for various reasons.
echo 'There was an error: ', $e->getMessage(), "\n";
die(1);
}
?>