#!/usr/bin/perl use Expect; use strict; use warnings; use Data::Dumper; my $path_to_file = "test.txt"; open my $handle, '<', $path_to_file or die "Could not open file '".$path_to_file."': $!"; chomp(my @devices = <$handle>); close $handle or warn "Could not close '".$path_to_file."': $!"; # print Dumper \@devices; my @params; my $username = "user"; my $ssh_copy_id = "ssh-copy-id ".$username."\@"; my $password = "password"; # These is to bypass the foreach my $device (@devices) { # create an Expect object by spawning another process my $command = join('', $ssh_copy_id, $device); # print $command . "\n"; my $session = Expect->spawn($command) or die "Error calling external program: $!"; my $output; $session->expect(10, [ qr/passphrase/i, sub { my $self = shift; $self->send("$password\n"); exp_continue; }], [ qr/my comments/i, sub { my $self = shift; $output = $self->exp_before; exp_continue; }], ); print $output . "\n" if $output; $session->soft_close; }