Hi Joe
I did not write our script but I'll copy it below for our reference. Hope it helps you. I'm still looking for a way to write a config.
#!/opt/SPECTRUM/bin/perl -w
# This script will capture the running configuration of a
# Juniper OS 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::Telnet;
### Main ###
if( $#ARGV != 2 && $#ARGV != 3 )
{
print STDERR "Usage: capture_running.pl <deviceIP> <user> <pass> <enable_pass> $ARGV[5]\n";
exit 255;
}
else
{
my $errorCode = 1;
my @data;
my $errorString = "\nHost $ARGV[0]: \n";
($errorCode,@data) = GetConfig( $ARGV[0], $ARGV[1], $ARGV[2], $ARGV[3] );
if( $errorCode == 0 )
{
# Success. The running configuration
# content is in the data variable
for( @data ) { print }; # 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 @config;
my $msg;
my $telnet=new Net::Telnet();
$telnet->errmode('return');
$telnet->prompt('/[\$#>]/');
$telnet->open( $deviceIP );
if( $telnet->errmsg )
{
$msg = "Error connecting to device: ".$telnet->errmsg;
$telnet->close;
return( 252, $msg );
}
#first try login without username and just password
$telnet->waitfor( Match => '/Password:/');
if( $telnet->errmsg )
{
$telnet->login($user, $pass);
if( $telnet->errmsg )
{
$msg = $telnet->errmsg;
$telnet->close;
return( 251, $msg );
}
}
else
{
$telnet->cmd( $pass );
if( $telnet->errmsg )
{
# can't use errmsg as it will give command timed-out
# and we should really indicate bad password
$msg = "login failed: bad password";
$telnet->close;
return( 251, $msg );
}
}
$telnet->print( "set console page 0" );
$telnet->print( "get config" );
if( $telnet->errmsg )
{
$msg = "Device did not accept 'get config' command: ".$telnet->errmsg;
$telnet->close( );
return( 244, $msg );
}
while( my $line = $telnet->getline() )
{
# get configuration content
if( $line !~
/get config/ )
{
push @config, $line;
}
}
$telnet->waitfor(Match => '/[\$#>]/');
$telnet->print( "set console page 60" );
if( $telnet->errmsg )
{
$msg = "No prompt after 'get config': ".$telnet->errmsg;
$telnet->close( );
return( 244, $msg );
}
if( @config <= 0 )
{
$msg = "No data retrieved, the capture timeout may be too low.";
$telnet->close();
return( 244, $msg );
}
if( scalar grep {$_ =~ /^%/} @_ )
{
# Ensure get config actually returned the config and not an error message containing '%'
return( 253, @config);
}
return( 0, @config); # everything was okay, return the captured data
}