a way to go to next valid entry in an infile

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Jason3231
    New Member
    • Oct 2005
    • 3

    a way to go to next valid entry in an infile

    well i've found and pieced together a script (mainly found; i'm very new to perl) to scan a group of cisco switches that i have and print information out in a text file. everything seems to work pretty well except when a particular cmd doesn't apply to one of the switches listed in my infile. the script seems to get stuck in these instances and closes without going to the next valid switch. maybe i need some sort of ELSE type of statement or something?? here's the basic script that i've been using(searchall .txt is just a list of IP addresses):

    use Net::Telnet::Ci sco;

    open(OUTFILE, ">allrslts.doc" );
    $start = localtime;
    print OUTFILE "TIME OF REPORT $start\n\n";

    open(INFILE, "searchall.txt" ) or die "Can't open searchall.txt: $!";

    while (<INFILE>) { # assigns each line in turn to $_
    print "$_";

    $line = <INFILE>;
    print "IP ADDRESS $line\n";
    print OUTFILE " IP ADDRESS $line\n";
    $timeout1 = ("30");
    $session1 = Net::Telnet::Ci sco->new(Host => $line, timeout => "$timeout1" );
    ###ENTER USERNAME AND PASSWORD INFO BELOW

    $session1->login('USERNAM E', 'PASSWORD');

    ###ENTER ENABLE PASSWORD AND CISCO CMD BELOW

    if ($session1->enable('PASSWO RD') ) {
    my @output = $session1->cmd('CISCO_CMD ');

    print OUTFILE "@output \n\n";
    print @output;

    } else {
    warn "Can't enable: " . $session1->errmsg;

    }

    $session1->close;

    }

    close INFILE;
    close OUTFILE;
    Last edited by Jason3231; Oct 12 '05, 02:24 PM.
  • octoberdan
    New Member
    • Sep 2005
    • 18

    #2
    I did some major clean up... I just quickly went through though...I don't know if it works, I just hacked at it with a broom stick... It should be easier to work with now atleast... I'll come back in a bit when I'm not so busy and take another look. This cisco stuff is bring back bad memorys... :(

    Code:
    #!/usr/bin/perl
    
    use Net::Telnet::Cisco;
    
    my $session1;
    
    my $timeout1 = 30;
    my $start = localtime;
    
    open my $outfile,">", "allrslts.doc" or die "Error: $!";
    open my $infile, "<", "searchall.txt" or die "Can't open searchall.txt: $!";
    
    print $outfile "TIME OF REPORT $start\n\n";
    
    while (<$infile>) {
        print;
        $line = <$infile>;
        
        print "IP ADDRESS $line\n";
        print OUTFILE " IP ADDRESS $line\n";
        
        $session1 = Net::Telnet::Cisco->new(Host => $line, timeout => $timeout1);
    
        $session1->login('USERNAME', 'PASSWORD');
    
        if ($session1->enable('PASSWORD') ) {
            my @output = $session1->cmd('CISCO_CMD');
    
            print $outfile "@output \n\n";
            print @output;
        } else {
            warn "Can't enable: " . $session1->errmsg;
        }
    
        $session1->close;
    }
    
    #you can probably do $foo->close on these too.
    close $infile;
    close $outfile;
    Are you skipping a line every iteration on purpose? if not replace
    Code:
    while (<$infile>) { #reads a line from the file
        print; #the asumed scalar is asumed ;-)
        $line = <$infile>; #reads a line from the file
    with

    Code:
    while (<$infile>) {
        $line = $_;
    Try to run my code, troubleshoot, and then post back with any errors. If you have any questions, please ask!
    Last edited by octoberdan; Oct 5 '05, 08:41 PM.

    Comment

    • Jason3231
      New Member
      • Oct 2005
      • 3

      #3
      This works

      after a little help and a bit of trial and error i've come up with something that:
      1. prompts user for cmd to run on cisco devices
      2. logs in to each device that an IP is given
      3. goes to enable mode
      4. runs cmd on ea. device
      5. prints an out file with data searched for

      not the prettiest/neatest script, but it works great:



      #!perl -w
      use Net::Telnet::Ci sco;



      open(OUTFILE, ">allrslts.doc" );
      $start = localtime;
      print OUTFILE "TIME OF REPORT $start\n\n";

      open(INFILE, "searchall.txt" ) or die "Can't open searchall: $!";
      print "Enter the command to run on all devices: ";
      $cmd = <STDIN>;
      while (<INFILE>) { # assigns each line in turn to $_
      print "$_";

      $line = <INFILE>;
      print "IP ADDRESS $line\n";
      print OUTFILE " IP ADDRESS $line\n";
      $timeout1 = ("90");
      eval('$session1 = Net::Telnet::Ci sco->new(Host=> $line, timeout=> "$timeout1");') ;warn $@ if $@;

      ###########CHAN GE USERNAME AND BOTH INSTANCES OF PASSWORD

      $session1->login('USERNAM E', 'PASSWORD') or die "Unable to login: $!";

      if ($session1->enable('PASSWO RD') ) {

      eval('my @output = $session1->cmd("$cmd");
      print OUTFILE "@output \n\n";
      print @output;

      ');warn $@ if $@;


      } else {
      warn "Can't enable: " . $session1->errmsg;

      }

      $session1->close;

      }

      close INFILE;
      close OUTFILE;

      Comment

      Working...