Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

comment on

( [id://3333] : superdoc . print w/replies, xml ) Need Help??

I am working on a script that will run on Redhat 6.5 with Perl 5.10.1 and an Oracle client. I need to connect to a few dozen Oracle databases using 10g, 11g or 12c. Unfortunately, there are two possible passwords that could get used, and I don't have a list of which password to try. Hence, I want to try one password and if that does not work try the other. Otherwise, I will just add it to an array that will print out at the end and I can look at those databases and possibly change the password. I am using the system to connect to every database.

How should I do the error handling to deal with not being able to connect to a database? Also when I get an error and try to exit, the script doesn't exit, but restarts from the beginning. How can I change that behavior?

Thanks in advance!
sub connect_to_db { my $tns_name = shift; my $logger = shift; my $run_as_user = shift; my $system_password = shift; my $system_password2 = shift; my $dbh; my $correct_password; # try connecting with one password then the other, if both passwords +fail mark it as failed try { $dbh = DBI->connect( "DBI:Oracle:".$tns_name, $run_as_user, $sys +tem_password, { PrintError => 1, RaiseError => 1, AutoCommit => 1 } ); $correct_password = $system_password; } catch { print "wrong password, try another\n"; }; if ( undef $dbh ) { try { $logger->info( 'password two |'.$system_password2.'|'); $dbh = DBI->connect("DBI:Oracle:".$tns_name, $run_as_user, $ +system_password2, { PrintError => 1, RaiseError => 1, AutoCommit => 1 } ); $correct_password = $system_password2; } catch { print "wrong password, add it to the connection issues list\n +"; }; } if ( undef $dbh ) { push @db_connect_issues, "error:".$tns_name." -- ".$DBI::errstr; } else { $users_and_passwords{ 'INSTALLER'}{$run_as_user } = $correct_pas +sword; print ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> The right password is + $correct_password <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n"; } return $dbh; }

In reply to error handling with dbi by gandolf989

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

  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.