Perl - file processing

I have a list of files on the host machine in the / src / directory. The directory has more subdirectories. Now this directory is copied to the remote computer after installation. The files are now preconfigured on the remote computer in the / dst directory.

Example. If I had / src / a / f 1, / src / b / f2 I will be on the remote machine / dst / a / f 1, / dst / b / f2

Now I have only information about the host directory, the host file. Using this information, how can I access files on a remote machine using ssh in perl. I would need cd to / dst and read the files from there. How can I make this cd and read in one ssh command.

Thank.

+3
source share
2 answers

/usr/bin/ssh, Net:: SSH2 :: Spec

scp_get ( remote [, local ] )

.

use File::Spec ();
use Net::SSH2 ();
my ( $vol, $dir, $file ) = File::Spec::splitpath( $path );
my @dirs = File::Spec::splitdir( $dir );

## Change the root dir
$dirs[0] = 'dst'; # new_root_dir

my $new_remote_path = File::Spec::catfile( @dirs, $file );

  ## Almost right from Net::SSH2 SYNOPSIS
  my $ssh2 = Net::SSH2->new();
  $ssh2->connect('example.com') or die $!;
  if ($ssh2->auth_keyboard('fizban')) {

      my $sftp = $ssh2->sftp();

      my $fh = $sftp->open('/etc/passwd') or die;
      print $_ while <$fh>;

      ## or $ssh2->scp_get( $new_remote_path );
  }
+2

Googling 'man ssh' SSH. (http://unixhelp.ed.ac.uk/CGI/man-cgi?ssh+1), . .

ssh [-1246AaCfgkMNnqsTtVvXxY] [-b bind_address] [-c cipher_spec] [-D port] [-e escape_char] [-F configfile] [-i identity_file] [-L [bind_address:] port: host: hostport] [-l login_name] [-m mac_spec] [-O ctl_cmd] [-o option] [-p port] [-R] [bind_address:] port: host: hostport] [-S ctl_path] [user @] [ ]

...

ssh (SSH-) - . rlogin rsh, . X11 TCP/IP .

ssh ( ). .

, .

, ssh user@hostname ls -lR /src/, 'ls -lR' , .

, man - , , SSH . , , // .

0

Source: https://habr.com/ru/post/1752335/