Guest User

Untitled

a guest
Jul 15th, 2018
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.97 KB | None | 0 0
  1. #!/opt/SPECTRUM/bin/perl -w
  2.  
  3. # This script will capture the running configuration of a
  4. # Cisco SAN-OS device through an SSH session and print it to STDOUT.
  5. #
  6. # Error Codes:
  7. # 0 = Success
  8. # 255 = Usage error
  9. # 254 = Invalid timeout value
  10. # 252 = Login error
  11. # 249 = Exec prompt not found error
  12. # 244 = Error retrieving configuration
  13. # 245 = Insufficient privileges
  14. # 253 = Unexpected output
  15. #
  16.  
  17. use strict;
  18. use warnings;
  19. use Net::SSH::Expect;
  20.  
  21. $ENV{'PATH'} = "/usr/bin:". $ENV{'PATH'};
  22.  
  23. ### Main ###
  24. if( $#ARGV != 4 && $#ARGV != 5 )
  25. {
  26. print "Usage: capture_running.pl <device IP> <user> <pass> <enable_pass>
  27. <login_timeout_in_seconds> <capture_timeout_in_seconds>n";
  28. print STDERR "Usage: capture_running.pl <deviceIP> <user> <pass>
  29. <enable_pass> <login_timeout_in_seconds> <capture_timeout_in_seconds>n";
  30. exit 255;
  31. }
  32. elsif( $ARGV[4] < 1 || $ARGV[4] > 600 )
  33. {
  34. print "$ARGV[4] is the login timeout and must be an int between 1 and 600 secondsn";
  35. print STDERR "$ARGV[4] is the login timeout and must be an int between 1 and 600 secondsn";
  36. exit 254;
  37. }
  38. elsif( $#ARGV == 5 && ( $ARGV[5] < 1 || $ARGV[5] > 600 ) )
  39. {
  40. print "$ARGV[5] is the capture timeout and must be an int between 1 and 600 secondsn";
  41. print STDERR "$ARGV[5] is the capture timeout and must be an int between 1 and 600 secondsn";
  42. exit 254;
  43. }
  44. else
  45. {
  46. my $capture_timeout = $ARGV[4];
  47. if( $ARGV[5] )
  48. {
  49. $capture_timeout = $ARGV[5];
  50. }
  51.  
  52. my $errorCode = 1;
  53. my @data;
  54. my $errorString = "nHost $ARGV[0]: n";
  55.  
  56. ($errorCode, @data) = GetConfig( $ARGV[0], $ARGV[1], $ARGV[2], $ARGV[3],
  57. $ARGV[4], $capture_timeout );
  58.  
  59. if( $errorCode == 0 )
  60. {
  61. # Success. The running configuration
  62. # content is in the data variable
  63.  
  64. foreach ( @data ) { print "$_n" }; # print the configuration to STDOUT
  65. exit 0;
  66. }
  67. else
  68. {
  69. print STDERR $errorString;
  70.  
  71. if( $errorCode == 245 )
  72. {
  73. print STDERR join " ", @data, "nEnsure that the device user has
  74. sufficient privileges to disable paging and view the confign";
  75. }
  76. else
  77. {
  78. print STDERR join " ", @data, "n";
  79. }
  80.  
  81. exit $errorCode;
  82. }
  83. }
  84.  
  85. exit 0;
  86.  
  87. sub GetConfig
  88. {
  89. my $deviceIP=shift;
  90. my $user=shift;
  91. my $pass=shift;
  92. my $epass=shift;
  93. my $login_timeout=shift;
  94. my $capture_timeout=shift;
  95. my @config;
  96. my $msg;
  97.  
  98. my $ssh = Net::SSH::Expect->new ( host => $deviceIP,
  99. user => $user,
  100. password=> $pass,
  101. raw_pty => 1,
  102. no_terminal => 0,
  103. timeout => $login_timeout
  104. );
  105.  
  106.  
  107. my $login_output;
  108. eval { $login_output = $ssh->login(); };
  109.  
  110. if( $@ )
  111. {
  112. $msg = "Login has failed. Output: $login_output";
  113. return( 252, $msg );
  114. }
  115.  
  116. # login output should contain the right prompt characters
  117. if( $login_output !~ />s*z/ )
  118. {
  119. $msg = "Login has failed. Didn't see device prompt as expected.";
  120. $ssh->close();
  121. return( 252, $msg );
  122. }
  123.  
  124. if( $login_output !~ />s*z/ ) # Replace '#' is the prompt character here
  125. {
  126. # we don't have the '#' prompt, means we still can't exec commands
  127. $msg = "Exec prompt not found.";
  128. $ssh->close();
  129. return( 249, $msg );
  130. }
  131.  
  132. my $elogin = $ssh->exec("en");
  133.  
  134. my $elogin2 = $ssh->exec($epass);
  135.  
  136.  
  137. if( $elogin2 !~ /#s*z/ ) # Replace '#' is the prompt character here
  138. {
  139. $msg = "Exec prompt not found.";
  140. $ssh->close();
  141. return( 249, $msg );
  142. }
  143.  
  144. # disable paging
  145. # different commands for different devices, if they don't
  146. # work then we will get messages about problems later
  147. # specifically the "No prompt after 'sh run'" error
  148. # errmsg doesn't get set when these error and if we use print
  149. # and getlines to read for errors it causes problems with print "sh run"
  150. # later.
  151. # $ssh->exec( "term pager 0" );
  152. my $paging = $ssh->exec( "term pager 0" );
  153. if ( $paging =~ /s?%s/ )
  154. {
  155. $msg = "Unable to set terminal size to 0 - Insufficient privileges";
  156. $ssh->close();
  157. return( 245, $msg);
  158. }
  159.  
  160. $ssh->send( "sh run" );
  161. $ssh->timeout( $capture_timeout );
  162. $ssh->peek(0);
  163.  
  164. while( my $line = $ssh->read_line() )
  165. {
  166. # get configuration content
  167.  
  168. if( $line !~
  169. /sh run|Building configuration|Current configuration|^s*$/ )
  170. {
  171. push @config, $line;
  172. }
  173. }
  174.  
  175. if( @config <= 0 )
  176. {
  177. $msg = "No data retrieved, the capture timeout may be too low.";
  178. $ssh->close();
  179. return( 244, $msg );
  180. }
  181.  
  182. if( scalar grep { $_ =~ /^%/ } @config )
  183. {
  184. # Ensure show running actually returned the config and not an error
  185. # message containing '%'
  186. return( 245, @config );
  187. }
  188.  
  189. return( 0, @config ); # everything was okay, return the captured data
  190. }
Add Comment
Please, Sign In to add comment