Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re: For each loop through mysql db to run Net::Telnet command

by Marshall (Canon)
on Oct 22, 2022 at 08:20 UTC ( [id://11147593]=note: print w/replies, xml ) Need Help??


in reply to For each loop through mysql db to run Net::Telnet command

Echoing previous post, you should "use strict;"

Do not use function prototypes. This turns out to be a bad idea although it looks familiar from other languages. There is reason for them, but this is not it.

Once you have done that, I could move the main loop part of the code to the front and put the subs at the end.
I recoded one sub for you.

#---------------------------------------------------------------- # Execute a command on a Zhone device - recoded***** #---------------------------------------------------------------- sub zhoneCommand { my ($command,$session) = @_; #<- look at this line for I/F my @output=$session->cmd(String => "$command", Errmode => 'return'); chomp @output; return(@output); } #-------------- I would use fetchall_arrayref for a small, simple result set like this +. You get a reference to an array of server_host's. Then just iterate over that array. #--------------------- # loop through the servers table to pull data for each server my $sql = $dbc->prepare("SELECT server_host FROM servers"); my $result = $sql->execute(); or die "Unable to execute sql: $sql->errstr"; my $host_array_ref = $result->fetchall_arrayref() or die "can't get a +rray ref! $sql->errstr"; foreach my $rowref (@$host_array_ref) { my ($host) = @$rowref; print "Calling telenet stuff for $host\n"; #a little debugging o +utput is fine # call your telenet routine on this host }

Replies are listed 'Best First'.
Re^2: For each loop through mysql db to run Net::Telnet command
by jay83 (Novice) on Nov 08, 2022 at 18:02 UTC

    Thank you - I made edits to the main loop but the error in the terminal is : Can't locate object method "fetchall_arrayref" via package "2" (perhaps you forgot to load "2"?) at filepath line XXXXXXXXXX

    my $dbc = DBI->connect($dsn, $username, $password) or die "Unable to c +onnect to mysql: $DBI::errstr\n"; #//loop through the servers table to pull data for each server my $sql = $dbc->prepare("SELECT server_host FROM servers"); my $result = $sql->execute() or die "Unable to execute sql: $sql->errstr"; XXXX ERROR LINE XXXXX my $host_array_ref = $result->fetchall_arrayref( +) or die "can't get array ref! $sql->errstr"; XXXXXXXXXX foreach my $rowref (@$host_array_ref) { my ($host) = @$rowref; print "Calling telenet stuff for $host\n"; #a little debugging ou +tput is fine # call your telnet routine on this host my $device = $host; my $user = "admin"; my $password = "zhone"; STDOUT->autoflush; die "Unable to telnet to device\n" if (! (my $session=zhoneConnect($de +vice))); 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"); my @output=zhoneCommand($session, "bridge show"); for (@output) { print "$_\n"; } zhoneDisconnect($session); }

      Hi,

      You are trying to call fetchall_arrayref on the return value of your execute() call. In fact it is a method on the statement handle, which in your code is $sql (more idiomatically named as $sth).

      Hope this helps!


      The way forward always starts with a minimal test.
        This helped thank you!
      Yeah, there are some cut-n-paste errors above. Unfortunately you didn't provide a complete example for me to test my code with.

      First suggestion is to get rid of the "or die" statements, by specifying the raise error option during the dB connect. A little short of time this morning, but the below is some actual working code from a few days ago on an SQLite DB. I use $dbh for the data base handle. You can follow this pattern.

      my %attr = ( RaiseError => 1); #auto die with error printout my $dbh = DBI->connect("dbi:SQLite:dbname=$dbfilename","","",\%attr) or die "Couldn't connect to database $dbfilename: " . DBI->errstr; my $get_qsos = $dbh->prepare ("SELECT * FROM raw_qsos WHERE rcv_call = ?"); $get_qsos->execute($call); my $resultRef = $get_qsos->fetchall_arrayref; my $n_dbQso = scalar(@$resultRef); #number of rows with that call
        No problem Marshall - thank you for the follow up and help!!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11147593]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (5)
As of 2024-04-19 03:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found