A telnet client with response parser for network equipment written in PHP.
This library provide two features.
- Telnet client, that extends graze/telnet-client to provide waitForPrompt(), login(), enable(), configure() and several presets for network equipment
- The parser, that converts a supported Cisco IOS command response to the array
The parser is implemented as the integrated with telnet client and as the standalone library. If you want to use your own telnet/SSH client library, you can use the parser as standalone library.
Supported network equipment by the parser and the telnet client are:
- Cisco IOS
- Cisco IOS-XE
Telnet client may work with targeted are:
- Juniper JUNOS (with custom prompt setting)
- Linux and other UNIX like platforms (with custom prompt setting)
- Alaxala network switch
- Extreme XOS, HP Procurve, DLink and other intelligent network switch
- show interface status
- show interface counters
- show mac address-table
- show lldp neighbors
- show ip interface brief
- show arp
Via Composer
composer require marbocub/network-equipment
The following example login to a Cisco IOS switch to get and display the interface status:
<?php
require_once("vendor/autoload.php");
use Marbocub\NetworkEquipment\TelnetClient;
use Graze\TelnetClient\Exception\TelnetException;
$telnet = TelnetClient::factory();
try {
$telnet->connect("127.0.0.1:23");
$telnet->login("username", "password");
$telnet->execute("terminal length 0");
$response = $telnet->execute("show interface status");
echo $response;
print_r($response->getResponseArray());
} catch (TelnetException $e) {
echo $e->getMessage();
die();
}
The following example parse the "show int status" command response getted from a Cisco IOS switch:
<?php
require_once("vendor/autoload.php");
use Marbocub\NetworkEquipment\ResponseParser;
/* executed command and the response */
$command = "show int status";
$response = $yourSSHClient->execute($command);
echo $response;
/* start parse */
$parser = new ResponseParser();
$result = $parser->parse($command, $response);
print_r($result);
The result of both examples is:
Port Name Status Vlan Duplex Speed Type
Te1/0/1 description connected trunk full 10G SFP-10GBase-SR
Po1 connected trunk a-full a-1000
Array
(
[Te1/0/1] => Array
(
[Port] => 'Te1/0/1',
[Name] => 'description',
[Status] => 'connected',
[Vlan] => 'trunk',
[Duplex] => 'full',
[Speed] => '10G',
[Type] => 'SFP-10GBase-SR',
)
[Po1] => Array
(
[Port] => 'Po1',
[Name] => '',
[Status] => 'connected',
[Vlan] => 'trunk',
[Duplex] => 'a-full',
[Speed] => 'a-1000',
[Type] => '',
)
)
To instantiation a client, use the TelnetClient::factory() that return a TelnetClient instance:
require_once("vendor/autoload.php");
use Marbocub\NetworkEquipment\TelnetClient;
use Graze\TelnetClient\Exception\TelnetException;
$telnet = TelnetClient::factory();
To login a network equipment, use the connect() method to create a socket first and then use the login() method:
try {
$telnet->connect("127.0.0.1:23");
$telnet->login("username", "password");
} catch (TelnetException $e) {
/* failed */
}
Note: The difference of connect() between this library and the graze/telnet-client is the preset prompts for Cisco IOS.
Once connected, the execute() method can be used to write command to the socket and receive the response:
try {
$telnet->execute("terminal length 0");
$response = $telnet->execute("show interface status");
// Getting the response text
echo $response;
// Getting the parsed array
print_r($response->getResponseArray());
} catch (TelnetException $e) {
/* failed */
}
Note: Must be execute "terminal length 0" first for Cisco IOS.
Note: getResponseArray() calls the parser internally. Only works well with supported Cisco IOS command executed.
To enable privileged mode for Cisco IOS, the enable() method can be used:
try {
$telnet->enable("password");
} catch (TelnetException $e) {
/* failed */
}
To configure Cisco IOS, the configure() method can be used:
$commands = [
'interface Gi1/0/1',
'switchport access vlan 100',
];
try {
$telnet->configure($commands);
} catch (TelnetException $e) {
/* failed */
}
Note: The first argument of the configure() is an array listing the commands to execute.
You can specify the following prompt constant at the 2nd argument of connect().
- For Juniper JUNOS
$telnet->connect("127.0.0.1:23', TelnetClient::PROMPT_JUNOS);
- For Linux or other UNIX platforms
$telnet->connect("127.0.0.1:23', TelnetClient::PROMPT_SHELL);
require_once("vendor/autoload.php");
use Marbocub\NetworkEquipment\ResponseParser;
$parser = new ResponseParser();
The first argument of parse() is the executed Cisco IOS command. Required for the parser to select the format.
$result = $parser->parse(
"executed command",
"response text"
);
Telnet client of this library requires the PHP's low-level sockets extension (ext-sockets) for the underlying library.
- marbocub - Initial work
This project is licensed under the MIT License - see the LICENSE file for details.