Hacking shell scripts


Using perl, DBI::MySQL

#!/usr/bin/perl 

use DBI;
use strict;

my $dbh = DBI->connect( "dbi:mysql:perlsnmp", "root", "password") or
die("Failed to connect to Database");

my $sth = $dbh->prepare('SELECT * FROM deviceinfo WHERE sysname = ?')
   or die "Cant executer SQL statement: $DBI::errstr\n";

system('clear');

print "CTRL -C to exit\n";
print "Enter Device Host Name > ";
 while( my $sysname = <>) {

  my @data;
  chomp $sysname;
  
  $sth->execute($sysname) or die "Can't execute SQL statement:
$DBI::errstr\n";
  print "ifDescr\tifInOctets\tifOutOctets\n";
  my @row;
    while (@row = $sth->fetchrow_array( )) {
     print " @row[6]\t$row[7]\t$row[8]\n";
   }
  
  if($sth->rows == 0) {
    print "No Match for `$sysname`.\n\n";
  }

  print "\n";
  print "Enter Device Host Name > ";
}

$sth->finish;
$dbh->disconnect or warn "Error disconnecting: $DBI::errstr\n";

PCCS-Linux.COM ::ource Advocate Articles catalogue
2000 2002

Document Created by:   Chauncey Thorn @ http://PCCS-Linux.COM
EMAIL: cthorn