开发者

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.x开发者_如何学JAVAxx.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')
}


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜