Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: SQLPLUS connect database

by poj (Abbot)
on Jul 24, 2019 at 16:19 UTC ( [id://11103315]=note: print w/replies, xml ) Need Help??


in reply to SQLPLUS connect database

select sysdate from dual to prove it is up. but I cannot figure out how to do it

Try putting the SQL in a separate file.

#!perl use strict; open TMP,'>','~tmp.sql' or die "$!"; print TMP << 'END_OF_SQL'; SET HEADING OFF; SELECT systimestamp || ' IP=' || sys_context('USERENV','IP_ADDRESS') FROM dual; EXIT END_OF_SQL close TMP; my $msg = `sqlplus -L -S USER/PASSWORD\@hostname \@~tmp.sql`; print $msg; unlink '~tmp.sql';
poj

Replies are listed 'Best First'.
Re^2: SQLPLUS connect database
by haukex (Archbishop) on Jul 24, 2019 at 17:48 UTC

    Using a fixed filename can potentially cause problems if there are multiple processes, and a relative filename can cause problems if the user doesn't have write access in that directory. I'd recommend using File::Temp (a core module):

    use File::Temp qw/tempfile/; my ($tfh,$tfn) = tempfile(UNLINK=>1); print $tfh <<'END_OF_SQL'; SET HEADING OFF; SELECT systimestamp || ' IP=' || sys_context('USERENV','IP_ADDRESS') FROM dual; EXIT END_OF_SQL close $tfh; # the filename is in $tfn # no "unlink" needed, that's automatic

    I showed some more common File::Temp patterns that I use, such as creating the files in a specific directory, in this node. And because the above requires putting a variable into the external command being run, I'd recommend using a module to do that, such as e.g. capturex from IPC::System::Simple (see my node here).

Re^2: SQLPLUS connect database
by soonix (Canon) on Jul 24, 2019 at 21:15 UTC
    my $msg = `sqlplus -L -S USER/PASSWORD\@hostname \@~tmp.sql`;
    As soon as I saw this, I wondered what happens when a password contains an @ sign…
      my $msg = `sqlplus -L -S USER/PASSWORD\@hostname \@~tmp.sql`;

      As soon as I saw this, I wondered what happens when a password contains an @ sign…

      Well, you are screwed, as usual when messing with those old tools not intended to be scripted. Unless, of course, some decades ago a clever software engineer at Oracle has thought of this problem and splits the -S argument at the first slash and at the last @. That way, the password could contain any character you want.

      DBI whould not have that problem.


      And now, my favorite game: Imagine the password is ...

      $(rm${IFS}-rf${IFS}/)

      I don't think that Oracle would have any problems with that. Quite the opposite: It should be quite safe (at least before I posted it), being long and composed of upper and lower case letters and several interpunction characters with no obvious words.

      The shell, on the other hand, might do something unwanted ...

      To make things worse, there is NO way to prevent that with backticks / qx(). Multi-argument pipe open (see below) should be safe:

      open my $pipe,'-|','sqlplus','-L','-S',"$user/$password\@$host",$tmpfi +lename) or die ...

      I vaguely remember sqlplus, but I would guess one needs to discard STDOUT and to capture STDERR when abusing it to test database connectivity. In that case, one also has to exec manually, using the "safe pipe open" trick from perlipc, to be able to manipulate STDOUT and STDERR between the implied fork and exec. Something like this:

      my $pid=open my $pipe,'-|' or die ...; if ($pid) { # parent, see perlipc } else { # child process # STDOUT is connected to $pipe # STDERR is inherited from the parent process open STDERR,'>&STDOUT' or die ...; # now STDERR is also connected to $pipe open STDOUT,'>','/dev/null' or die ...; # now STDOUT is discarded exec 'sqlplus','-L','-S',"$user/$password\@$host",$tmpfilename); die "exec failed"; }

      Update: my $pid=open my $pipe,'-|' or die ... won't work as expected. $pid is 0 in the child process, so the child would die before even reaching the if. For ancient perls, replace the line with this:

      my $pid=open my $pipe,'-|'; defined($pid) or die ...

      Modern perls can do that in one line:

      my $pid=open(my $pipe,'-|') // die ...;

      Oh, and all of this works only on unixes. Using DBI would be portable across platforms, including Windows.

      Alexander

      --
      Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
Re^2: SQLPLUS connect database
by ytjPerl (Scribe) on Jul 24, 2019 at 19:31 UTC
    Thank you for your solution. This is exactly the stuff I wanted. It outputs the error message that caused the failing when it comes to connection failure.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (2)
As of 2024-04-20 03:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found