找到你要的答案

Q:Running sftp commands using Expect.pm

Q:运行sftp命令使用expect.pm

I am trying to download few files from specific folders using sftp utility and am doing it through Expect.pm . Below is the code for that :

use strict;

use Expect;

my $userid = `whoami`;

chomp($userid);

my $Password = "<password>";

my $command = "sftp " . "$userid" . "\@<server-name>";

my $spawn_ok = 0;

my $timeout = 10;

print "$command \n";

my $exp = new Expect();
$exp->log_file("FTPLOGFILE.txt");
$exp->spawn("$command") or die "Cannot spawn $command: $!\n";

$exp->log_stdout(0);
$exp->expect($timeout,
            [ 'Password:',
              sub {
                    $spawn_ok = 1;
                    my $fh = shift;
                    print "Sending password \n";
                    $fh->send("$Password\r");
                    exp_continue;
                  }
            ],
            [ 'sftp> ',
              sub {
                      my $fh = shift;
                      $spawn_ok = 3;
                      print "Downloading cfg files \n";
                      $fh->send("get /home/cfg/*.cfg /tmp/ACC_CCM_CFG","\n");
                      $fh->send("bye","\n");
                      exp_continue;
                  }
             ]

The problem is apart from downloading files into the above folder : /tmp/ACC_CCM_CFG i also want to run the below command inside the same sftp session : get /home/appl/*.pl /tmp/ACC_CCM_APPL But i can't do that since the regular expression for the expect function would be same (sftp>) . How do i run a series of commands inside the same sftp session using expect if the regex of the prompt does not change .

Please throw some light on the above since i can't find any solution .

我试图使用SFTP实用特定的文件夹下载一些文件和正在做的expect.pm。下面是代码:

use strict;

use Expect;

my $userid = `whoami`;

chomp($userid);

my $Password = "<password>";

my $command = "sftp " . "$userid" . "\@<server-name>";

my $spawn_ok = 0;

my $timeout = 10;

print "$command \n";

my $exp = new Expect();
$exp->log_file("FTPLOGFILE.txt");
$exp->spawn("$command") or die "Cannot spawn $command: $!\n";

$exp->log_stdout(0);
$exp->expect($timeout,
            [ 'Password:',
              sub {
                    $spawn_ok = 1;
                    my $fh = shift;
                    print "Sending password \n";
                    $fh->send("$Password\r");
                    exp_continue;
                  }
            ],
            [ 'sftp> ',
              sub {
                      my $fh = shift;
                      $spawn_ok = 3;
                      print "Downloading cfg files \n";
                      $fh->send("get /home/cfg/*.cfg /tmp/ACC_CCM_CFG","\n");
                      $fh->send("bye","\n");
                      exp_continue;
                  }
             ]

The problem is apart from downloading files into the above folder : /tmp/ACC_CCM_CFG i also want to run the below command inside the same sftp session : get /home/appl/*.pl /tmp/ACC_CCM_APPL But i can't do that since the regular expression for the expect function would be same (sftp>) . How do i run a series of commands inside the same sftp session using expect if the regex of the prompt does not change .

请在上面扔些灯,因为我找不到任何解决方法。

answer1: 回答1:

Any reason for using Expect and system sftp command for this? Using Net::SFTP module would make the job much more easier and clean.

使用这个系统的期望和sftp命令任何理由吗?使用网络::SFTP模块会使工作更加简单和干净。

perl