473,324 Members | 2,179 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

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::Cisco;

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::Cisco->new(Host => $line, timeout => "$timeout1");
###ENTER USERNAME AND PASSWORD INFO BELOW

$session1->login('USERNAME', 'PASSWORD');

###ENTER ENABLE PASSWORD AND CISCO CMD BELOW

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;

}

close INFILE;
close OUTFILE;
Oct 3 '05 #1
2 4286
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... :(

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2.  
  3. use Net::Telnet::Cisco;
  4.  
  5. my $session1;
  6.  
  7. my $timeout1 = 30;
  8. my $start = localtime;
  9.  
  10. open my $outfile,">", "allrslts.doc" or die "Error: $!";
  11. open my $infile, "<", "searchall.txt" or die "Can't open searchall.txt: $!";
  12.  
  13. print $outfile "TIME OF REPORT $start\n\n";
  14.  
  15. while (<$infile>) {
  16.     print;
  17.     $line = <$infile>;
  18.  
  19.     print "IP ADDRESS $line\n";
  20.     print OUTFILE " IP ADDRESS $line\n";
  21.  
  22.     $session1 = Net::Telnet::Cisco->new(Host => $line, timeout => $timeout1);
  23.  
  24.     $session1->login('USERNAME', 'PASSWORD');
  25.  
  26.     if ($session1->enable('PASSWORD') ) {
  27.         my @output = $session1->cmd('CISCO_CMD');
  28.  
  29.         print $outfile "@output \n\n";
  30.         print @output;
  31.     } else {
  32.         warn "Can't enable: " . $session1->errmsg;
  33.     }
  34.  
  35.     $session1->close;
  36. }
  37.  
  38. #you can probably do $foo->close on these too.
  39. close $infile;
  40. close $outfile;
  41.  
Are you skipping a line every iteration on purpose? if not replace
Expand|Select|Wrap|Line Numbers
  1. while (<$infile>) { #reads a line from the file
  2.     print; #the asumed scalar is asumed ;-)
  3.     $line = <$infile>; #reads a line from the file
  4.  
with

Expand|Select|Wrap|Line Numbers
  1. while (<$infile>) {
  2.     $line = $_;
  3.  
  4.  
Try to run my code, troubleshoot, and then post back with any errors. If you have any questions, please ask!
Oct 5 '05 #2
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::Cisco;



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::Cisco->new(Host=> $line, timeout=> "$timeout1");');warn $@ if $@;

###########CHANGE USERNAME AND BOTH INSTANCES OF PASSWORD

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

if ($session1->enable('PASSWORD') ) {

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;
Oct 12 '05 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

14
by: Bruce A. Julseth | last post by:
When I execute this SQL statement in my PHP code, I get an error "File '.\Address.txt' not found (Errcode: 2)" $File = addslashes(".\Address.txt"); $SQL = "Load Data InFile \"" . $File . "\"...
3
by: Marcel | last post by:
Hello, I'm working on a search application for my website. The website contains a lot of pictures, and a search should return clickable thumbnails. No problems there. My problem started when I...
7
by: las | last post by:
I'm having a wee problem with the get method, here is my code : ifstream infile; char x; infile.open("temp.txt"); if( !infile.good() ) { cout << "Error opening file" << endl; system("PAUSE");...
0
by: Donald Tyler | last post by:
Then the only way you can do it that I can think of is to write a PHP script to do basically what PHPMyAdmin is trying to do but without the LOCAL in there. However to do that you would need to...
0
by: Steffen | last post by:
Hello, I´m using mysql 3.23.58 and I want to enable the local-infile option. The manual on dev.mysql.com says the following to that: ..... If you use LOAD DATA LOCAL in Perl scripts or other...
1
by: Ray in HK | last post by:
What are the differences between LOAD DATA INFILE and LOAD DATA LOCAL INFILE ? I found some web hosting company do not allow using LOAD DATA INFILE but allow LOAD DATA LOCAL INFILE. The reason...
0
by: jjh | last post by:
So my code is below. I have a problem... I am trying to bring in a text file that is like this: Principles of Biochemistry Biology Chemistry $ Introductory Organic Chemistry Chemistry $ ...
15
by: waltbrad | last post by:
Hello. I'm studying the book "C++ Primer Plus" by Stephan Prata. In chapter 6 he gives an exercise that reads from a file. The list is thus: 4 Sam Stone 2000 Freida Flass 100500 Tammy...
4
by: ArizonaJohn | last post by:
Hello, The code below works great. The user enters a name into an HTML form, the code looks up a table with that name, and then that table is displayed. I am trying to use pagination with it,...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.