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". ;-)

In reply to Re^3: SQLPLUS connect database by afoken
in thread SQLPLUS connect database by ytjPerl

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.