ryalagikar has asked for the wisdom of the Perl Monks concerning the following question:

I am writing perl-expect script, in script am trying to login to remote machine then sudo su to another user and then execute two commands. Everything is working fine but the first command is executing multiple times.

my $ssh = Net::OpenSSH->new("$user:password\@$host"); $ssh->error and die "Unable to connect $host " . $ssh->error; my ($pty,$pid) = $ssh->open2pty({stderr_to_stdout => 1}, 'sudo su - us +er1') or die "Failed to attempt"; my $expect = Expect->init($pty); $expect->log_file("expect.pm_log", "w"); my $result = $expect->expect(3, ["\[\r\n]?\[^\r\n]+\[%#>\$] \$" ,sub { shift->send("da +te\r"); exp_continue;}], [qr/Day/ , sub { my $self = shift; $self->send("touch +test.txt\r"); $self->soft_close();}] )or die "**** Failed ** ";

Here 'date' command is executing multiple times(2...20times).

Suggestions Please

Replies are listed 'Best First'.
Re: Perl expect : send command executing multiple times
by salva (Canon) on Jul 03, 2015 at 08:15 UTC
    The first callback is returning exp_continue. That creates a loop: expect matches the prompt, sends the command, the command is run on the remote machine, a new prompt appears, expects matches the prompt again, etc,

      How to resolve this..? If i don't use exp_continue, then next command is not going to execute(only 'date' command will execute).

      Thanks in advance. .

        Just call expect several times.