Yes. The passwd utility is interactive. The chpasswd utility, on the other hand, will accept command line input. Syntax like this:

echo username:newpassword | /usr/sbin/chpasswd

chpasswd is available all most Linux distros as far as I know. On Red Hat it comes in the shadow-utils base system package. It's also supposed to be available on AIX 5.x and the BSD's. Inexplicably, it's missing from Solaris.

Here's a snippet that shows how it could be used in conjunction with Net::SSH::Perl as part of a mass password resetting script (FH is the handle for a file that contains target system and user data, including the new password -- to save space I've also removed error-checking code):

while (<FH>) { chomp; my ( $target, $username, $userpass ) = ( &parse_line(',',0,$_)); my $userstr = $username . "\:" . $userpass; print $target, " ", $userstr, "\n"; my $ssh = Net::SSH::Perl->new($target); $ssh->login($adminuser, $adminpass); my $cmd = "echo $userstr \| /usr/sbin/chpasswd"; my ($stdout, $stderr, $exit) = $ssh->cmd($cmd); }

In reply to Re^3: Net::SSH::Perl and Passwd by eldapo
in thread Net::SSH::Perl and Passwd by u235sentinel

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.