klactose has asked for the wisdom of the Perl Monks concerning the following question:

Hello monks,

Although I've used the site for tips for about a year, this is my first posting, so excuse me if I am not following correct protocol.

I have a problem that I can't figure out. I am programming in a windows environment, and I am trying to write a simple script that will run several curl commands.

My problem occurs when I run a curl command that needs a response (example "Enter host password for user 'username':").

I can't figure out how to actually send the password to curl.

I've tried several different methods but none have worked for me. Here is a snippet from my latest attempt:
my ($pid, $writeme); for(0..4){ $pid = open $writeme, "|-", "c:\\apps\\curl\\curl -u user$_ -X DELETE http://localhost:300 +0/site/users/user$_"; sleep 2; print $writeme "password$_\r"; #curl isn't receiving this input close $writeme; #perl hangs here sleep 1; }
The password that I send by printing to $writeme isn't being seen by curl for some reason, which leaves curl in a "waiting for input state".

If anyone could show me how I can fix this, I would be immensely grateful.

Thanks!

Replies are listed 'Best First'.
Re: How to control input of an external program?
by gmargo (Hermit) on Nov 09, 2009 at 19:34 UTC

    What's wrong with specifying the user/password on the command line? Save yourself the pain. From the man page:

    -u/--user <user:password>

      that will work for this specific instance, but what about in general?
        You already have the general solution. (Send the input to the program's STDIN.) You're asking for a specific solution. (For some reason, curl reads the password from the tty, not STDIN.)
Re: How to control input of an external program?
by ikegami (Patriarch) on Nov 09, 2009 at 19:43 UTC

    The curl library can be accessed from Perl more directly via WWW::Curl, WWW::Curl::Simple and LWP::Curl. You should check if any of those fulfill your needs.

    There's also LWP available to you, the standard Perl web module.

      Thanks, I will look into the Curl modules, didn't realize that Perl had any. I did look into LWP but I couldn't find info about support for the full range of HTTP methods, only for POST/GET. Perhaps I didn't look long enough!

        I don't know what you made you think that some methods aren't accepted, but testing shows DELETE is accepted

        $ perl -MHTTP::Request -e' print HTTP::Request->new( DELETE => "http://..." )->as_string ' DELETE http://...
Re: How to control input of an external program?
by ikegami (Patriarch) on Nov 09, 2009 at 20:10 UTC
    I just noticed you're sending \r instead of \n as you should. That could cause curl to hang.
      I tried \n first, but had the same result, \r was just the latest attempt at the time... haha... As I mentioned I was running into a dead end =)
Re: How to control input of an external program?
by Khen1950fx (Canon) on Nov 09, 2009 at 22:10 UTC
    If you use WWW::Curl, you'll want to read the tutorial for libcurl. I just looked over the tutorial, and I see three easy ways to do passwords:

    protocol://user:password@example.com/path/

    This appears to be the standard method for giving the password. You could also do:

    curl_easy_setopt(easyhandle, CURLOPT_USERPWD, "myname:thesecret");

    or

    curl_easy_setopt(easyhandle, CURLOPT_PROXYUSERPWD, "myname:thesecret");