|
NAMENet::Appliance::Session - Run command-line sessions to network appliancesSYNOPSISuse Net::Appliance::Session; my $s = Net::Appliance::Session->new({ personality => 'ios', transport => 'SSH', host => 'hostname.example', privileged_paging => 1, # only if using ASA/PIX OS 7+ # and there are other behaviour options, see below }); try { $s->connect({ username => 'username', password => 'loginpass' }); $s->begin_privileged({ password => 'privilegedpass' }); print $s->cmd('show access-list'); $s->end_privileged; } catch { warn "failed to execute command: $_"; } finally { $s->close; }; or, try the bundled "nas" helper script (beta feature!): nas --help DESCRIPTIONUse this module to establish an interactive command-line session with a network appliance. There is special support for moving into "privileged" mode and "configure" mode, along with the ability to send commands to the connected device and retrieve returned output.There are other CPAN modules that cover similar ground, but they are less robust and do not handle native SSH, Telnet and Serial Line connections with a single interface on both Unix and Windows platforms. Built-in commands come from a phrasebook which supports many network device vendors (Cisco, HP, etc) or you can install a new phrasebook. Most phases of the connection are configurable for different device behaviours. METHODSAs in the synopsis above, the first step is to create a new instance.Recommended practice is to wrap all other calls (except "close()") in a "try" block, to catch errors (typically time-outs waiting for CLI response). This module exports the "try/catch/finally" methods (from Try::Tiny) into your namespace as a simpler alternative to using "eval()". For a full demonstration of usage, see the example script shipped with this distribution. Net::Appliance::Session->new( \%options )my $s = Net::Appliance::Session->new({ personality => 'ios', transport => 'SSH', host => 'hostname.example', }); Prepares a new session for you, but will not connect to any device. Some options are required, others optional:
connect( \%options )$s->connect({ username => $myname, password => $mysecret }); To establish a connection to the device, and possibly also log in, call this method. Following a successful connection, paging of device output will be disabled using commands appropriate to the platform. This feature can be suppressed (see "CONFIGURATION", below). Options available to this method, sometimes required, are:
begin_privileged and end_privileged$s->begin_privileged; # do some work $s->end_privileged; Once you have connected to the device, change to "privileged" mode by calling the "begin_privileged" method. The appropriate command will be issued for your device platform, from the phrasebook. Likewise to exit "privileged" mode call the "end_privileged" method. Sometimes authentication is required to enter "privileged" mode. In that case, the module defaults to using the username and password first passed in the "connect" method. However to either override those or set them in case they were not passed to "connect", use either or both of the following options to "begin_privileged": $s->begin_privileged({ username => $myname, password => $mysecret }); begin_configure and end_configure$s->begin_configure; # make some changes $s->end_configure; To enter "configuration" mode for your device platform, call the "begin_configure" method. This checks you are already in "privileged" mode, as the module assumes this is necessary. If it isn't necessary then see "CONFIGURATION" below to modify this behaviour. Likewise to exit "configure" mode, call the "end_configure" method. cmd( $command )my $config = $s->cmd('show running-config'); my @interfaces = $s->cmd('show interfaces brief'); Execute a single command statement on the connected device. The statement is executed verbatim on the device, with a newline appended. In scalar context the response is returned as a single string. In list context the gathered response is returned as a list of lines. In both cases your local platform's newline character will end all lines. You can also call the "last_response" method which returns the same data with the same contextual behaviour. This method accepts a hashref of options following the $command, which can include a "timeout" value to permit long running commands to have all their output gathered. To handle more complicated interactions, for example commands which prompt for confirmation or optional parameters, you should use a Macro. These are set up in the phrasebook and issued via the "$s->macro($name)" method call. See the Phrasebook and Cookbook manual pages for further details. If you receive response text with a "mangled" copy of the issued command at the start, then it's likely you need to set the terminal width. This prevents the connected device from line-wrapping long commands. Issue something like: $s->begin_privileged; $s->cmd('terminal width 510'); close$s->close; Once you have finished work with the device, call this method. It attempts to back out of any "privileged" or "configuration" mode you've entered, re-enable paging (unless suppressed) and then disconnect. If a macro named "disconnect" exists in the loaded phrasebook then it's called just before disconnection. This allows you to issue a command such as "exit" to cleanly log out. CONFIGURATIONEach of the entries below may either be passed as a parameter in the options to the "new" method, or called as a method in its own right and passed the appropriate setting. If doing the latter, it should be before you call the "connect" method.
ASYNCHRONOUS BEHAVIOURThe standard, and recommended way to use this module is as above, whereby the application is blocked waiting for command response. It's also possible to send a command, and separately return to ask for output at a later time.$s->say('show clock'); This will send the command "show clock" to the connected device, followed by a newline character. $s->gather(); This will gather and return output, with similar behaviour to "cmd()", above. That is, it blocks waiting for output and a prompt, will timeout, and accepts the same options. You can still use "last_response" after calling "gather", however be aware that the command (from "say") may be echoed at the start of the output, depending on device and connection transport. DIAGNOSTICSTo see a log of all the processes within this module, and a copy of all data sent to and received from the device, call the following method:$s->set_global_log_at('notice'); In place of "notice" you can have other log levels (e.g. "debug" for more, or "info" for less), and via the embedded Logger at "$s->nci->logger" it's possible to finely control the diagnostics. INTERNALSSee Net::CLI::Interact.THANKSOver several years I have received many patches and suggestions for improvement from users of this module. My heartfelt thanks to all, for their contributions.AUTHOROliver Gorwits <oliver@cpan.org>COPYRIGHT AND LICENSEThis software is copyright (c) 2019 by Oliver Gorwits.This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
Visit the GSP FreeBSD Man Page Interface. |