Here's the "capture startup" I'm using (it's a rip off of the SSH one from the
DACHSUG WIKI)
Just change "sh startup" to "sh run" for the "capture running config" script.
#!/opt/SPECTRUM/bin/perl -w
# This script will capture the startup configuration of a
# Cisco IOS OS SSH device and print it to STDOUT.
#
# Error Codes:
# 0 = Success
# 255 = Usage error
# 254 = Invalid timeout value
# 252 = Connection error
# 251 = Login error
# 249 = Enable error
# 244 = Error retrieving configuration
# 253 = Unexpected output
#
use strict;
use warnings;
use Net::SSH::Expect;
$ENV{'PATH'} = "/usr/bin:". $ENV{'PATH'};
### Main ###
if( $#ARGV != 4 && $#ARGV != 5 )
{
print "Usage: capture_startup.pl <device IP> <user> <pass> <enable_pass>
<login_timeout_in_seconds> <capture_timeout_in_seconds>\n";
print STDERR "Usage: capture_startup.pl <deviceIP> <user> <pass>
<enable_pass> <login_timeout_in_seconds> <capture_timeout_in_seconds>\n";
exit 255;
}
elsif( $ARGV[4] < 1 || $ARGV[4] > 600 )
{
print "$ARGV[4] is the login timeout and must be an int between 1 and 600
seconds\n";
print STDERR "$ARGV[4] is the login timeout and must be an int between 1
and 600 seconds\n";
exit 254;
}
elsif( $#ARGV == 5 && ( $ARGV[5] < 1 || $ARGV[5] > 600 ) )
{
print "$ARGV[5] is the capture timeout and must be an int between 1 and 600
seconds\n";
print STDERR "$ARGV[5] is the capture timeout and must be an int between 1
and 600 seconds\n";
exit 254;
}
else
{
my $capture_timeout = $ARGV[4];
if( $ARGV[5] )
{
$capture_timeout = $ARGV[5];
}
my $errorCode = 1;
my @data;
my $errorString = "\nHost $ARGV[0]: \n";
($errorCode, @data) = GetConfig( $ARGV[0], $ARGV[1], $ARGV[2], $ARGV[3],
$ARGV[4], $capture_timeout );
if( $errorCode == 0 )
{
# Success. The startup configuration
# content is in the data variable
foreach ( @data ) { print "$_\n" }; # print the configuration to STDOUT
exit 0;
}
else
{
print STDERR $errorString;
if( $errorCode == 253 )
{
print STDERR join " ", @data, "\nEnable password may be invalid\n";
}
else
{
print STDERR join " ", @data, "\n";
}
exit $errorCode;
}
}
exit 0;
sub GetConfig
{
my $deviceIP=shift;
my $user=shift;
my $pass=shift;
my $epass=shift;
my $login_timeout=shift;
my $capture_timeout=shift;
my @config;
my $msg;
my $ssh = Net::SSH::Expect->new ( host => $deviceIP,
user => $user,
password=> $pass,
raw_pty => 1,
no_terminal => 1,
timeout => $login_timeout
);
my $login_output;
eval { $login_output = $ssh->login(); };
if( $@ )
{
$msg = "Login has failed. Output: $login_output";
return( 252, $msg );
}
#first try login without username and just password
if( $login_output !~ /[\#\>]\s*\z/ )
{
$msg = "Login has failed. Didn't see device prompt as expected.";
$ssh->close();
return( 252, $msg );
}
if( $login_output !~ /\#\s*\z/ )
{
my $enable = $ssh->exec( "enable" );
# if we have a password prompt after enable, send the password
if( $enable =~ /[Pp]assword:/ )
{
my $enablepass = $ssh->exec( $epass );
# did the enable password fail?
if( $enablepass !~ /\#\s*\z/ )
{
$msg = "Enable password failed.";
$ssh->close( );
return( 249, $msg );
}
}
# we didn't require a password, but did we get the enable prompt?
elsif( $enable !~ /\#\s*\z/ )
{
$msg = "Enable mode prompt not found.";
$ssh->close();
return( 249, $msg );
}
}
# disable paging
# different commands for different devices, if they don't
# work then we will get messages about problems later
# specifically the "No prompt after 'sh run'" error
# errmsg doesn't get set when these error and if we use print
# and getlines to read for errors it causes problems with print "sh run"
# later.
$ssh->exec( "term pager 0" );
$ssh->send( "sh start" );
$ssh->timeout( $capture_timeout );
$ssh->peek(0);
while( my $line = $ssh->read_line() )
{
# get configuration content
if( $line !~
/sh start|Building configuration|Current configuration|^\s*$/ )
{
push @config, $line;
}
}
if( @config <= 0 )
{
$msg = "No data retrieved, the capture timeout may be too low.";
$ssh->close();
return( 244, $msg );
}
if( scalar grep { $_ =~ /^%/ } @config )
{
# Ensure show start actually returned the config and not an error
message containing '%'
return( 253, @config );
}
return( 0, @config ); # everything was okay, return the captured data
}
David K. Game
Consultant – Managed Services
Logicalis UK Ltd
110 Buckingham Avenue
Slough, Berkshire, SL1 4PF
www.uk.logicalis.com
_________________________________________________
Business and technology working as one
-----Original Message-----
From: [email protected] [mailto:[email protected]]
Sent: 21 February 2013 18:06
To: spectrum
Subject: [spectrum] Host Configuration Manager
Hi
I like capture the configuration to Cisco PIX by ssh or tftp, the question is,
any one have the script for that????
Thanks
Maynard Suarez
Especialista Junior.
Data Center Hatillo.
“Con la finalidad de brindarle un mejor servicio, hemos dispuesto el
alias: <[email protected]>, para el procesamiento de sus sugerencias,
agradecimientos o reclamos.”
---
To unsubscribe from spectrum, send email to [email protected] with the body:
unsubscribe spectrum [email protected]
Please be aware that Logicalis UK Ltd may monitor email traffic data and also
email content for security purposes.
______________________________________________________________________
This email has been scanned by the Symantec Email Security.cloud service.
For more information please visit http://www.symanteccloud.com
______________________________________________________________________
This email communication does not create or vary any contractual relationship
between Logicalis and you. Internet communications are not secure and
accordingly Logicalis does not accept any legal liability for the contents of
this message. The contents of this email are confidential to the intended
recipient at the email address to which it has been addressed. It may not be
disclosed to or used by anyone other than this addressee, nor may it be copied
in any way. If received in error, please contact Logicalis on the above
switchboard number quoting the name of the sender and the addressee and then
delete it from your system. Please note that neither Logicalis nor the sender
accepts any responsibility for viruses and it is your responsibility to scan
the email and attachments (if any).
Please be aware that Logicalis UK Ltd may monitor email traffic data and also
email content for security purposes.
Logicalis UK Ltd, Registered in England and Wales No: 3732397, Registered
Office: 110 Buckingham Avenue, Slough. Berkshire, SL1 4PF
______________________________________________________________________
This email has been scanned by the Symantec Email Security.cloud service.
For more information please visit http://www.symanteccloud.com
______________________________________________________________________
---
To unsubscribe from spectrum, send email to [email protected] with the body:
unsubscribe spectrum [email protected]