2012-12-06 66 views
0

我需要编写脚本来扫描服务器上的端口并生成报告。该脚本应该:用perl检查打开的端口

从文件中读取IP列表; 扫描每个IP,然后写入一个包含结果的文件。

我使用下面的脚本这个::

#!/usr/bin/perl -w 
use strict; 
use IO::Socket::PortState qw(check_ports); 

my $hostfile = 'hosts.txt'; 

my %port_hash = (
     tcp => { 
      22  => {}, 
      443  => {}, 
      80  => {}, 
      53  => {}, 
      30032 => {}, 
      13720 => {}, 
      13782 => {}, 
      } 
     ); 

my $timeout = 5; 

open HOSTS, '<', $hostfile or die "Cannot open $hostfile:$!\n"; 

while (my $host = <HOSTS>) { 
    chomp($host); 
    my $host_hr = check_ports($host,$timeout,\%port_hash); 
    print "Host - $host\n"; 
    for my $port (sort {$a <=> $b} keys %{$host_hr->{tcp}}) { 
     my $yesno = $host_hr->{tcp}{$port}{open} ? "yes" : "no"; 
     print "$port - $yesno\n"; 
    } 
    print "\n"; 
} 

close HOSTS; 

现在我有一样东西做是::

扫描所有开放端口。

目前它正在扫描端口%port_hash但我需要扫描所有打开的端口和列表端口。这个怎么做?

+1

[需要编写perl脚本以扫描服务器上的开放端口并创建报告](http://stackoverflow.com/questions/13724801/need-to-write-a-perl-script-to -scan-open-ports-on-servers-and-create-a-report) –

+0

@sudo_O由于这个问题不是真正的问题,所以这个问题被封闭了。这看起来更好,它有代码要讨论。 – Barmar

+4

正如您在其他问题中提到的那样,为什么不使用'nmap'? – Barmar

回答

0

这将填补%porthash所有的端口:

my %port_hash = (tcp => {}); 
for my $port (1 .. 65535) { 
    $port_hash{'tcp'}{$port} = {}; 
} 

然后就可以调用check_ports与此%port_hash。

+0

查看更新的答案。 – Barmar

+0

我在$ hostfile ='hosts.txt'后写上面的代码;并显示以下错误:: “我的”变量$ hostfile在scanner.pl第21行的同一范围内屏蔽了前面的声明。 “my”变量$ hostfile在scanner.pl第21行的同一语句中掩盖了更早的声明。 语法错误在scanner.pl第16行,在“%port_hash {” 附近无法使用全局$!在“my”的scanner.pl第21行,在“$ hostfile:$!”附近 由于编译错误,scanner.pl的执行中止。 – ernitinjain

+0

修复了我的语法错误。 – Barmar