开发者

perl - file handling

I have a list of files on host machine say in directory /src/. The directory has more subdirectories. now this directory is copied onto remote machine after mounting. Now the files are preset in remote machine in directory /dst.

Example. If I had /src/a/f1, /src/b/f2 I will have on remote machine /dst/a/f1 ,/dst/b/f2

Now I only have information on host directory,host file. Using this information how do I access files on rem开发者_如何转开发ote machine using ssh in perl. I would have to cd to /dst and read files from there. How do I do this cd and read in one single ssh command.

Thanks.


If you want to do it all without delegating to /usr/bin/ssh, check out Net::SSH2, and File::Spec

You'll need scp_get ( remote [, local ] )

It will look something like this.

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 );
  }


Googling 'man ssh' comes up with the SSH manual page. I followed the first link ( http://unixhelp.ed.ac.uk/CGI/man-cgi?ssh+1 ), from which we see the following. Interesting parts are specified.

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@]hostname [command]

...

ssh (SSH client) is a program for logging into a remote machine and for executing commands on a remote machine. It is intended to replace rlogin and rsh, and provide secure encrypted communications between two untrusted hosts over an insecure network. X11 connections and arbitrary TCP/IP ports can also be forwarded over the secure channel.

ssh connects and logs into the specified hostname (with optional user name). The user must prove his/her identity to the remote machine using one of several methods depending on the protocol version used.

If command is specified, command is executed on the remote host instead of a login shell.

So, try ssh user@hostname ls -lR /src/, replacing 'ls -lR' with whatever command you actually want.

EDIT Oh, and look at the man page for the identity file - if you have keys set up, you can use that to avoid providing a password to SSH on the command line. And make sure the whole path to your identity file has strong permissions, so nobody else can read/replace/edit it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜