#!/usr/bin/perl ## $URL: https://svn/repos/internetwork_ncm/trunk/HP_ProCurve/hp_procurve_capture_config.pl $ ## $Id: hp_procurve_capture_config.pl 5 2011-03-30 21:05:05Z dopheideb $ use strict; use warnings; ## We include Data::Dumper if we need debugging output ## use Data::Dumper; ## Within Spectrum NCM configuration, set the following parameters: ## Name: scp_file ## Value: cfg/startup-config or cfg/running-config use Net::SSH2; use Net::SSH2::SFTP; use Time::HiRes 'usleep'; my $host = $ARGV[0] || 'dummy_host'; my $username = $ARGV[1] || 'dummy_username'; my $password = $ARGV[2] || 'dummy_password'; my $pw_enale = $ARGV[3] || 'dummy_pw_enable'; my $scp_file = $ARGV[4] || 'dummy_scp_file'; ## NCM gives us the password via the command line. Silly and insecure. ## Best we can do, is hide it. $0 = "$0 $host $username ****** ###### $scp_file"; ## It seems that Spectrum and this script don't always go hand in hand. ## We are not sure where the problem is, so we will have to try obvious ## quickfixes and workarounds. ## ## Workaround 1: if we somehow hang, commit suicide. { $SIG{'ALRM'} = sub { die "Suicide timer reached. Goodbye cruel world."; }; alarm (30); ## 30 seconds should be more than enough. } ## Workaround 2: Behave daemonlike. { chdir ('/'); open (STDIN, '/dev/null'); } my $ssh2 = Net::SSH2->new (); $ssh2->connect ($host) or die $!; unless ($ssh2->auth_password ($username, $password)) { die "Connecting to $username\@$host failed. Password wrong perhaps?"; } ## There seems to be some timing issues on procurve. Hence, insert some ## sleepiness. $ssh2->blocking (1); usleep (100); my $sftp = $ssh2->sftp () or die "Can not open subsystem SFTP: $!"; usleep (100); my $fh = $sftp->open ($scp_file) or die "Opening $scp_file via sftp failed: $!"; usleep (100); while (1) { my $buf; $fh->read ($buf, 1024); last unless $buf; print $buf; usleep (100); } __END__