I wrote a program not too long ago that needed to run another program and slurp up the output. I used something I believe is called an "Exec Fork", like this:

open PROG, "-|" || exec './prog', 'arg'; @chunks = <PROG>; close PROG;

I'm assuming that I can do that in reverse, sending output from my script as input to the foreign program. Specifically, I'm trying to run passwd, as root, from a perl script.

My code looks like

open (PASSWD, "|-") || exec 'passwd', $name; print PASSWD "$password\n$password\n"; close PASSWD;

and it does launch passwd, but still prompts for the new password at the console, interactively. The script doesn't seem to be printing to it.

I assume I'm doing something wrong. Can you see what it is?

Before I get a (probably deserved) "don't reinvent the wheel; use X" response, I did find and try Unix::PasswdFiles, but it didn't seem to work with the shadow file on my OpenBSD installation. In fact, it replaced the entire file with the one line it was adding. That would have been very scary if I hadn't backed up first. I decided it was safer to use the supplied program...if I can get it to work. I'm still open to alternative suggestions, but I didn't want you to think I was ignoring all of the work that's come before me ;)


In reply to Exec Fork Trick by ginseng

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.