This is looking pretty slick right out of the box. I get pretty good functionality, including being able to use keypairs to authenticate without having to change architecture. Output then source:
$ ./1.open.pl 1.hello.pl
...
/homepages/9/d349337426/htdocs
howdy
mkdir1 failed
remote dir is /perlmonks/scripts/cgi
.
..
1.color.cgi
1.env.cgi
1.refer.cgi
1.rhost.cgi
1.browser.cgi
1.envform.html
2.ssh2_1.pl
5.create.sh
1.hello.pl
Can't open perl script "1.hello.pl": No such file or directory
remote command failed: child exited with code 2 at ./1.open.pl line 40
+.
$ cat 1.open.pl
#!/usr/bin/perl -w
use 5.011;
use Data::Dumper;
use Net::OpenSSH;
use Net::SFTP::Foreign;
my $upload_file = shift;
my $ssh = get_tiny_open();
$ssh->system("pwd")
or die "remote command failed: " . $ssh->error;
my @ls = $ssh->capture("ls");
$ssh->error
and die "remote ls command failed: " . $ssh->error;
my ( $rin, $pid ) = $ssh->pipe_in("cat >/tmp/foo")
or die "pipe_in method failed: " . $ssh->error;
print $rin "howdy\n";
close $rin;
my ( $rout, $qid ) = $ssh->pipe_out("cat /tmp/foo")
or die "pipe_out method failed: " . $ssh->error;
while (<$rout>) { print }
close $rout;
my $sftp = $ssh->sftp();
$sftp->error and die "SFTP failed: " . $sftp->error;
my $server_dir = "perlmonks/scripts/cgi";
$sftp->mkdir("/$server_dir") or warn "mkdir1 failed $!\n";
$sftp->setcwd("/$server_dir") or warn "setcwd1 failed $!\n";
$sftp->put($upload_file) or warn "upload put failed $!\n";
my $remote_dir = $sftp->cwd;
say "remote dir is $remote_dir";
my $ls = $sftp->ls($remote_dir);
print "$_->{filename}\n" for (@$ls);
$ssh->system("perl $upload_file")
or die "remote command failed: " . $ssh->error;
undef $sftp;
undef $ssh;
sub get_tiny_open {
use 5.011;
use warnings;
use Config::Tiny;
use Data::Dumper;
use Net::OpenSSH;
my $ini_path = qw( /home/bob/Documents/html_template_data/3.values.i
+ni );
say "ini path is $ini_path";
my $sub_hash = "my_sftp";
my $Config = Config::Tiny->new;
$Config = Config::Tiny->read( $ini_path, 'utf8' );
say Dumper $Config;
# -> is optional between brackets
my $domain = $Config->{$sub_hash}{'domain'};
my $username = $Config->{$sub_hash}{'username'};
my $password = $Config->{$sub_hash}{'password'};
my $port = $Config->{$sub_hash}{'port'};
my $key_path = $Config->{$sub_hash}{'key_path'};
#dial up the server
say "values are $domain $username $password $port $key_path";
my $ssh = Net::OpenSSH->new(
$domain,
user => $username,
port => $port,
# password => $password,
key_path => $key_path
) or die "Can't connect: $!\n";
return $ssh;
}
__END__
$
I'm really happy with this result from less than an hour of poking at it. I didn't quite get what I wanted, where I would be able to execute the file I just uploaded.(?)
|