Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Add Perl Varibles in system()

by ahjohnston25 (Initiate)
on May 11, 2009 at 20:12 UTC ( [id://763333]=perlquestion: print w/replies, xml ) Need Help??

ahjohnston25 has asked for the wisdom of the Perl Monks concerning the following question:

Hello- I am working for a company as basically a network admin. Occasionally, I like to pentest the systems to make sure everything is in working order. Being the lazy person that I am, I would like to create a script that does most of the work for me. So I wanted to use a command line program known as Nmap in Perl, so I use system(). The only problem is I want it to be flexible so I don't have to edit the source each time I use it. I created a varible named $target and tried to use it in the follwing way (This is a very small excerpt):
#!usr/bin/perl -w use strict; my ($target); #A few lines later. . . system( nmap $target);
What I want to know Is how I can rephrase that section of code so I can add my Perl varible $target into system(). How do I do this?

Replies are listed 'Best First'.
Re: Add Perl Varibles in system()
by ikegami (Patriarch) on May 11, 2009 at 20:21 UTC
    If $target is an entire argument, the best method would be to use the multi-argument form of system.
    system('nmap', $target);

    Using the this method, the shell will be bypassed so you don't have to worry about converting the value of $target into a shell literal.

Re: Add Perl Varibles in system()
by almut (Canon) on May 11, 2009 at 20:22 UTC
    system( nmap $target);

    Not exactly sure if that's what you're asking, but system() expects either a single string as the command to run

    system("nmap $target");

    or a list of values

    system("nmap", $target);
Re: Add Perl Varibles in system()
by John M. Dlugosz (Monsignor) on May 11, 2009 at 21:33 UTC
    You probably meant
    system (qq(nmap "$target"));
    rather than using the "indirect object" form.

    Also, your line my ($target); should be my $target;. Don't add extra parens because they are pretty to you—that will bite you later because that is not the actual syntax. You are specifying list context there. Normally when list context is needed you might say (my $s)=func_returning_list();, with the my on the inside. Note that you would not say my ($x,$y) as that is actually a syntax error.

    —John

      Normally when list context is needed you might say (my $s)=func_returning_list();, with the my on the inside
      my ($s) causes list context too.
      Note that you would not say my ($x,$y) as that is actually a syntax error.
      That works fine here on perl 5.10.0 and on any earlier perl I can remember

      $ perl -Mstrict -cwe 'my ($x,$y)' -e syntax OK
        I must be getting old. I distinctly remember learning that lesson when Perl 5 was freshly minted. The grammar must have been "improved" since then. You are right about the latter for sure; how many times do I say
        my ($x,$y,$z)= @_; </code? So the <code>my ($x)
        vs (my $x) must be (or have been) something more subtle.
        I verified that it works as far back as 5.6.0, but I'm sure it worked even before that.

      Normally when list context is needed you might say (my $s)=func_returning_list();, with the my on the inside.

      Based on all the posts I've seen here, that's actually much rarer than my ($s). In fact, I rarely see (my $s). It's just too wordy. Compare

      (my $x, my $y, my $z) = @_;
      with
      my ($x, $y, $z) = @_;

      I was going to count the nodes with "(my" and count those with "my (", but I don't have a handy way of avoiding open(my $fh, ...). Despite that common construct seriously throwing off the count, "my (" still appears more often.

      And of course, your code is subject to the shell equivalent of Bobby Tables.

        Thank you for your quick response. And the reason why  $target was written the way it was is because it was orginally going to be mutiple varibles, and I forgot to remove the paraentheses when I took ou the other varible. But now I have a different problem I want to the success of the program. This is what I believe will work:
        system( 'nmap', $ver, $os, $target ' -oG pentest.txt')=0 || print "Nma +p failed! $!"; exit1;
        But there is probably a better way to write that. Any suggestions?

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://763333]
Approved by ikegami
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (8)
As of 2024-04-23 10:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found