Cisco IOS communications and configuration-parsing library for PHP. Provides:
Detain\CiscoParser\CiscoParserβ converts a Cisco IOS running/startup configuration text dump into a nested associative-array tree based on indentation depth.Detain\CiscoParser\CiscoLoaderβ SSH transport (via PECLssh2) that drives an interactive shell against a real device and parses the output of commonshowcommands (show int status,show int <int>,show log,show arp,show mac address-table,show ipv6 neighbors,show ipv6 routers, DHCP snooping bindings, trunk ports, VLANs, err-disabled, β¦).
| Site | Status |
|---|---|
![]() |
|
![]() |
|
![]() |
![]() |
![]() |
|
![]() |
|
![]() |
- PHP 7.4 or newer
ext-mbstringext-ssh2β only required if you useCiscoLoaderto talk to a real device. The pure-textCiscoParserworks without it.
Install with Composer:
composer require detain/cisco_parserCiscoParser::parse_cisco_children() takes an array of lines (typically from
explode("\n", file_get_contents(...)) after stripping \r) and produces a
nested tree where each node has:
commandβ the first whitespace-delimited token on the lineargumentsβ (optional) anything after the first tokenchildrenβ (optional) the sub-block of more deeply-indented lines
use Detain\CiscoParser\CiscoParser;
$config = <<<CFG
hostname Switch1
interface FastEthernet0/1
switchport access vlan 10
spanning-tree portfast
interface FastEthernet0/2
description Uplink
CFG;
$lines = explode("\n", str_replace("\r", '', $config));
$parser = new CiscoParser();
$tree = $parser->parse_cisco_children($lines);
print_r($tree);Output:
Array
(
[0] => Array
(
[command] => hostname
[arguments] => Switch1
)
[1] => Array
(
[command] => interface
[arguments] => FastEthernet0/1
[children] => Array
(
[0] => Array
(
[command] => switchport
[arguments] => access vlan 10
)
[1] => Array
(
[command] => spanning-tree
[arguments] => portfast
)
)
)
[2] => Array
(
[command] => interface
[arguments] => FastEthernet0/2
[children] => Array
(
[0] => Array
(
[command] => description
[arguments] => Uplink
)
)
)
)
A small wrapper script is shipped at bin/cisco_parser.php:
php bin/cisco_parser.php /path/to/show-running-config.txtIf installed via Composer, the same script is exposed at
vendor/bin/cisco_parser.php.
The wrapper recognises an optional Building configuration... /
Current configuration : N bytes header (as emitted by show running-config)
and pulls the byte count out into the result. Files without that header are
parsed in full.
CiscoLoader opens an SSH session and exposes typed helpers around the most
common show commands. It requires the ssh2 PECL extension.
use Detain\CiscoParser\CiscoLoader;
$device = new CiscoLoader('switch1.example.net', 'admin', 's3cret');
// Implicit connect on first exec() because $autoconnect is true by default.
$status = $device->show_int_status();
foreach ($status as $port) {
echo "{$port['interface']}\t{$port['status']}\t{$port['vlan']}\n";
}
// Other helpers
$device->show_log(); // requires enable mode (#)
$device->show_int('Gi1/0/1');
$device->trunk_ports();
$device->vlans();
$device->errdisabled();
$device->dhcpsnoop_bindings();
$device->mac_address_table();
$device->arp_table();
$device->ipv6_neighbor_table();
$device->ipv6_routers();
// Push configuration. Requires enable mode (#).
$device->configure("interface Gi1/0/2\n description new label\n");
$device->write_config(); // saves running-config to startup-config
$device->disconnect();CiscoLoader::exec($cmd) is available for any other command not covered by a
helper; it returns the device's raw response with the trailing prompt stripped.
composer install
composer testFor coverage output:
composer test-coverage
# β tests/ + coverage.xml (clover format)The Cisco Parser library is licensed under the LGPL-2.1-only license.








