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

I am trying to use the Expect module to connect to a SFTP site and download files and move them to a new folder. When I run this, instead of simply sending the command, it breaks it into 80 character segments and the command fails. I looked at the actual code of the expect module and it is hard coded to break commands into 80 character segments. If I manually login to the site and type out the 140 characters required the command works.
my $exp = Expect->spawn("sftp","username@host") or fatalmsg("$sender + Cannot spawn sftp command"); $exp->expect($timeout, ["Password Authentication"], ["Are you sure you want to continue connecting", sub {my $se +lf = shift; $self->send("yes\n");}] ); $exp->expect($timeout, ["password:"]); $exp->send('xxxxx'); $exp->expect($timeout, ["sftp>"]); my $cmd = "rename path/Long_Filename new_path/Long_Filename"; $exp->send("$cmd\n"); $exp->expect($timeout, ["sftp>"]);
Here is the "print/send" sub in the Expect module:
sub print (@) { my ($self, @args) = @_; return if not defined $self->fileno(); # skip if closed if (${*$self}{exp_Exp_Internal}) { my $args = _make_readable(join('', @args)); cluck "Sending '$args' to ${*$self}{exp_Pty_Handle}\r\n"; } foreach my $arg (@args) { while (length($arg) > 80) { $self->SUPER::print(substr($arg, 0, 80)); $arg = substr($arg, 80); } $self->SUPER::print($arg); } }

Replies are listed 'Best First'.
Re: Why does Expect.pm break lines into 80 characters?
by nicomen (Novice) on Jan 12, 2016 at 15:53 UTC
    Could you have turned on the -l switch or have set $\ to something? (so that the Expect module's print call ends up having an extra linefeed?)
      That's it. $\ was set to "\n". Thanks!
Re: Why does Expect.pm break lines into 80 characters?
by salva (Canon) on Jan 13, 2016 at 07:31 UTC
Re: Why does Expect.pm break lines into 80 characters?
by eskiphill (Novice) on Jan 12, 2016 at 21:35 UTC
    Why are you using Perl to drive EXPECT to do SFTP work? I would think it would be much easier to use Perl to drive a SFTP client to download the file and then use Perl to move it. I process several thousand files a day this way. The SFT client is PSFTP, I fire it once to do a grab of the available files which gets stashed in a temp file. I use Perl to parse that file to pull out the file names and do a second call to PSFTP to download the specific files into a temp folder. I then delete the files from the SFT server and go through the temp folder and move the files to the appropriate destination. Has worked great for many years now.
Re: Why does Expect.pm break lines into 80 characters?
by hotchiwawa (Scribe) on Jan 12, 2016 at 17:58 UTC
    send method is an alias of print but did you try with send_slow?