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 }

In reply to For each loop through mysql db to run Net::Telnet command by jay83

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.