Perl - 使用逐行变量循环 SSH 命令

Perl - Loop SSH command with line by line variable

我想 运行 通过 SSH 命令并使其循环,直到我的变量从文件中读取所有行。

我有这个:

$channel = $ssh->channel();
$channel->exec('echo -n "$command"') 
$channel->exec('rest of commands')

我需要做的是运行那个echo命令,变量是我本地文件/home/variables中的每一行循环。

它应该继续循环 echo 命令,直到我的文件中的所有行都完成,然后再移动到脚本的其余部分。

我想我应该使用类似的东西:

open my $enable, '<', '/home/variables';

while (my $command = <$enable>) {
chomp $command;
$channel->exec("echo -n $command");
last;
$channel->exec('next command');

虽然不是真正的循环。

提前致谢

试试这个:

#-- creating a channel
my $channel = $ssh->channel();
$channel->shell();

open my $enable, '<', 'x1';

while (my $command = <$enable>) {
  $channel->write( $command );
  }

$channel->write( $final_command );

另请注意,每个命令后都需要一个换行符,所以我省略了 chomp。

来自Net::SSH2 documentation

"Note that only one of these requests can succeed per channel (cp. "exec" in perlfunc); 如果您想 运行 一系列命令,请考虑使用 shell。"

希望对您有所帮助。