perl:函数调用
下面的脚本用于ssh到路由器,从abc.txt文件读取信息并执行命令。脚本按预期工作perl:函数调用,perl,function,Perl,Function,下面的脚本用于ssh到路由器,从abc.txt文件读取信息并执行命令。脚本按预期工作 use strict; use warnings; use autodie; use feature qw/say/; use Net::SSH::Expect; print "\n[INFO] script Execution Started\n"; my $ssh = Net::SSH::Expect->new( host => "ip addr", password =&
use strict;
use warnings;
use autodie;
use feature qw/say/;
use Net::SSH::Expect;
print "\n[INFO] script Execution Started\n";
my $ssh = Net::SSH::Expect->new(
host => "ip addr",
password => ' user ',
user => 'pwd',
raw_pty => 1,
);
my $login_output = $ssh->login();
$ssh->exec("enter command 1");
$ssh->exec("enter command 2");
open my $pr, '<', 'abc.txt';
while (my $config = <$pr>) {
chomp $config;
my $conf = $ssh->exec("$config");
print("$conf");
}
函数调用部分不工作,不会抛出任何错误。我在这里遗漏了什么吗?如果使用正确的缩进,您的错误就会变得明显:
sub mysub {
my ($ssh,$filename) = @_;
$ssh->exec("command 1");
$ssh->exec("command 2");
open my $pr, '<', $filename;
while (my $config = <$pr>)
{
chomp $config;
my $conf = $ssh->exec("$config");
print("$conf");
}
mysub($ssh,"abc.txt"); # this should be outside
}
sub-mysub{
我的($ssh,$filename)=@;
$ssh->exec(“命令1”);
$ssh->exec(“命令2”);
打开我的$pr,'
sub mysub {
my ($ssh,$filename) = @_;
$ssh->exec("command 1");
$ssh->exec("command 2");
open my $pr, '<', $filename;
while (my $config = <$pr>)
{
chomp $config;
my $conf = $ssh->exec("$config");
print("$conf");
}
mysub($ssh,"abc.txt"); # this should be outside
}