#!/usr/bin/perl use warnings; use strict; use CGI; use CGI::Carp qw(fatalsToBrowser); my $user = $ARGV[0]; my $password_old = $ARGV[1]; my $password_new = $ARGV[2]; my $host = $ARGV[3]; use Net::SSH::Expect; my $ssh = Net::SSH::Expect->new ( host => "$host", user => "$user", password => "$password_old", raw_pty => 1, ); my $login_output = $ssh->login(); $ssh->exec("stty raw -echo"); $ssh->send("passwd"); $ssh->waitfor('password:\s*\z', 1) or die "prompt 'password' not found after 1 second"; $ssh->send("$password_old"); $ssh->waitfor(':\s*\z', 1) or die "prompt 'New password:' not found"; $ssh->send("$password_new"); $ssh->waitfor(':\s*\z', 1) or die "prompt 'Confirm new password:' not found"; $ssh->send("$password_new"); my ($before_match, $match) = $ssh->waitfor('>\s*\z', 1); # waitfor() in a list context