Solved

Perl - Problem writing script to get status of open ports.

Posted on 2008-10-01
2
688 Views
Last Modified: 2013-11-05
Hi,
Some background - I do a lot of vulnerability scanning, and as part of the remediation, when the server admins which reports they have closed, I usually have to manually 'telnet <ip_address> <port>' to see if the port has been closed. Most of these are prod servers, so I cannot just rescan them, but would need to go through a much longer process.

To get around this, I am trying to write a perl script that will do this for me. Basically, I read in the address and port info from an Excel report, and then have tried various methods to try to duplicate the manual telnet process to determine if a port is open or not.

I have tried Net::Telnet, IO::Socket::INET, and a few others that are basically versions of these. I have found that neither produce consistent results, which most often means it's a "me" problem. If someone could point out how I could make this work, or propose an alternate method, I would greatly appreciate it.

I have included a couple snippets to show what I have been trying.

Thanks,
Jeff
.....

use Net::Telnet();

...........

...........

foreach (@hosts)

{

    my $host = $_;

    $telnet = new Net::Telnet();

    $telnet->open(Host => $host,

                  Port => $port);

    $telnet->errmode('return');

    if (($telnet->errmsg eq "") || ( "$telnet->errmsg\n" =~ /^Net::Telnet=GLOB\(0x([0-9a-fA-F]+)\)->errmsg$/ ))

    {

        printf "%s: %s\n", $host, ${\$telnet->errmsg};       #"open";

    }

    else

    {

        printf "%s: %s\n", $host, ${\$telnet->errmsg};

    }

    $telnet->close;

}

***********************************************************************
 

************************************************************************

....

use IO::Socket::INET;

......

.....

            foreach (@hosts)

            {

              my $host = $_;

              $socket = new IO::Socket::INET (

                                              PeerAddr => $host,

                                              PeerPort => $port

                                              );

              if ($socket)

              {

                printf ("%s:%s -- %s\n", $host, $port, "open");

              }

              else

              {

                 printf ("%s:%s -- %s\n", $host, $port, "closed");               

              }

              if ($socket)

              {

                close($socket);

              }

            }

Open in new window

0
Comment
Question by:jpetter
2 Comments
 
LVL 39

Accepted Solution

by:
Adam314 earned 500 total points
Comment Utility
I think for this (just testing if a port is open), the IO::Socket::INET module is probably what you want, not the Net::Telnet module.

What is wrong with the code you are using?  It should work just fine.  You might want to also use the Timeout option.  Also, if you care about the protocol, should specify the Proto option, otherwise IO::Socket::INET will make a guess based on the port.  
0
 

Author Comment

by:jpetter
Comment Utility
Adam314,

Thanks again for the speedy response. I've added the proto and timeout options, and it seems to be working as expected now....and using the IO::Socket::INET as you recommended.

Thanks,
Jeff
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now