Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Executing unix commands from a Perl file

Status
Not open for further replies.

carlosAlberto

IS-IT--Management
Oct 9, 2001
109
GB
Hello,

I would like to execute the following unix command from my Perl file:

snmpwalk -v 3 -u testUser -l noAuthNoPriv $ipAddress $miboid

and then display the result to the screen.

How is is possible to execute this command and display the result???????????????

All help greatly appreciated.


Regards,
Carl.
 
a couple of different ways

easy:

$a = `your_command`;
print $a;

but not what you'd call flexible....

a bit harder:

open(CMD, "your_command|");
while(<CMD>){
print $_;
}

that way you can control how many lines you dislay, edit the content and format of the lines etc Mike

Want to get great answers to your Tek-Tips questions? Have a look at faq219-2884

It's like this; even samurai have teddy bears, and even teddy bears get drunk.
 
Hi,

I've took the above command a little further but have come up with another error i can't solve.

PROBLEM:
open(RESULTS, &quot;$snmpCommand -v 3 -u testUser -l noAuthNoPriv $ipAddress $miboid | &quot;);


The command works when it begins without a variable:
open(RESULTS, &quot;snmpwalk -v 3 -u testUser -l noAuthNoPriv $ipAddress $miboid | &quot;);


I have checked to make sure $snmpCommand contains the value 'snmpwalk' but i still get an error.


I am soooo puzzled.... please help!!

Carl.
 
oh

I define the variable as follows, just in case


my ($snmpCommand) = snmpwalk;
 
Should, I think, be:

my ($snmpCommand) = 'snmpwalk'; Mike

Want to get great answers to your Tek-Tips questions? Have a look at faq219-2884

It's like this; even samurai have teddy bears, and even teddy bears get drunk.
 
Hi Mike,

Thanks for the reply.

I tried that and that doesn't seem to work.

I also tried creating a variable as:


my $command = '$snmpCommand -v 3 -u testUser -l noAuthNoPriv $ipAddress $miboid | ';

open (RESULTS, $command);


which also didn't work.

????

Carl.
 
[tt]my $command = '$snmpCommand ...'[/tt]
wouldn't work. Variables don't expand in single quotes.

Try double quotes.
 
Hi,

I tried the previous comment with no success. Here are my commands:

my $snmpCommand = 'snmpwalk -v 3 -Os -u $userName -a MD5 -A $authPass -l authPriv -x DES -X $privPass $ipAddress $miboid |';

open (RESULTS, $snmpCommand)


 
Was that a typo, or still using single quotes? Your assignment should be:

[tt]my $snmpCommand = &quot;snmpwalk -v 3 -Os -u $userName -a MD5 -A $authPass -l authPriv -x DES -X $privPass $ipAddress $miboid |&quot;;[/tt]
 
what rosenk is saying:
Code:
my $var1 = &quot;Perl&quot;;
my $var2 = '$var1';
my $var3 = &quot;$var1&quot;;

print &quot;$var1\n$var2\n$var3\n&quot;;
outputs
Code:
Perl
$var1
Perl
When variables are within single quotes, they aren't interpolated (expanded out to their value), but within double quotes, they are. ----------------------------------------------------------------------------------
...but I'm just a C man trying to see the light
 
yes sorry, I've actually tried both ways.

I'll just include the lines i'm having problems with again:

my $snmpCommand = &quot;snmpwalk -v 3 -Os -u $userName -a MD5 -A $authPass -l authPriv -x DES -X $privPass $ipAddress $miboid |&quot;;

Search Find A Forum Keyword Search
Find An Expert Advanced Search

Programmers > Languages > Perl Forum
Executing unix commands from a Perl file
thread219-455027

Read
New Posts Reply To
This Thread
E-mail It
Print It Next
Thread
carlosAlberto (IS/IT--Manageme) Jan 22, 2003
Hello,

I would like to execute the following unix command from my Perl file:

snmpwalk -v 3 -u testUser -l noAuthNoPriv $ipAddress $miboid

and then display the result to the screen.

How is is possible to execute this command and display the result???????????????

All help greatly appreciated.


Regards,
Carl.





Inappropriate post?
If so, Red Flag it!


Check out the FAQ
area for this forum!




Tek-Tips Forums is Member Supported. Click Here to donate.
MikeLacey (MIS) Jan 22, 2003
a couple of different ways

easy:

$a = `your_command`;
print $a;

but not what you'd call flexible....

a bit harder:

open(CMD, &quot;your_command|&quot;);
while(<CMD>){
print $_;
}

that way you can control how many lines you dislay, edit the content and format of the lines etc
Mike

Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884

It's like this; even samurai have teddy bears, and even teddy bears get drunk.




Mark this post as a helpful/expert post!


Inappropriate post?
If so, Red Flag it!


Check out the FAQ
area for this forum!



marblecat (Programmer) Jan 23, 2003
system(&quot;your_command&quot;);


Mark this post as a helpful/expert post!


Inappropriate post?
If so, Red Flag it!


Check out the FAQ
area for this forum!



carlosAlberto (IS/IT--Manageme) Jan 29, 2003
Hi,

I've took the above command a little further but have come up with another error i can't solve.

PROBLEM:
open(RESULTS, &quot;$snmpCommand -v 3 -u testUser -l noAuthNoPriv $ipAddress $miboid | &quot;);


The command works when it begins without a variable:
open(RESULTS, &quot;snmpwalk -v 3 -u testUser -l noAuthNoPriv $ipAddress $miboid | &quot;);


I have checked to make sure $snmpCommand contains the value 'snmpwalk' but i still get an error.


I am soooo puzzled.... please help!!

Carl.






Inappropriate post?
If so, Red Flag it!


Check out the FAQ
area for this forum!



carlosAlberto (IS/IT--Manageme) Jan 29, 2003
oh

I define the variable as follows, just in case


my ($snmpCommand) = snmpwalk;





Inappropriate post?
If so, Red Flag it!


Check out the FAQ
area for this forum!



MikeLacey (MIS) Jan 29, 2003
Should, I think, be:

my ($snmpCommand) = 'snmpwalk';
Mike

Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884

It's like this; even samurai have teddy bears, and even teddy bears get drunk.




Mark this post as a helpful/expert post!


Inappropriate post?
If so, Red Flag it!


Check out the FAQ
area for this forum!



carlosAlberto (IS/IT--Manageme) Jan 29, 2003
Hi Mike,

Thanks for the reply.

I tried that and that doesn't seem to work.

I also tried creating a variable as:


my $command = '$snmpCommand -v 3 -u testUser -l noAuthNoPriv $ipAddress $miboid | ';

open (RESULTS, $command);


which also didn't work.

????

Carl.





Inappropriate post?
If so, Red Flag it!


Check out the FAQ
area for this forum!



rosenk (Programmer) Jan 29, 2003
my $command = '$snmpCommand ...'
wouldn't work. Variables don't expand in single quotes.

Try double quotes.


Mark this post as a helpful/expert post!


Inappropriate post?
If so, Red Flag it!


Check out the FAQ
area for this forum!



carlosAlberto (IS/IT--Manageme) Jan 29, 2003
Hi,

I tried the previous comment with no success. Here are my commands:

my $snmpCommand = &quot;snmpwalk -v 3 -Os -u $userName -a MD5 -A $authPass -l authPriv -x DES -X $privPass $ipAddress $miboid |&quot;;

open (RESULTS, $snmpCommand);

@replyData = <RESULTS>;


To which @replyData is empty.


The following command did work:
open (RESULTS, &quot;snmpwalk -v 3 -Os -u $userName -a MD5 -A $authPass -l authPriv -x DES -X $privPass $ipAddress $miboid |&quot;;

But to bring flexibility there is more than 1 type of snmp command.

Thanks for your help.

Carl.

 
ooooops, not sure what happened there....



yes sorry, I've actually tried both ways.

I'll just include the lines i'm having problems with again:

my $snmpCommand = &quot;snmpwalk -v 3 -Os -u $userName -a MD5 -A $authPass -l authPriv -x DES -X $privPass $ipAddress $miboid |&quot;;

open (RESULTS, $snmpCommand);

@replyData = <RESULTS>;


To which @replyData is empty.


The following command did work:
open (RESULTS, &quot;snmpwalk -v 3 -Os -u $userName -a MD5 -A $authPass -l authPriv -x DES -X $privPass $ipAddress $miboid |&quot;;

But to bring flexibility there is more than 1 type of snmp command.

Thanks for your help.

Carl.
 
Very strange. This test works perfectly fine for me:
[tt]my $command = &quot;ls |&quot;;
open(RESULTS, &quot;$command&quot;);
print while (<RESULTS>);[/tt]

I don't think it will help me know why it's not working, but what version of perl are you using and under what OS?
 
This may seem obvious, but is the assignment to $snmpCommand
Code:
my $snmpCommand = &quot;snmpwalk -v 3 -Os -u $userName -a MD5 -A $authPass -l authPriv -x DES -X $privPass $ipAddress $miboid |&quot;;
being made after the other variables ($userName, $authPass, etc.) have valid values assigned to them?

Also, are you running this with warnings and strict turned on? (i.e. [tt]use warnings; use strict;[/tt])

jaa
 
Jaa,

Yep, the other variables are assigned first.

I don't have any warning declarations... Is there 1 of the 2 i should be using do you think????


Thanks.

Carl.
 
I'm actually 'printing' the $snmpCommand variable which is exactly correct. I have copied it straight to the command line and ran fine.

But for some reason the open () is dieing.......


Carl.
 
I have the following piece of code to assigned the $snmpCommand variable.



if ($miboid =~ /Table$/) {
my $snmpCommand = &quot;snmptable -v 3 -Os -u $userName -a MD5 -A $authPass -l authPriv -x DES -X $privPass -CB $ipAddress $miboid |&quot;;
}
else {
my $snmpCommand = &quot;snmpwalk -v 3 -Os -u $userName -a MD5 -A $authPass -l authPriv -x DES -X $privPass $ipAddress $miboid |&quot;;
print &quot;$snmpCommand&quot;;
}

print &quot;$snmpCommand&quot;;


The print $snmpCommand works fine within the else, but once out of the loop displays nothing. Is something to do with the 'my'. I used this as i wanted it to be local to this subroutine.


This my help to solve the problem.

Thanks.

Carl.

 
Yes, the my() localizes $snmpCommand to the if block. You need to declare it in the closing scope of the subroutine block instead.
Code:
sub mysub {
   my $snmpCommand; 
   ...
   if ($miboid =~ /Table$/) {
      $snmpCommand = &quot; .. &quot;;
   }
   else {
      $snmpCommand = &quot; .. &quot;;
   }
}
jaa
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top