Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to check for a bad password?

Status
Not open for further replies.

bubsgt95

Programmer
Feb 21, 2003
6
US
this is a repost.. but i got no replys before.. and am just looking for some help.. if anyones out there.

i am currently creating a script that uses $telnet->login($user,$passwd) to login and then run the passwd command to change the password.. but the script bombs out if the current password is wrong for that server.. does anyone have any ideas to help me check for a bad login or password so that it will skip that host and move on to the next? heres some of the code: THANKS

if ($go eq "true") {
print "-------------------------------\nTrying to connect to -- $host...\n";
$ping = `/usr/sbin/ping $host 3`;
if ($ping =~/alive/) {
$telnet = new Net::Telnet (Timeout=>15, Prompt=>'/[\$%:#>] *$/' );
$telnet->open($host);
$telnet->login($username,$curpasswd1);
print "Connection established -- $host...\n";
$line = $telnet->lastline;
#if ($telnet=> 'New') {
#if the password is expired
print "This is line $line\n";
if($line =~ /Please/) {
print "expired aix\n";
$telnet->print($newpasswd1);
$telnet->waitfor('/assword/');
$telnet->print($newpasswd1);
$telnet->waitfor('/[\$%:#>] *$/');
$telnet->cmd('');

}
elsif($line =~ /assword/) {
print "expired sun hp\n";
$telnet->print($curpasswd1);
$telnet->waitfor('/assword/');
$telnet->print($newpasswd1);
$telnet->waitfor('/assword/');
$telnet->print($newpasswd1);
$telnet->waitfor('/[\$%:#>] *$/');
$telnet->cmd('');
}
else {
print "not expired\n";
$telnet->print('passwd');
$telnet->waitfor('/password:/');
$telnet->print($curpasswd1);
$telnet->waitfor('/assword/');
$telnet->print($newpasswd1);
$telnet->waitfor('/assword/');
$telnet->print($newpasswd1);
$telnet->waitfor('/[\$%:#>] *$/');
$telnet->cmd('');
}
$telnet->close;
print "password change successful on $host...\n";
} else {
print "** $host not pingable, aborted password change for $host **\n";
}
}
}
}
}

print "-------------------------------\n"
 
You need to set the error handling to something besides die(). Read the module's docs about the [tt]errmode()[/tt] method:
Code:
$telnet->errmode($mode);
From the POD:
Valid values for $mode are "die" (the default), "return", a coderef, or an arrayref.

jaa
 
... im kinda slow at picking this stuff up.. im kinda learning perl as i go here.. i dont suppose u have some kind of example i could look from?
 
One way is to wrap all your passwd chanding code in an if-statement that tests the success of the login. Since a successful login returns 1 you can setup the coderef of $mode to return undef (a false value). Something like
Code:
my $mode = 'return';
# The following does almost the same thing 
# but you can customize what the subroutine does
# (i.e. print an error message)
# my $mode = sub { print "Error!\n"; return undef };

...

my $telnet = new Net::Telnet (Timeout=>15, Prompt=>'/[\$%:#>] *$/' );
$telnet->errmode($mode);

...

if ($telnet->login($username,$curpasswd1))  {

    # Successful login to $host
    # Rest of pw changing code goes here

} else {

    # Loging failed, burp a message and continue 
    # to next host or rest of program
}
You can do some more elaborate error handling with the [tt]error()[/tt] and [tt]errmsg()[/tt] methods also described in the POD.

jaa
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top