473,698 Members | 2,411 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

a way to go to next valid entry in an infile

3 New Member
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;
Oct 3 '05 #1
2 4294
octoberdan
18 New Member
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
Jason3231
3 New Member
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;
Oct 12 '05 #3

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

Similar topics

14
9743
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 . "\" into table addresses"; $result = mysql_query($SQL) or die(mysql_error()); The file is located in the same directory as my .PHP file. How do I generate a relative address for this file so that it can be found?
3
3051
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 wanted to build in NEXT and PREVIOUS buttons, so that you only get 5 or 10 or 20 (I haven't made up my mind yet) thumbnails at a time. I use a SQL LIMIT statement to achieve that. After displaying the first 5 pictures I would like to see the next 5,...
7
12086
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"); exit(1); }
0
6684
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 be able to place the PHP file on the server, and I guess you probably can't do that either. Talk about catch 22... The only other way I can think of is to install MySQL on a machine you control, then import the data there using the method I...
0
1648
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 programs that read the group from option files, you can add the local-infile=1 option to that group. However, to keep this from causing problems for programs that do not understand local-infile, specify it using the
1
16072
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 is for the sake of security. What does that mean ?
0
1522
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 $ I get the first book and its two keywords and the $ but when i exit the inner loop and go to the outer loop i am needing to bring in "Introductory Organic Chemistry", But the inner loop doesn't get it to the next line so I read in ""...
15
2996
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 Tubbs
4
3568
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, and the pagination almost works. The first page of the pagination works fine, but when I click on one of the links for one of the next pages, the page is blank. I have seen people mention this problem, and they have been told that a variable is...
0
8604
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9160
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8862
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7729
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5860
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4370
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4619
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2331
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2002
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.