#!/usr/bin/perl use strict; use warnings; use Net::SSH2; use Data::Dumper; my $server_file='test.txt'; my $local_file='test.local.txt'; my $server='server'; my $user='topeg'; my $pass='passwort'; my $ssh2 = Net::SSH2->new(); $ssh2->connect($server) or die("Can't connect $server ($!)"); if ($ssh2->auth_keyboard($user,$pass)) { my @stat=ssh_stat($ssh2,$server_file); print Dumper(\@stat); print ssh_read($ssh2,$server_file,$local_file)."\n"; } $ssh2->disconnect(); #0 dev #1 ino #2 mode #3 nlink #4 uid #5 gid #6 rdev = 0 #7 size #8 atime #9 mtime #10 ctime #11 blksize #12 blocks sub ssh_stat { my $ssh2=shift; my $file=shift; my @data=(); return @data unless($file); $file=~s/"/\\"/gs; my $chan = $ssh2->channel(); if($chan->exec(qq(/usr/bin/stat -c '%d:%i:%f:%h:%g:%u:0:%s:%X:%Y:%Z:%B:%b' "$file"))) { my $buff=''; $chan->read ( $buff, 1024); chomp($buff); @data=split(/:/,$buff); } $chan->close(); return @data; } # komprimierte Übertragung # das geht schneller sub ssh_read { my $ssh2=shift; my $file_ssh=shift; my $file_local=shift; my $gzip=shift // '/bin/gzip'; return 0 unless($file_ssh); return 0 unless($file_local); $file_ssh=~s/"/\\"/gs; $file_local=~s/"/\\"/gs; my $chan = $ssh2->channel(); my $ok=0; if($chan->exec(qq($gzip -cq1 "$file_ssh"))) { if( open(my $fh, '|-', qq($gzip -dqc > "$file_local") )) { my $buff=''; while($chan->read ( $buff, 1024)) { print $fh $buff; } if(close($fh)) { $ok=1; } else { warn("Can't run local $gzip ($!)"); } } else { warn("Can't run local $gzip ($!)"); } } else { warn("Can't run remote $gzip ($!)"); } $chan->close(); return $ok; }