简体   繁体   English

使用Perl检查仅开放端口

[英]Check for only open port with perl

I need to write a script to scan ports on server and generate a report. 我需要编写脚本来扫描服务器上的端口并生成报告。 This script should: 该脚本应:

Read a list of IPs from a file; 从文件中读取IP列表; Scan each IP, and write a file with the results. 扫描每个IP,并写入包含结果的文件。

I am using below script for this:: 我为此使用以下脚本::

#!/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;

Now I have 1 thing to do with is:: 现在我要做的一件事是:

Scan for all open ports. 扫描所有打开的端口。

Currently it is scanning ports %port_hash but I need to scan all the ports and list ports which are open. 当前正在扫描端口%port_hash,但是我需要扫描所有端口并列出已打开的端口。 How to do this? 这个怎么做?

This will fill up %porthash with all the ports: 这将用所有端口填充%porthash:

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

Then you can call check_ports with this %port_hash. 然后,您可以使用此%port_hash调用check_ports

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM