From: Zippy <spa...@ya...> - 2004-04-23 21:44:22
|
I'm not able to get the script below to work. It is trying to connect to a host via ssh. It is supposed to wait for the password prompt and then send a valid password, but for some reason it looks like the first line that I read in is "Permission denied, please try again." Since waiting for the password prompt kept failing, I did a getline() as soon as the ssh object was created and received "Permission denied, please try again." If I use RSA keys and remove the log in portion of the script, everything works fine. It is just the initial connection. It acts like, before I can send my password, it is automatically sending something and entering it. Does anyone know what is going on? Thanks for any help. #!/usr/bin/perl # Figure 6.6: Changing passwords over a Secure Shell connection # file: change_passwd_ssh.pl use strict; use Net::Telnet; use Getopt::Long; use IO::Pty; use POSIX 'setsid'; use constant PROMPT => '/[\$%#>] $/'; use constant DEBUG => 1; my $user = shift || $ENV{LOGNAME}; #my $old = get_password('old password'); #my $new = get_password('new password'); my $old = "*****"; my $new = "*****"; my $host = shift; change_passwd($host,$user,$old,$new); sub change_passwd { my ($host,$user,$oldpass,$newpass) = @_; my $ssh = do_cmd('ssh',$host) or die "couldn't launch ssh subprocess"; my $shell = Net::Telnet->new(Fhopen => $ssh); $shell->binmode(1); $shell->input_log('passwd.log') if DEBUG; $shell->dump_log('dump.log') if DEBUG; $shell->errmode('return'); my $line = $shell->getline(); print "line=$line\n"; $shell->waitfor('/password: $/') or return $shell->errmsg,"\n"; *******problem occurs here******* $shell->print($oldpass); $shell->waitfor(PROMPT) or return "host refused login: wrong password?\n"; $shell->print('passwd'); $shell->waitfor('/password:/') or return warn "$host: ",$shell->errmsg,"\n"; $shell->print($oldpass); my($pre,$match) = $shell->waitfor(Match => '/error/', Match => '/password:/'); $match =~ /password/ or return warn "$host: Incorrect password.\n"; $shell->print($newpass); ($pre,$match) = $shell->waitfor(Match => '/Bad password/', Match => '/Retype new password:/'); $match =~ /Retype/ or return warn "$host: New password rejected.\n"; $shell->print($newpass); $shell->waitfor('/successfully\./') or return warn "$host: ",$shell->errmsg,"\n"; print "Password changed for $user on $host.\n"; } sub do_cmd { my ($cmd,@args) = @_; my $pty = IO::Pty->new or die "can't make Pty: $!"; defined (my $child = fork) or die "Can't fork: $!"; return $pty if $child; setsid(); my $tty = $pty->slave; close $pty; STDIN->fdopen($tty,"r") or die "STDIN: $!"; STDOUT->fdopen($tty,"w") or die "STDOUT: $!"; STDERR->fdopen(\*STDOUT,"w") or die "STDERR: $!"; close $tty; $| = 1; exec $cmd, @args or die "Couldn't exec: $!"; } |