Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I write a data file so my script can take information from it?

Status
Not open for further replies.

samdaman

Technical User
Oct 23, 2002
23
NZ
Hi everyone.

I am trying to retrieve some information from a text file, but am not sure if the format is ok. Could someone please tell me how its supposed to be? Currently its like this

Host:

172.24.100.111

Dialer:

1

Eventually this data file will have multiple hosts, with dialers linked to its respective host. I have the script below.

Thanks for your help

Kind regards

Sam

#!E:\Perl\bin

use strict;
use warnings;
use Net::Telnet::Cisco;
use Getopt::Long;

# Globals. Change these!
our $LOGIN = 'LOGIN';
our $PASS = 'password';
our $ENPASS = 'password';

# Parse command-line options
my ($host, $dialer);
GetOptions( "host=s" => \$host, "dialer=s" => \$dialer );

unless ( defined $host && defined $dialer ) {
warn "You forgot to pass me some arguments!\n";
print usage();
exit 1;
}

# Read from logfile
open LOGFILE, "> log.txt" or die "Can't read from log.txt: $!";
my $title = <LOGFILE>;
print &quot;Report Title: $title&quot;, <LOGFILE>;
close LOGFILE or warn $!;

# Router automation
my $session = Net::Telnet::Cisco->new( Host => $host );
$session->login( $LOGIN, $PASS );

if ( $session->enable( $ENPASS ) ) {
print &quot;My privileges: &quot; . $session->cmd( 'show privilege' );
$session->cmd( 'configure terminal' );
$session->cmd( &quot;interface Dialer $dialer&quot; );
$session->cmd( 'No Shutdown' );
} else {
warn &quot;Can't enable: &quot; . $session->errmsg;
}

exit;

sub usage { &quot;$0 --host=hostname --dialer=number&quot; }

__END__
 
Personally I don't use flatfiles for anything, even small counter scripts. I'd suggest you actually use a database, they are easier to post to and retrieve information from. Codes are easier to follow and it requires less typing.

I'd personally rewrite the snippet as:
open (LOGFILE, &quot;> log.txt&quot;) or die &quot;Can't read from log.txt: $!&quot;;
my $title = <LOGFILE>;
print LOGFILE &quot;Report Title: $title&quot;;
close (LOGFILE) or warn($!); &quot;Age is nothing more than an inaccurate number bestowed upon each of us at birth as just another means for others to judge and classify us- sulfericacid
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top