DopeBP
DopeBP

Reputation: 99

How to start a process in perl with with a command-line argument?

Is there a way to initiate an nmap scan based off a symbol? I want to be able to run a nmap scan
when i execute my code at the terminal something like this: script.pl -b xx.xxx.xxx. I tried using an if statement to do this but it's working. Does anyone know hpw to do this properly?

if("-b")
{
('nmap -v -r ARGS[0] >>file.txt')
}

Upvotes: 1

Views: 167

Answers (2)

wollw
wollw

Reputation: 414

If you're using a single letter as a flag Getopt::Std is all you need.

use Getopt::Std;

my %opt;
getopt("b",\%opt);

system "nmap -v -r $opt{b} >> file.txt" if exists $opt{b};

getopt takes a string of the letters used as flags and maps them and their values to a hash table. getopt("ab",%opt) would set %opt to something like ( a => "foo", b => "bar" ) provided a and b are both actually used when the script is called.

Upvotes: 1

Corey Henderson
Corey Henderson

Reputation: 7455

Command line option parsing is made easy by using perl's Getopt::Long module. Here is an example:

use Getopt::Long;

my ($b);

my $result = GetOptions ("b=s" => \$b);

if ($b) {
    system ('nmap -v -r ' . $b . ' >>file.txt');
}

the b=s inside GetOptions gets a string argument from -b, and sets it to the $b variable.

Upvotes: 5

Related Questions