Re: System() or the like
by blakem (Monsignor) on Sep 18, 2001 at 02:10 UTC
|
You can use the backticks (like the apostrophe, but slanted the other way... usually found next to the !/1 key) to capture the output of another
process....
$run="snmpwalk $host $opt_c ifType.".$thisport;
$type=`$run`;
print "$MAC $type $ifName $vlan $host\n";
-Blake
| [reply] [d/l] |
Re: System() or the like (code)
by deprecated (Priest) on Sep 18, 2001 at 02:48 UTC
|
Um, I am surprised nobody has mentioned qx//. I tend to prefer qx{ foo } over backticks because it is much easier to see at high resolution.
Also, several shell programming books I have read say that the use of backticks is, er, deprecated. They prefer to use $(), which qx{} somewhat resembles.
Just my too sense.
brother dep
--
Laziness, Impatience, Hubris, and Generosity. | [reply] |
|
I've not heard that Perl's backticks are deprecated. The $() syntax is a ksh feature, which by the way nests very easily, and is not available in sh. I don't agree that $() looks like qx{} but regardless agree that it's a more elegant solution.
| [reply] |
Re: System() or the like
by geektron (Curate) on Sep 18, 2001 at 03:17 UTC
|
get_table() - retrieve a table from the remote agent
Blocking
$response = $session->get_table($oid);
Non-blocking
$ok = $session->get_table(
-baseoid => $oid,
[-callback => sub {},]
[-delay => $seconds]
);
it might be best to use a module here rather than depending on system commands and/ or backticks.
i do a fair to large amount of SNMP work at work, and I use SNMP.pm for most, if not all, apps that need this kind of info. | [reply] [d/l] |
|
Just to avoid confusion, I think you mean Net::SNMP.
| [reply] |
Re: System() or the like
by maverick (Curate) on Sep 18, 2001 at 02:13 UTC
|
Try opening the command to a pipe.
open(PIPE,"$run |") || die "pipe open failed: $!";
while(<PIPE>) {
print $_;
}
close(PIPE);
/\/\averick
perl -l -e "eval pack('h*','072796e6470272f2c5f2c5166756279636b672');"
| [reply] [d/l] |
Re: System() or the like
by VSarkiss (Monsignor) on Sep 18, 2001 at 02:12 UTC
|
$run="snmpwalk $host $opt_c ifType.".$thisport;
$type=`$run`;
print "$MAC $type $ifName $vlan $host\n";
More information at perlfunc:system; in particular, the second paragraph.
HTH
| [reply] [d/l] |
Re: System() or the like
by howie (Sexton) on Sep 18, 2001 at 03:13 UTC
|
either $type=`$run`; (those are backticks, not quotes) or
open(OUTPUT,"$run|");
$type=<OUTPUT>;
close(OUTPUT);
should do it. Both are in the 'Social Perl' chapter of the camel book, IIRC. | [reply] [d/l] [select] |
Re: System() or the like
by cider (Acolyte) on Sep 18, 2001 at 12:57 UTC
|
Yes.
I present thee with hoho_system(), it can be called with the silent option below to not additionally display output to stdout:
- $type = hoho_system("$run", "silent");
Or, it can also be noisy, and may be executed like so:
- $type = hoho_system("$run");
And for bonus points, I have included support for an optional global $logfile variable, which can be declared as the following optionally:
- $logfile = "some_file_that_exists";
# on with the show, cider@compulsion.org
sub hoho_system {
my ($cmd, $option) = @_;
my ($returned_result);
my $silent = 0;
if ( defined($option) ) { if ( $option =~ m/silent/ ) { $silent = 1
+; } }
if ( defined($logfile) ) { open LOG, "+>> $logfile"; print LOG "$cm
+d: $_\n"; }
open CMD, "$cmd |" or die "failure running $cmd: $!\n";
while(<CMD>) {
print unless $silent;
print LOG "$_"; $returned_result = "$_\n$returned_result";
}
close CMD;
if ( defined($logfile) ) { close LOG; }
return $returned_result;
}
Using arrays instead of scalars in multi-line responses would be advisable, and for such you may change the $returned_result references to @returned_result, and change the $returned_result assignment line to push @returned_result, "$_";
busunsl: switched pre tag to code tag. htmlism.
| [reply] [d/l] |
|
Sorry to spoil that, but I think if you present a solution, it should work:
running it gives me:
syntax error at C:\WINNT\Profiles\int5398\src\test\xx.pl line 9, near
+"if $option "
syntax error at C:\WINNT\Profiles\int5398\src\test\xx.pl line 19, near
+ "}"
The line while () { starts an endless loop.
$returned_result will be empty.
And even if it would contain something, it would be in reverse order.
Please test your code before posting. | [reply] [d/l] [select] |
DONE: System() or the like
by PyroX (Pilgrim) on Sep 18, 2001 at 20:27 UTC
|
Thanks guys,
It looks like this will work the best for me, thank you everyone. $run="snmpwalk $host $opt_c ifType.".$thisport;
open(PIPE,"$run |") || die "Failed to get type, stopped.";
while(<PIPE>) {
$type=$_;
}
close(PIPE);
print "$MAC $type $ifName $iftomodule{$ifIndex} $iftopor
+t{$ifIndex} $ifIndex $bridgeport $vlan $host\n";
Edit kudra,
2001-09-19
Added code tags
| [reply] [d/l] |
|
Thank you soooooo much this help a ton!
$run="dir.".$thisport;
open(PIPE,"$run |") || die "Failed to get type, stopped.";
while(<PIPE>) {
$type=$_;
print($type);
}
close(PIPE);
| [reply] |