You didn't say if you want to check the password for a local or remote host. The other responses provide useful advice to validate the password by trying to access a remote system. However, it is not a good idea to allow root access at all. Consider to disable remote root access (see PermitRootLogin in sshd_config).
Instead, login using a non-privileged account and then check the root password locally - as described by rovf and marto.

If you want to check the root password locally, it would be nice to just mill the password and the salt through crypt and compare the result with the hashed password. However, on most systems, you need root privileges to access /etc/shadow in order to get the necessary information. Furthermore, some (most?) systems doesn't use crypt anymore.
Update: Well, seems that crypt is smart enough to handle a variety of hash algorithms. However, on systems that store hashes in /etc/shadow, privileged access is still required - even for validation. The script below works - at least here - with any non-privileged account.
.oO(always thought there is a program or library-call to validate passwords - but couldn't find one - other monks will know...)

Meanwhile, the following snipped might be a suitable workaround:
use strict; use warnings; use IPC::Open2; chomp(my $pwd = <>); my($chld_out, $chld_in); my $phrase = quotemeta "lookatme-i-am-roy"; my $pid = open2($chld_out, $chld_in, '/bin/su -c echo\\ ' . $phrase . ' 2>&1'); print $chld_in $pwd , "\n"; my $outcome = grep(/$phrase/, <$chld_out>); print "Password is ", $outcome ? "*valid*\n" : "not correct!\n"; waitpid($pid, 0);

It tries to run a single echo command after successfully switching user. The script checks for the expected output.

HTH

In reply to Re: script to verify password by Perlbotics
in thread script to verify password by perldesire

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.