Hi wise monks, after several days of going in circles I would like some help.
I have a webform (CGI) that I am developing, and after the user fills in various parts of the form, I need to copy a file from one server to another. I have a short sample code (that isn't CGI-based but may illustrate my problem) below:

#!/usr/bin/perl -w -T use Net::SSH::Expect; my $ssh = Net::SSH::Expect->new ( host => "<IP>", user => "<username>", raw_pty => 1 ); $ssh->run_ssh() or die "SSH Process couldn't start: $!"; $ssh->exec("stty raw -echo"); my $who = $ssh->exec("whoami"); print ($who); my $scp = "scp <file> <username>\@<IP Address>:<destinationpath>; $ssh->exec($scp) or die "SCP Process error: $!";

I understand that the taint flag isn't necessary in this short example, but it is required for the main application and I believe is causing most of my problems.
Prior to this attempt, I have also tried using system() to run the scp:

system('ssh <user>\@<IP> scp $file <user2>\@<Destination IP>:<destinat +ion path>');

This involved untainting all variables before this call. This attempt resulted in a slew of errors ("Can't exec 'ssh': No such file or directory", "sh: 1: ssh: not found").
I had read (although I may be wrong) that the Net::SSH::Expect module does not have full support for taint mode.
Also, as the script is generally being run from the web, the user for the commands is www-data (I don't know exactly what relevance this has, but it seems to be important, at least in my debugging attempts).
Thank you very much for any help or advice you can give. If more information is needed, I'm happy to provide it (I just can't think of anything else to include at the moment). Cheers!


In reply to Using SSH and SCP in a perl script with taint flag by stefl

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.