| 1 | package Mnet; | ||
| 2 | |||
| 3 | # version number used by Makefile.PL | ||
| 4 | # these should be set to "dev", expect when creating a new release | ||
| 5 | # refer to developer build notes in Makefile.PL for more info | ||
| 6 | 441 | our $VERSION = "5.26"; | |
| 7 | |||
| 8 | =head1 NAME | ||
| 9 | |||
| 10 | Mnet - Testable network automation and reporting | ||
| 11 | |||
| 12 | =head1 SYNOPSIS | ||
| 13 | |||
| 14 | # sample.pl script to report Loopback0 ip on cisco devices | ||
| 15 | # | ||
| 16 | # demonstrates typical use of all major Mnet modules | ||
| 17 | # | ||
| 18 | # --help to list all options, also --help <option> | ||
| 19 | # --device <address> to connect to device with logging | ||
| 20 | # --username and --password should be set if necessary | ||
| 21 | # --debug to generate extra detailed logging outputs | ||
| 22 | # --batch <file.batch> to process multiple --device lines | ||
| 23 | # --report csv:<file.csv> to create an output csv file | ||
| 24 | # --record <file.test> to create replayable test file | ||
| 25 | # --test --replay <file.test> for regression test output | ||
| 26 | # | ||
| 27 | # refer to various Mnet modules' perldoc for more info | ||
| 28 | |||
| 29 | # load needed modules | ||
| 30 | use warnings; | ||
| 31 | use strict; | ||
| 32 | use Mnet::Batch; | ||
| 33 | use Mnet::Expect::Cli::Ios; | ||
| 34 | use Mnet::IP; | ||
| 35 | use Mnet::Log qw(DEBUG INFO WARN FATAL); | ||
| 36 | use Mnet::Opts::Cli; | ||
| 37 | use Mnet::Report::Table; | ||
| 38 | use Mnet::Stanza; | ||
| 39 | use Mnet::Test; | ||
| 40 | |||
| 41 | # define --device, --username, --password, and --report cli options | ||
| 42 | # record, redact, default, and help option attributes are shown | ||
| 43 | Mnet::Opts::Cli::define({ getopt => "device=s", record => 1 }); | ||
| 44 | Mnet::Opts::Cli::define({ getopt => "username=s" }); | ||
| 45 | Mnet::Opts::Cli::define({ getopt => "password=s", redact => 1 }); | ||
| 46 | Mnet::Opts::Cli::define({ getopt => "report=s", default => undef, | ||
| 47 | help_tip => "specify report output, csv, json, sql, etc", | ||
| 48 | help_text => "perldoc Mnet::Report::Table for more info", | ||
| 49 | }); | ||
| 50 | |||
| 51 | # create object to access command line options and Mnet env variable | ||
| 52 | # export Mnet="--password '<secret>'" env var from secure file | ||
| 53 | my $cli = Mnet::Opts::Cli->new("Mnet"); | ||
| 54 | |||
| 55 | # define output --report table, will include first of any errors | ||
| 56 | # use --report cli opt to output data as csv, json, or sql, etc | ||
| 57 | my $report = Mnet::Report::Table->new({ | ||
| 58 | columns => [ | ||
| 59 | device => "string", | ||
| 60 | error => "error", | ||
| 61 | ip => "string", | ||
| 62 | ], | ||
| 63 | output => $cli->report, | ||
| 64 | }); | ||
| 65 | |||
| 66 | # fork children if in --batch mode, cli opts set for current child | ||
| 67 | # process one device or ten thousand devices with the same script | ||
| 68 | # exit --batch parent process here when finished forking children | ||
| 69 | $cli = Mnet::Batch::fork($cli); | ||
| 70 | exit if not $cli; | ||
| 71 | |||
| 72 | # output report row for device error if script dies before finishing | ||
| 73 | $report->row_on_error({ device => $cli->device }); | ||
| 74 | |||
| 75 | # call logging function, also create log object for current --device | ||
| 76 | FATAL("missing --device") if not $cli->device; | ||
| 77 | my $log = Mnet::Log->new({ log_id => $cli->device }); | ||
| 78 | $log->info("processing device"); | ||
| 79 | |||
| 80 | # uncomment the push commands below to skip ssh host key checks | ||
| 81 | # ideally host keys are already accepted, perhaps via manual ssh | ||
| 82 | my @ssh = qw(ssh); | ||
| 83 | #push @ssh, qw(-o StrictHostKeyChecking=no); | ||
| 84 | #push @ssh, qw(-o UserKnownHostsFile=/dev/null); | ||
| 85 | |||
| 86 | # create an expect ssh session to current --device | ||
| 87 | # log ssh login/auth prompts as info, instead of default debug | ||
| 88 | # password_in set to prompt for password if --password opt not set | ||
| 89 | # for non-ios devices refer to perldoc Mnet::Expect::Cli | ||
| 90 | my $ssh = Mnet::Expect::Cli::Ios->new({ | ||
| 91 | spawn => [ @ssh, "$cli->{username}\@$cli->{device}" ], | ||
| 92 | log_id => $cli->{device}, | ||
| 93 | log_login => "info", | ||
| 94 | password => $cli->password, | ||
| 95 | password_in => 1, | ||
| 96 | }); | ||
| 97 | |||
| 98 | # retrieve ios config using ssh command, warn otherwise | ||
| 99 | my $config = $ssh->command("show running-config"); | ||
| 100 | WARN("unable to read config") if not $config; | ||
| 101 | |||
| 102 | # parse interface loopack0 stanza from device config | ||
| 103 | # returns int loop0 line and lines indented under int loop0 | ||
| 104 | # see perldoc Mnet::Stanza for more ios config templating info | ||
| 105 | my $loop = Mnet::Stanza::parse($config, qr/^interface Loopback0$/); | ||
| 106 | |||
| 107 | # parse primary ip address and mask from loopback config stanza | ||
| 108 | my ($ip, $mask) = (undef, undef); | ||
| 109 | ($ip, $mask) = ($1, $2) if $loop =~ /^ ip address (\S+) (\S+)$/m; | ||
| 110 | |||
| 111 | # calculate cidr value from dotted decimal mask | ||
| 112 | my $cidr = Mnet::IP::cidr($mask); | ||
| 113 | |||
| 114 | # report on parsed loopback0 interface ip address and cidr value | ||
| 115 | $report->row({ device => $cli->device, ip => $ip, cidr => $cidr }); | ||
| 116 | |||
| 117 | # finished | ||
| 118 | exit; | ||
| 119 | |||
| 120 | =head1 DESCRIPTION | ||
| 121 | |||
| 122 | The L<Mnet> modules are for perl programmers who want to create testable | ||
| 123 | network automation and/or reporting scripts as simply as possible. | ||
| 124 | |||
| 125 | The main features are: | ||
| 126 | |||
| 127 | =over | ||
| 128 | |||
| 129 | =item * | ||
| 130 | |||
| 131 | L<Mnet::Expect::Cli::Ios> and L<Mnet::Expect::Cli> modules for reliable | ||
| 132 | automation of cisco ios and other command line sessions, including | ||
| 133 | authentication and command prompt handling. | ||
| 134 | |||
| 135 | =item * | ||
| 136 | |||
| 137 | L<Mnet::Stanza> module for templated config parsing and generation on cisco ios | ||
| 138 | devices and other similar indented stanza text data. | ||
| 139 | |||
| 140 | =item * | ||
| 141 | |||
| 142 | L<Mnet::Report::Table> module for aggregating report data from scripts, | ||
| 143 | supporting output in formats such as csv, json, and sql. | ||
| 144 | |||
| 145 | =item * | ||
| 146 | |||
| 147 | L<Mnet::Test> module can record and replay L<Mnet> script options, connected | ||
| 148 | expect sessions, and compare outputs, speeding development and allowing for | ||
| 149 | integration and regression testing of complex automation scripts. | ||
| 150 | |||
| 151 | =item * | ||
| 152 | |||
| 153 | L<Mnet::Batch> can run automation scripts in batch mode to concurrently process | ||
| 154 | a list of devices, using command line arguments and a device list file. | ||
| 155 | |||
| 156 | =item * | ||
| 157 | |||
| 158 | L<Mnet::Log> and L<Mnet::Tee> modules facilitate easy log, debug, alert and | ||
| 159 | error output from automation scripts, along with redirection to per-device | ||
| 160 | output files. | ||
| 161 | |||
| 162 | =item * | ||
| 163 | |||
| 164 | L<Mnet::Opts::Cli> module for config settings via command line, environment | ||
| 165 | variable, and/or batch device list files, with help, tips, and password | ||
| 166 | redaction. | ||
| 167 | |||
| 168 | =item * | ||
| 169 | |||
| 170 | L<Mnet::IP> module to parse IPv4 and IPv6 addresses and network masks. | ||
| 171 | |||
| 172 | =back | ||
| 173 | |||
| 174 | Most of the L<Mnet> sub-modules can be used independently of each other, | ||
| 175 | unless otherwise noted. | ||
| 176 | |||
| 177 | Refer to the individual modules listed in the SEE ALSO section below | ||
| 178 | for more detail. | ||
| 179 | |||
| 180 | =head1 INSTALLATION | ||
| 181 | |||
| 182 | The L<Mnet> perl modules should work in just about any unix perl environment, | ||
| 183 | some modules require perl 5.12 or newer. | ||
| 184 | |||
| 185 | The latest release can be installed from CPAN | ||
| 186 | |||
| 187 | cpan install Mnet | ||
| 188 | |||
| 189 | Or downloaded and installed from L<https://github.com/menzascripting/Mnet> | ||
| 190 | |||
| 191 | tar -xzf Mnet-X.y.tar.gz | ||
| 192 | cd Mnet-X.y | ||
| 193 | perl Makefile.PL # INSTALL_BASE=/specify/path | ||
| 194 | make test | ||
| 195 | make install | ||
| 196 | |||
| 197 | Check your PERL5LIB environment variable if INSTALL_BASE was used, or if you | ||
| 198 | copied the lib/Mnet directory somewhere instead of using the included | ||
| 199 | Makefile.PL script. Refer to L<ExtUtils::MakeMaker> for more information | ||
| 200 | |||
| 201 | =head1 FAQ | ||
| 202 | |||
| 203 | Below are answers to some frequently asked questions. | ||
| 204 | |||
| 205 | =head2 How should I get started? | ||
| 206 | |||
| 207 | Copy the sample script code from the SYNOPSIS above to a new .pl file, read | ||
| 208 | through the comments, make changes as necessary, use the --debug cli option to | ||
| 209 | troubleshoot execution. | ||
| 210 | |||
| 211 | =head2 What's the easiest way to get more log output? | ||
| 212 | |||
| 213 | Add 'use' commands for both L<Mnet::Log> and L<Mnet::Opts::Set::Debug> to the | ||
| 214 | beginning of your script for more output, mostly from other Mnet modules unless | ||
| 215 | you add L<Mnet::Log> calls, which are a compatible subset of log4perl calls, | ||
| 216 | to your script. | ||
| 217 | |||
| 218 | =head2 How should passwords be secured? | ||
| 219 | |||
| 220 | Environment variables should be used to provide passwords for scripts, not | ||
| 221 | command line options. Command line options can be seen in the system process | ||
| 222 | list by other users. | ||
| 223 | |||
| 224 | The L<Mnet::Opts::Cli> new method allows a named environment variable to be | ||
| 225 | specified that will also be parsed for command line options. Your perl network | ||
| 226 | automation script can be called from a secured shell script that contains the | ||
| 227 | usernames and passwords, this secure shell script accessible only to authorized | ||
| 228 | users, such as in the example below: | ||
| 229 | |||
| 230 | #!/bin/sh | ||
| 231 | # sample.sh script, chmod 700 to restrict access to owner user | ||
| 232 | # works with Mnet::Opts calls in above SYNOPISIS sample.pl script | ||
| 233 | # "$@" passes through all command line options, modify as needed | ||
| 234 | export Mnet='--username <user> --password <secret>' | ||
| 235 | perl -- sample.pl "$@" | ||
| 236 | |||
| 237 | The L<Mnet::Opts::Cli> module define function has a redact property that should | ||
| 238 | be set for password options that should be hidden in L<Mnet::Log> output. | ||
| 239 | |||
| 240 | Also note that the L<Mnet::Expect> module log_expect method is used by the | ||
| 241 | L<Mnet::Expect::Cli> modules to temporarily disable expect session logging | ||
| 242 | during password entry. Any user code bypassing the L<Mnet::Expect::Cli> | ||
| 243 | modules to send passwords directly, using the expect method in the | ||
| 244 | L<Mnet::Expect> module, may need to do the same. | ||
| 245 | |||
| 246 | =head2 Why should I use the Mnet::Expect module? | ||
| 247 | |||
| 248 | The L<Mnet::Expect> module works with the L<Mnet::Log> and L<Mnet::Opts::Cli> | ||
| 249 | modules, for easy logging of normal L<Expect> module activity, with options | ||
| 250 | to control logging, debugging, raw pty, and session tty rows and columns. | ||
| 251 | |||
| 252 | However, you still have to handle all the expect session details, including | ||
| 253 | send and expect calls for logging in, detection of command prompts, capturing | ||
| 254 | device output, etc. It's easier to use the L<Mnet::Expect::Cli> module which | ||
| 255 | handles all of this, if you can. | ||
| 256 | |||
| 257 | =head2 Why should I use the Mnet::Expect::Cli module? | ||
| 258 | |||
| 259 | The L<Mnet::Expect::Cli> module makes it easy to login and obtain outputs from | ||
| 260 | command line interfaces, like ssh. This module builds on the L<Mnet::Expect> | ||
| 261 | module mentioned above, adding features to handle a variety of typical username | ||
| 262 | and password prompts, command prompts, pagination prompts on long outputs, and | ||
| 263 | caching of session command output. | ||
| 264 | |||
| 265 | This module also works with the L<Mnet::Test> module, allowing expect session | ||
| 266 | activity to be recorded and replayed while offline. This can be of tremendous | ||
| 267 | value, during development and for sustainability. | ||
| 268 | |||
| 269 | Refer also the the L<Mnet::Expect::Cli::Ios> module mentioned below, which has | ||
| 270 | a couple of features relevant when working with cisco ios and other similar | ||
| 271 | devices. | ||
| 272 | |||
| 273 | =head2 Why should I use the Mnet::Expect::Cli::Ios module? | ||
| 274 | |||
| 275 | The L<Mnet::Expect::Cli::Ios> builds on the L<Mnet::Expect::Cli> module | ||
| 276 | mentioned above, also handling enable mode authentication, the prompt changes | ||
| 277 | going from user to enable mode, and the prompt changes in configuration modes. | ||
| 278 | |||
| 279 | =head1 AUTHOR | ||
| 280 | |||
| 281 | The L<Mnet> perl distribution has been created and is maintained by Mike Menza. | ||
| 282 | Feedback and bug reports are welcome, feel free to contact Mike via email | ||
| 283 | at <mmenza@cpan.org> with any comments or questions. | ||
| 284 | |||
| 285 | =head1 COPYRIGHT AND LICENSE | ||
| 286 | |||
| 287 | Copyright 2006 Michael J. Menza Jr. | ||
| 288 | |||
| 289 | L<Mnet> is free software: you can redistribute it and/or modify it under | ||
| 290 | the terms of the GNU General Public License as published by the Free Software | ||
| 291 | Foundation, either version 3 of the License, or (at your option) any later | ||
| 292 | version. | ||
| 293 | |||
| 294 | This program is distributed in the hope that it will be useful, but WITHOUT ANY | ||
| 295 | WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A | ||
| 296 | PARTICULAR PURPOSE. See the GNU General Public License for more details. | ||
| 297 | |||
| 298 | You should have received a copy of the GNU General Public License along with | ||
| 299 | this program. If not, see L<http://www.gnu.org/licenses/> | ||
| 300 | |||
| 301 | =head1 SEE ALSO | ||
| 302 | |||
| 303 | L<Mnet::Batch> | ||
| 304 | |||
| 305 | L<Mnet::Expect::Cli> | ||
| 306 | |||
| 307 | L<Mnet::Expect::Cli::Ios> | ||
| 308 | |||
| 309 | L<Mnet::IP> | ||
| 310 | |||
| 311 | L<Mnet::Log> | ||
| 312 | |||
| 313 | L<Mnet::Opts::Cli> | ||
| 314 | |||
| 315 | L<Mnet::Report::Table> | ||
| 316 | |||
| 317 | L<Mnet::Stanza> | ||
| 318 | |||
| 319 | L<Mnet::Test> | ||
| 320 | |||
| 321 | =cut | ||
| 322 | |||
| 323 | # required modules | ||
| 324 | # note that cpan complians if use strict is missing | ||
| 325 | # perl 5.12 or higer required for some Mnet modules, they all use this module | ||
| 326 | # perl 5.8.9 warning: use of "shift" without parentheses is ambiguous | ||
| 327 | # see 'use x.xxx' commands in other modules for additional info | ||
| 328 | # update Makefile.PL and INSTALLATION in this perldoc if changed | ||
| 329 | 441 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 330 | 441 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 331 | 441 | ▶ | use 5.012; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 332 | |||
| 333 | # normal end of package | ||
| 334 | 441 | 1; | |
| 335 | |||