in reply to Short example using IPC::Open2 or IPC::Open3
I couldn't test this 100% as-is but a similar version worked.#!/usr/bin/perl use strict; use warnings; use IPC::Open2; use Fcntl qw(F_SETFL F_GETFL O_NONBLOCK); my ($rh, $wh); my $pid = open2 ($rh, $wh, "sftp somehost") or die "can't sftp: $!"; my $flags = fcntl ($rh, F_GETFL, 0) or die "can't get flags: $!"; fcntl ($rh, F_SETFL, $flags|O_NONBLOCK) or die "can't set O_NONBLOCK: +$!"; print $wh "ls foo\n"; my @files; my $ln; while (! ($ln = <$rh>)) {} ## wait for valid output do { $ln=~/.+sftp>\s*//; ## eat sftp> prompts chomp $ln; push @files, $ln; } while ($ln = <$rh>); for (@files) { my $qf = quotemeta; print $wh "get foo/$qf\n"; print $wh "mv foo/$qf bar/$qf\n"; } print $wh "bye\n"; waitpid $pid, 0;
Edited (twice) to ignore sftp> prompts.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Short example using IPC::Open2 or IPC::Open3
by ikegami (Patriarch) on Oct 23, 2007 at 21:27 UTC | |
by gamache (Friar) on Oct 23, 2007 at 22:15 UTC |