I am attempting to cycle through my db table list of 5 servers and run the same command for each server to collect and print all the results to the browser through a php script. This is my first work with perl! Here is my code:
#!/usr/local/bin/perl
use warnings;
use Net::Telnet();
# MySQL database configuration //BELOW DB CONNECTION TEST IS WORKING A
+ND CONNECTED. JB 20220930 15:25
use DBI;
#----------------------------------------------------------------
# Execute a command on a Zhone device
#----------------------------------------------------------------
sub zhoneCommand($$) {
my ($command, @output, $session);
($session, $command)=@_;
@output=$session->cmd(String => "$command", Errmode => 'return');
chomp @output;
return(@output);
}
#----------------------------------------------------------------
# Connect to a Zhone device via telnet session
#----------------------------------------------------------------
sub zhoneConnect($) {
my ($device , $session);
($device)=@_;
$session = new Net::Telnet(Host=> "$device", Prompt => '/>\s*/', Tim
+eout => 10, Errmode => 'return', Output_record_separator => "\n\r");
$session->input_log("log_$device") if ($session && ($DEBUG));
return($session);
}
#----------------------------------------------------------------
# Disconnect from Zhone device
#----------------------------------------------------------------
sub zhoneDisconnect($) {
my ($session);
($session)=@_;
if ($session) {
$session->cmd("logout");
$session->close();
}
}
#----------------------------------------------------------------
# Login to a Zhone device
#----------------------------------------------------------------
sub zhoneLogin($$$) {
my ($password, $session, $user);
($session, $user, $password)=@_;
return($session->login("$user","$password"));
}
#----------------------------------------------------------------
# Globals
#----------------------------------------------------------------
$DEBUG=1;
#----------------------------------------------------------------
# Main Program
#----------------------------------------------------------------
##################### PERLMONKS QUESTION STARTING HERE ###############
+###########
$dsn = "DBI:mysql:bfcma:localhost";
$username = "root";
$password = 'password';
$dbc = DBI->connect($dsn, $username, $password) or die "Unable to conn
+ect to mysql: $DBI::errstr\n";
#//loop through the servers table to pull data for each server
$sql = $dbc->prepare("SELECT server_host FROM servers");
$result = $sql->execute();
or die "Unable to execute sql: $sql->errstr";
while (my @row = $sql->fetchrow_array())
{
#print "@row\n";
}
foreach $host (@row) {
$device= $host;
$user="username";
$password="password";
STDOUT->autoflush;
die "Unable to telnet to device\n" if (! ($session=zhoneConnect($devic
+e)));
die "unable to login to device\n" if (! zhoneLogin($session, $user, $p
+assword));
zhoneCommand($session, "timeout off");
zhoneCommand($session, "setline 0");
$session->prompt('/\n\rAUTO>\s*/');
zhoneCommand($session, "setprompt session AUTO>\r");
@output=zhoneCommand($session, "bridge show");
for (@output) {
print "$_\n";
}
zhoneDisconnect($session);
#close while loop
}