Below you will find a sample script using ssh communication. Hope this will help.
I wrote it long back for Nortel device using ssh and non default port. 172.19.1.12 was IP of SpectroServer.
#!/opt/SPECTRUM/bin/perl -w
#
# Author:
Saurabh Bohra
# Version:
1.0.0
# Date:
April 16, 2009
# Purpose:
This script will upload the running configuration of a
#
Nortel 4550T ERS devices using SSH on a non default port 8591 and print it to STDOUT.
#
# Command Sequence for Nortel 4550T ERS to upload configuration
#
Logon name - Password - Port
#
Ctrl + Y
#
enable
#
execute the commands to upload the diag and image file
#
exit
#
# Usage:
perl nortel4550T-ssh-firmware.pl 172.16.2.66 rw '$tuP1D#123' enpass 15 50 diagFileName imageFileName
#
# Known Issues:
#
1. Testing of commands sents to execute are commented out.
#
After sucessfully logon on a Nortel 4550T ERS (HW:03 FW:5.0.1.0 SW:v5.0.1.001) and
#
executing Ctrl+Y through perl script if two commands say cmd1 and cmd2 are run and output
#
is requested after running each command the result will be as follows:
#
Ctrl+Y
#
cmd1 -> outout of Ctrl+Y
#
cmd2 -> output of cmd1
#
Due to this behavior of Nortel 4550T ERS the testing of the output of the commands
#
sent are commented out. However the scripts works fine if the testing output of the commands
#
is not commented and the switch is Baystack 470-24T (HW:08 FW:3.0.0.5 SW:3.5.0.59)
#
# Help:
#
http://search.cpan.org/~bnegrao/Net-SSH-Expect-1.09/lib/Net/SSH/Expect.pod
#
http://search.cpan.org/~rgiersig/Expect-1.21/Expect.pod
#
http://perldoc.perl.org/perlretut.html
#
# Error Codes:
# 0 = Success
# 255 = Usage error
# 254 = Invalid timeout value
# 252 = Connection error
# 251 = Login error
# 249 = Enable error
# 253 = Bad upload file
# 247 = Configure Terminal error
# 246 = Upload error
# 200 = Test result
use strict;
use warnings;
use Net::SSH::Expect;
$ENV{'PATH'} = "/usr/bin:". $ENV{'PATH'};
#print ("\nHello World\n");
### Main ###
if( $#ARGV != 6 && $#ARGV != 7 )
{
print "Usage: nortelssh-firmware-v100.pl <device IP> <user> <pass> <enable_pass> <login_timeout_in_seconds> <upload_timeout_in_seconds> <diagFile> <imageFile>\n";
print STDERR "Usage: nortelssh-firmware-v100.pl <deviceIP> <user> <pass> <enable_pass> <login_timeout_in_seconds> <upload_timeout_in_seconds> <diagFile> <imageFile>\n";
exit 255;
}
elsif( $ARGV[4] < 1 || $ARGV[4] > 60 )
{
print "$ARGV[6] is the login timeout and must be an int between 1 and 60 seconds\n";
print STDERR "$ARGV[6] is the login timeout and must be an int between 1 and 60 seconds\n";
exit 254;
}
elsif( $#ARGV == 7 && ( $ARGV[5] < 1 || $ARGV[5] > 100 ) )
{
print "$ARGV[7] is the upload timeout and must be an int between 1 and 100 seconds\n";
print STDERR "$ARGV[7] is the upload timeout and must be an int between 1 and 100 seconds\n";
exit 254;
}
else
{
my $upload_timeout = $ARGV[4];
if( $ARGV[5] )
{
$upload_timeout = $ARGV[5];
}
my $prt = 8591; # Define the port on which SSH is listening
my $errorMsg;
my $errorCode = 0;
# Pass the following parameters for the device - IPAddress, UserName, Password, LoginTimeout, UploadTimeout, SSHPort, diagFileName and imageFileName
# For Nortel 470-24T, 470-48T and 4550 enable password is not required.
($errorCode, $errorMsg) = UploadConfig( $ARGV[0], $ARGV[1], $ARGV[2],
$ARGV[4], $upload_timeout, $prt,
$ARGV[6], $ARGV[7] );
if( $errorCode != 0 )
{
print STDERR $errorMsg;
exit $errorCode;
}
}
exit 0;
### Functions ###
sub UploadConfig
{
my $deviceIP = shift;
my $user = shift;
my $pass = shift;
# my $epass = shift;
my $login_timeout = shift;
my $upload_timeout = shift;
my $port = shift;
my $diagFile = shift;
my $imageFile = shift;
my $msg = "Failure";
my $ssh = new Net::SSH::Expect( host => $deviceIP,
user => $user,
password=> $pass,
port => $port,
raw_pty => 1,
no_terminal => 1,
timeout => $login_timeout
);
# read ressource data
# open (DFH, $diagFile) or return( 253, "Diag file - $diagFile does not exist at the specified location\n" );
#
open (IFH, $imageFile) or return( 253, "Image file - $imageFile does not exist at the specified location\n" );
my $login_output = $ssh->login();
if ($login_output !~ /Enter Ctrl-Y to begin/)
{
$msg = "Login has failed. Login output was $login_output\n";
return (252, $msg);
}
#print ("Login was Successful\n");
#return (200, $login_output);
# After logging in execute Ctrl+Y command
my $cty = $ssh->send("\cY");
print ("Ctrl+Y sent\n");
#return (200, $cty);
# Execute enable command
my $enable = $ssh->exec("enable");
$ssh->exec("\r");
$ssh->exec("\r");
$ssh->exec("\r");
print ("enable sent\n");
#return (200, $enable);
#
if ($enable !~ /\#\s*\z/ )
#
{
#
$msg = "Device did not except 'enable' command\n";
#
$ssh->close(); # Close the connection before returning
#
return (249, $msg);
#
}
# Set upload time out
$ssh->timeout( $upload_timeout );
$ssh->peek(0);
my $diagCmd = "download address 172.19.1.12 diag $diagFile no-reset\r";
print ("\nSending command -> $diagCmd\n");
my $diagExec = $ssh->exec("$diagCmd\r");
print ("download diag command sent\n");
$ssh->exec("\r");
#
return (200, $diagExec);
#
if ( $diagExec !~ /Finished Upgrading Diag Image\s*\z/ )
#
{
#
$msg = "Uploading the diag file on the device $deviceIP failed\n";
#
$ssh->close(); # Close the connection before returning
#
return (246, $msg);
#
}
my $imageCmd = "download address 172.19.1.12 image $imageFile";
print ("\nSending command -> $imageCmd\n");
my $imageExec = $ssh->exec("$imageCmd\r");
print ("download image command sent\n");
$ssh->exec("\r");
#
return (200, $imageExec)
#
if ( $imageExec !~ /Finished Upgrading Image|Rebooting\s*\z/ )
#
{
#
$msg = "Uploading the image file on the device $deviceIP failed\n";
#
$ssh->close(); # Close the connection before returning
#
return (246, $msg);
#
}
$msg = "Success";
$ssh->close();
return( 0, $msg );
}