junos_dump.pl 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. #! /usr/bin/perl -w
  2. #############################################################################
  3. # (c) 2001, 2003 Juniper Networks, Inc. #
  4. # (c) 2011 Sebastian "tokkee" Harl <sh@teamix.net> #
  5. # and team(ix) GmbH, Nuernberg, Germany #
  6. # #
  7. # This file is part of "team(ix) Monitoring Plugins" #
  8. # URL: http://oss.teamix.org/projects/monitoringplugins/ #
  9. # It is based on the example diagnose_bgp.pl script of the #
  10. # JUNOScript distribution. #
  11. # #
  12. # All rights reserved. #
  13. # Redistribution and use in source and binary forms, with or without #
  14. # modification, are permitted provided that the following conditions #
  15. # are met: #
  16. # 1. Redistributions of source code must retain the above copyright #
  17. # notice, this list of conditions and the following disclaimer. #
  18. # 2. Redistributions in binary form must reproduce the above copyright #
  19. # notice, this list of conditions and the following disclaimer in the #
  20. # documentation and/or other materials provided with the distribution. #
  21. # 3. The name of the copyright owner may not be used to endorse or #
  22. # promote products derived from this software without specific prior #
  23. # written permission. #
  24. # #
  25. # THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR #
  26. # IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED #
  27. # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE #
  28. # DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, #
  29. # INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES #
  30. # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR #
  31. # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) #
  32. # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, #
  33. # STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING #
  34. # IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE #
  35. # POSSIBILITY OF SUCH DAMAGE. #
  36. #############################################################################
  37. use strict;
  38. use warnings;
  39. use JUNOS::Device;
  40. use JUNOS::Trace;
  41. use Getopt::Std;
  42. use Term::ReadKey;
  43. use File::Basename;
  44. use Data::Dumper;
  45. my $jnx;
  46. # send a query
  47. sub send_query
  48. {
  49. my $device = shift;
  50. my $query = shift;
  51. my $href_queryargs = shift;
  52. my $res;
  53. unless ( ref $href_queryargs ) {
  54. eval {
  55. $res = $device->$query();
  56. };
  57. if ($@) {
  58. $res = $device->command($query);
  59. }
  60. } else {
  61. my %queryargs = %$href_queryargs;
  62. print "$_ => $queryargs{$_}\n" foreach (keys %queryargs);
  63. $res = $device->$query(%queryargs);
  64. }
  65. unless ( ref $res ) {
  66. print STDERR "ERROR: Failed to execute query '$query'\n";
  67. return 0;
  68. }
  69. unless (ref $res) {
  70. print STDERR "ERROR: failed to execute command $query\n";
  71. return undef;
  72. }
  73. my $err = $res->getFirstError();
  74. if ($err) {
  75. print STDERR "ERROR: ", $err->{message}, "\n";
  76. return 0;
  77. }
  78. return $res;
  79. }
  80. # print the usage of this script
  81. sub output_usage
  82. {
  83. my $usage = "Usage: $0 [options] <target> <query>
  84. Where:
  85. <target> The hostname of the target router.
  86. <query> The query to send to the target router.
  87. Options:
  88. -l <login> A login name accepted by the target router.
  89. -p <password> The password for the login name.
  90. -m <access> Access method. It can be clear-text, ssl, ssh or telnet. Default: telnet.
  91. -o <file> Output file. Default: dump.xml.
  92. -d Turn on debug, full blast.\n\n";
  93. die $usage;
  94. }
  95. my %opt;
  96. getopts('l:p:dm:x:o:h', \%opt) || output_usage();
  97. output_usage() if $opt{h};
  98. # Check whether trace should be turned on
  99. JUNOS::Trace::init(1) if $opt{d};
  100. my $hostname = shift || output_usage();
  101. my $query = shift || output_usage();
  102. my %args = map { split m/=/, $_ } @ARGV;
  103. if ($opt{d}) {
  104. print "Args:\n";
  105. foreach my $key (keys %args) {
  106. print "\t$key => $args{$key}\n";
  107. }
  108. }
  109. # Retrieve the access method, can only be telnet or ssh.
  110. my $access = $opt{m} || "telnet";
  111. use constant VALID_ACCESSES => "telnet|ssh|clear-text|ssl";
  112. output_usage() unless (VALID_ACCESSES =~ /$access/);
  113. # Check whether login name has been entered. Otherwise prompt for it
  114. my $login = "";
  115. if ($opt{l}) {
  116. $login = $opt{l};
  117. } else {
  118. print "login: ";
  119. $login = ReadLine 0;
  120. chomp $login;
  121. }
  122. # Check whether password has been entered. Otherwise prompt for it
  123. my $password = "";
  124. if ($opt{p}) {
  125. $password = $opt{p};
  126. } else {
  127. print "password: ";
  128. ReadMode 'noecho';
  129. $password = ReadLine 0;
  130. chomp $password;
  131. ReadMode 'normal';
  132. print "\n";
  133. }
  134. # Get the name of the output file
  135. my $outfile = $opt{o} || 'dump.xml';
  136. # Retrieve command line arguments
  137. my %deviceinfo = (
  138. access => $access,
  139. login => $login,
  140. password => $password,
  141. hostname => $hostname,
  142. 'ssh-compress' => 0,
  143. );
  144. #
  145. # CONNECT TO the JUNOScript server
  146. # Create a device object that contains all necessary information to
  147. # connect to the JUNOScript server at a specific router.
  148. #
  149. $jnx = new JUNOS::Device(%deviceinfo);
  150. unless ( ref $jnx ) {
  151. die "ERROR: $deviceinfo{hostname}: failed to connect.\n";
  152. }
  153. my $res = send_query($jnx, $query, scalar(keys %args) ? \%args : undef);
  154. if ($res) {
  155. if ($outfile eq "-") {
  156. print STDOUT $res->toString;
  157. }
  158. else {
  159. $res->printToFile($outfile);
  160. }
  161. }
  162. print "DONE\n";
  163. $jnx->request_end_session();
  164. $jnx->disconnect();