在 Perl 中调用具有不同属性的函数
我编写了一个 Perl 脚本,它将启动 SNMP 会话并提取数据/计数器及其值到 csv 文件。有7个perl脚本;顶部有不同的属性/定义/变量..但引擎是相同的。
此时,除了定义的变量之外,那 7 个 perl 脚本都是多余的。有没有办法将执行 perl 脚本保留为属性/执行文件并将引擎保留在另一个文件中?此属性/执行 perl 脚本将调用引擎(使用其自己的脚本中定义的属性)。
简而言之,我想在自己的脚本中使用变量(也作为执行),但从统一的“引擎”调用特定的函数。
即
检索_mibs1.pl检索_mibs2.pl 检索_mibs3.pl 检索_mibs4.pl 检索_mibs5.pl 检索_mibs6.pl 检索_mibs7.pl
检索_mibs1.pl
#!/usr/local/bin/perl
use Net::SNMP;
##DEFINITION START
my @Servers = (
'server1',
'server2',
);
my $PORT = 161;
my $COMMUNITY = 'secret';
my $BASEOID = '1.2.3.4.5.6.7.8';
my $COUNTERS = [
[11,'TotalIncomingFromPPH'],
[12,'TotalFailedIncomingFromPPH'],
];
##ENGINE START
sub main {
my $stamp = gmtime();
my @oids = ();
foreach my $counter (@$COUNTERS) {
push @oids,("$BASEOID.$$counter[0].0");
}
foreach my $server (@Servers) {
print "$stamp$SEPARATOR$server";
my ($session,$error) = Net::SNMP->session(-version => 1,-hostname => $server,-port => $PORT,-community => $COMMUNITY);
if ($session) {
my $result = $session->get_request(-varbindlist => \@oids);
if (defined $result) {
foreach my $oid (@oids) {
print $SEPARATOR,$result->{$oid};
}
} else {
print STDERR "$stamp Request error: ",$session->error,"\n";
print "$SEPARATOR-1" x scalar(@oids);
}
} else {
print STDERR "$stamp Session error: $error\n";
print "$SEPARATOR-1" x scalar(@oids);
}
print "\n";
}
}
main();
I have written a Perl script that would start a SNMP session and extracting the data/counters and it's value to a csv file. There are 7 perl scripts; different properties/definition/variables on the top.. but the engine is the same.
At this point, those 7 perl scripts are redundant except for the defined variables. Is there a way to keep the execution perl script as a properties/execution file and keep the engine in a another file? This properties/execution perl script will call the engine (using the properties defined in it's own script).
So in short, I want to use the variables in their own script (as an execution as well), but calls a specific function from a unified "engine".
i.e.
retrieve_mibs1.pl retrieve_mibs2.pl
retrieve_mibs3.pl
retrieve_mibs4.pl
retrieve_mibs5.pl
retrieve_mibs6.pl
retrieve_mibs7.pl
retrieve_mibs1.pl
#!/usr/local/bin/perl
use Net::SNMP;
##DEFINITION START
my @Servers = (
'server1',
'server2',
);
my $PORT = 161;
my $COMMUNITY = 'secret';
my $BASEOID = '1.2.3.4.5.6.7.8';
my $COUNTERS = [
[11,'TotalIncomingFromPPH'],
[12,'TotalFailedIncomingFromPPH'],
];
##ENGINE START
sub main {
my $stamp = gmtime();
my @oids = ();
foreach my $counter (@$COUNTERS) {
push @oids,("$BASEOID.$counter[0].0");
}
foreach my $server (@Servers) {
print "$stamp$SEPARATOR$server";
my ($session,$error) = Net::SNMP->session(-version => 1,-hostname => $server,-port => $PORT,-community => $COMMUNITY);
if ($session) {
my $result = $session->get_request(-varbindlist => \@oids);
if (defined $result) {
foreach my $oid (@oids) {
print $SEPARATOR,$result->{$oid};
}
} else {
print STDERR "$stamp Request error: ",$session->error,"\n";
print "$SEPARATOR-1" x scalar(@oids);
}
} else {
print STDERR "$stamp Session error: $error\n";
print "$SEPARATOR-1" x scalar(@oids);
}
print "\n";
}
}
main();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用 eval 来完成此操作:在一个文件中设置变量,然后打开引擎并评估其内容。
variables.pl(设置变量并调用引擎):
engine.pl(实际工作内容):
其他替代方法是:
engine.pl
并评估的内容>@ARGV
use
该模块You could do it using eval: set up the variables in one file, then open the engine and eval it's content.
variables.pl (set up your variables and call the engine):
engine.pl (the actual working stuff):
Other alternatives would be:
engine.pl
and evaluate the contents of@ARGV
use
this module我立即想到两个想法:
为您的公共代码构建一个 Perl 模块,然后
要求或
使用
模块。 (区别主要在于您是否要运行LynxLee::run_servers()
还是run_servers()
——您是否希望该模块影响您当前的作用域。)使用符号链接:创建这些符号链接:retrieve_mibs1.pl ->检索_mibs.pl 检索_mibs2.pl -> retrieve_mibs.pl 等,然后根据程序名称设置变量:
用程序名称索引到哈希中以检索参数会更清晰,但我不太擅长 Perl 引用。 :)
Two thoughts come to mind immediately:
Build a Perl module for your common code, and then
require
oruse
the module as your needs dictate. (The difference is mostly whether you want to runLynxLee::run_servers()
orrun_servers()
-- do you want the module to influence your current scope or not.)Use symbolic links: create these symlinks: retrieve_mibs1.pl -> retrieve_mibs.pl retrieve_mibs2.pl -> retrieve_mibs.pl, and so on, then set the variables based on the program name:
Indexing into a hash with the name of the program to retrieve the parameters would be much cleaner, but I'm not so good at Perl references. :)
我不确定问题是什么,所以我猜测一下。除了一些变量之外,您在不同位置的代码每次都是相同的。这就是子程序的定义。
也许问题是您不知道如何在这些不同的脚本中包含通用代码。这相当简单:您在 perl 模块中编写该代码。这基本上是一个以
pm
而不是pl
结尾的文件。当然,您必须处理很多事情,例如导出函数。 Perldoc 应该有很大帮助。I'm not sure what the problem is so I'm guessing a little. You have code in various places that is the same each time save for some variables. This is the very definition of a subroutine.
Maybe the problem is that you don't know how to include the common code in those various scripts. This is fairly easy: You write that code in a perl module. This is basically a file ending in
pm
instead ofpl
. Of course you have to take care of a bunch of things such as exporting your functions. Perldoc should be of great help.