OK, so I wrote a telnet version and that works, but now we are migrating to SSh and I have (attempted) to modify it to figure out which to use, and be able to use SSH. So far, it works with telnet capable devices, buy still tries to SSH to them as well, and doesn't work with SSH devices. So, technically, it does nothing more than the original telnet script... Hmm.... Anyway, am I missing something small or am I completely off base and need to RTFM?
#!/usr/bin/perl -w ######################################################## # This script will tftp copies of the running config # # of a predetermined list of devices to a tftp server. # # The script will accept as input a text file which is # # a list of device IP addresses and produce a log file # # to list which devices were successfully backed-up. # # -- Written by Russell Gibbons # ######################################################## use Net::Telnet::Cisco; use Net::SSH::Perl; $ENV{'HOME'} = 'c:\strawberry'; $in_file = $ARGV[0]; # This is the filename of the input file with th +e IP addresses open (LOG, ">backup-log.txt") or die " cannot open log file!"; # log f +ile open (LIST, "<$in_file") or die " cannot open $in_file: $!"; # list +of IP addresses while (<LIST>) { chomp; push @ips, $_; # Populate array of IP addresses } close (LIST) or die " cannot close $in_file: $!"; $user = "********"; $pw = "********"; # Most special characters will require a pr +eceeding '\' $tftp_server = 'XX.XX.XX.XX'; # IP address of TFTP Server for(@ips) { $ssh = TRUE; eval { $cs = Net::Telnet::Cisco->new( Host =>$_, Timeout => 45); if ($cs->login( $user, $pw)) # Login { print "Opening Telnet session to $_\n"; $ssh = FALSE; # If telnet worked, no need to ss +h @hostline = $cs->cmd('sh run | inc hostname'); @hostline = split(/ /, $hostline[0]); chomp $hostline[1]; $hostname = $hostline[1]; print $hostname . "\n"; $cs->cmd("copy system:/running-config tftp://$tftp_server/$h +ostname-confg.txt\n\n\n"); print "sent CRT\n"; $cs->close; # Log out of device print LOG $hostname . ' - ' . $_ . ".\n"; } }; # End trying telnet if ($ssh) { eval { $cs = Net::SSH::Perl->new($_); if ($cs->login( $user, $pw)) # login { print "Opening SSH session to $_\n"; ### Seems to fail here, + or on the next line my(@hostline, $stderr, $exit) = $cs->cmd('sh run | inc hostname' +); @hostline = split(/ /, $hostline[0]); chomp $hostline[1]; $hostname = $hostline[1]; print $hostname . "\n"; $cs->cmd("copy system:/running-config tftp://$tftp_server/$h +ostname.txt\n\n\n"); print "sent CRT\n"; $cs->close; # Log out of device print LOG $hostname . ' - ' . $_ . ".\n"; } }; # End trying ssh } } close (LOG);

In reply to accessing Cisco via SSH by Always Improving

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.