sub mask_pwd {
 my $str = shift;
 $str =~ s/conn(ect)?\s+(.*?)\/(.*?)([@\s])/connect $2\/(hidden)$4/ig;
 $str =~ s/identified\s+by(\s+values)?\s+(.+?)(\s)/identified by$1 (hidden) $3/ig;
 return $str;
}
sub run_sql {
 my $sql = shift;
 my $error_handling = shift;
 my $error_mode;
 my $exec_sqlfile;
 
 if (defined $error_handling) {
  
  switch ($error_handling) {
   
   case 'IGNORE_SQL_ERROR' {
    $error_mode = "WHENEVER OSERROR exit 1; ";
   }
   case 'IGNORE_OS_ERROR' {
    $error_mode = "WHENEVER SQLERROR exit SQL.SQLCODE; ";
   }
   case 'IGNORE_ALL_ERROR' {
    $error_mode = "";
   }
   
  }
  
 }
 else { # default
  $error_mode = "WHENEVER OSERROR exit 1; \nWHENEVER SQLERROR exit SQL.SQLCODE; ";
 }
 
 
 if ( $sql =~ /^\//) {  # starting with '/' -> filename    
  $exec_sqlfile = $sql;
 }
 else {
  &xOpen_File_Write( $sqlplusfile );
  &xWrite_File( $sql );
  &xClose_File;
  $exec_sqlfile = $sqlplusfile;
 }
  
    
  my $str = qq{ . /cs/oracle/local/dba/bin/basenv.ksh; . /cs/oracle/local/dba/bin/oraenv.ksh $instance;
sqlplus -s \\"/ as sysdba\\" << EOF > $sqlpluslogfile 2>&1
${error_mode}
\@$exec_sqlfile
EOF
  };
 $cmd = &xKSH_Call_Out_String( $str );
 &xWrite_Log("running:\n$error_mode\n".&mask_pwd($sql) );
   
 my $log            = qx( $cmd );
 my $err            = $?>>8;
 chomp(my $sqllog         = &xGobble( $sqlpluslogfile ));
 if ($err) {
  &xWrite_Log( &mask_pwd($sqllog) );
  return (-1, $sqllog);
 }
 else {
  if ( $sql !~ /feed off/ ) {
   &xWrite_Log("got:\n".&mask_pwd($sqllog));
  }
  return (0, $sqllog) ;
 }
}
Wednesday, July 20, 2016
run sqlplus in perl
Subscribe to:
Post Comments (Atom)
 
No comments:
Post a Comment