CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Advanced: Re: [KevinR] Help with parse words please.: Edit Log



cuboidgraphix
User

Feb 22, 2009, 6:43 AM


Views: 4700
Re: [KevinR] Help with parse words please.

Here's my final code.


Code
#!/usr/bin/perl 
# This is a script that will telnet into the MTX and gather the BRSTAT OM(s).

# Declare 'use' statements
use DBI;
use strict;
use warnings;
use DBD::mysql;
use Net::Telnet;
use Text::ParseWords;
use POSIX qw/strftime/;

# Establish the MySQL connection
my($host,$database,$usr,$pwd,$dsn,$dbh,$sth,$query);
$host = 'localhost';
$database = 'db';
$usr = 'USR';
$pwd = 'PWD';

# Data Source Name
$dsn = "dbi:mysql:$database:localhost:3306";

# Perl DBI connect
$dbh = DBI->connect($dsn, $usr, $pwd);

# Establish the Telnet Connection
my($user,$pass,$server,$t,$data);
$user="USER";
$pass="PASS";
$server="HOST";
$t = Net::Telnet->new($server);
$t->waitfor('/Enter User Name/');
$t->print("$user");
$t->waitfor('/Enter Password/');
$t->print("$pass");
$t->waitfor('/>/');
$t->print("logutil;open act 102");
$t->waitfor('/Done./');

# Collecting data output
($data) = $t->waitfor('/>/');

$t->print("logout");

# Stripping the data output of lines, ms and %.
$data =~ s/(\n)|(ms)|(%)//g;

# Short script to print the output in a text file for test.pl
# my($FH,$FILE);
# $FILE = "sample.txt";
# open $FH, '>', $FILE or die "Can't open '$FILE' $!";
# print $FH $data;
# close $FH;

if($data =~ /^SPEEDNET/){

# Parsing the Line
my ($TIME,$Catmp,$Lorig,$Torig,$ToAnn,$Cpocc,$Conctr,$OAvgDel,$O95,$PAvgDel,$P95,$BAvgDel,$B95,$RTrip, $Origdeny,$Inefdeny,$CPloovfl,$CPsuic,$CPtrap,$LCMdtsr,$LMdtsr) = (quotewords('[\s]+', 0, $data))[3,9,11,13,15,17,19,21,23,25,27,29,31,33,35,37,39,41,43,45,47];

# Date variable
my $DATE = strftime("%Y-%m-%d", localtime(time));
# Prepare and execute query to insert into the database
$query = "INSERT INTO ACT_102 VALUES('$DATE','$TIME','$Catmp','$Lorig','$Torig','$ToAnn','$Cpocc','$Conctr','$OAvgDel','$O95','$PAvgDel', '$P95','$BAvgDel','$B95','$RTrip','$Origdeny','$Inefdeny','$CPloovfl', '$CPsuic','$CPtrap','$LCMdtsr','$LMdtsr')";
$sth = $dbh->prepare($query);
$sth->execute();

$sth->finish();
$dbh->disconnect();
}


Don't know what rating my script has... but I tried to use best perl practices. :)


(This post was edited by cuboidgraphix on Feb 22, 2009, 6:46 AM)


Edit Log:
Post edited by cuboidgraphix (User) on Feb 22, 2009, 6:46 AM


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives