我想通过 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 :“请注意,每个 channel 中只有一个请求可以成功(cp。perlfunc 中的“exec”);如果要运行一系列命令,请考虑改用 shell。”
我希望这会有所帮助。
关于Perl - 使用逐行变量循环 SSH 命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28533538/