NAME

PDK::Device::Role - A Moose role providing basic interaction functionality with network devices

SYNOPSIS

package PDK::Device::Cisco;
with "PDK::Device::Role";

# Initialize the object
my $device = PDK::Device::Cisco->new(
  host => '192.168.1.1',
  username => 'admin',
  password => 'password',
);

# Login to the device
my $login_status = $device->login();
if ($login_status->{success}) {
  $device->send('show running-config');
}

# Retrieve running configuration
$device->getConfig();

# FTP configuration backup
$device->ftpConfig();

# Execute commands in normal mode
$device->execCommands(['show ip int b', 'show ver']);

# Execute commands in privileged mode, automatically entering config view
$device->runCommands(['show ver', 'show lldp n']);

# Write configuration information to a text file
$device->write_file($device->getConfig(), '192.168.1.1_startup.txt');

DESCRIPTION

PDK::Device::Role is a Moose role that provides basic interaction functionality with network devices. It includes methods for logging in, sending commands, error handling, and managing device connections. This module is designed to be used as a Role for specific device type implementations.

ENVIRONMENT VARIABLES

PDK::Device::Role supports the following environment variables:

PDK_DEVICE_USERNAME

Device login username.

PDK_DEVICE_PASSWORD

Device login password.

PDK_DEVICE_ENPASSWORD

Device enable mode password.

PDK_FTP_SERVER

FTP server address.

PDK_FTP_USERNAME

FTP username.

PDK_FTP_PASSWORD

FTP password.

PDK_DEVICE_CONFIG_HOME

Root directory for configuration backups, defaults to /opt/pdk.

PDK_DEVICE_DEBUG

Project debug level: 1 - Print logs 2 - Print CLI interaction 3 - Print Expect low-level details

PDK_CATCH_ERROR

Whether to enable exception code interception, supports only 1 (enabled) and 0 (disabled); values other than 1 will be converted to 0.

ATTRIBUTES

exp

Expect object used for interaction with the device.

host

Device hostname or IP address.

port

Device connection port, defaults to an empty string.

proto

Connection protocol, defaults to SSH.

prompt

Normal prompt, defaults to '\S+[#>]\s*\z'.

enPrompt

Enable prompt.

enCommand

Command to enter enable mode.

username

Device username, defaults to an empty string.

password

Device password, defaults to an empty string.

enPassword

Enable mode password.

passphrase

Key phrase, defaults to an empty string.

mode

Current mode, defaults to 'normal'.

catchError

Whether to catch errors, defaults to 1 (enabled).

enabled

Device enable mode status, defaults to 0 (disabled).

status

Device login status, defaults to 0 (not logged in).

month

Current month formatted as YYYY-MM.

date

Current date formatted as YYYY-MM-DD.

workdir

Root directory for configuration files, defaults to PDK_CONFIG_HOME environment variable or user's home directory.

debug

Debug switch, defaults to 0 (disabled).

METHODS

login

Attempts to log in to the device. If already logged in, it returns a success status directly.

connect($args)

Establish a connection with the device.

Return values: - Return status, 1 for success, -1 for failure.

send($command)

Send a command to the device.

enable

Switch to enable mode.

Return values: - 1 for success, 0 for failure.

execCommands($commands)

Execute a series of commands and process the results.

Parameters: - $commands: List of commands as an array reference.

Return values: - On success, returns { success => 1, result => 'Execution result' } - On failure, returns { success => 0, failCommand => 'Failed command', reason => 'Error reason', snapshot => 'Relevant snapshot' }

write_file($config, $name)

Write the configuration to the specified file.

Parameters: - $config: The configuration content to be written. - $name: Optional, the filename (defaults to hostname plus ".txt").

SEE ALSO

https://github.com/railsboot/PDK-Device.

AUTHOR

WENWU YAN <968828@gmail.com>

LICENSE AND COPYRIGHT

Copyright (C) 2024 WENWU YAN

This program is free software; you can redistribute it and/or modify it under the terms of either: the GNU General Public License as published by the Free Software Foundation; or the Artistic License.