Question

perl script to check if a port is open or closed for a given host and protocol

Asked by: CharanR

Hi,

Could you help me on this.  I was searching for this kind of script for the past 2 weeks and nobody was help me out.

I'm very new to perl scripting and that's the reason why I'm asking you guys.

I need a script which can find the blocked ports

Our company uses IBM servers and they opened only few ports for us to work on. So we use this script and request them to open the particular ports for us.

for example, if I give

portdetect.pl <hostname> <start_port> <end_port>

the script must be able to check if a port is open or closed for a given host and protocol and write this to a file.

After some research I found a command that can be used to test ports. I'm not sure whether this can be really used or not. I think it might help.

use strict;
   use warnings;
   use IO::Socket::PortState qw(check_ports);

   my %porthash = ( ... );

   check_ports($host,$timeout,\%porthash);

   for my $proto (keys %porthash) {
      for(keys %{ $porthash{$proto} }) {
         print "$proto $_ is not open ($porthash{$proto}->{$_}->{name}) if !$porthash{$proto}->{$_}->{open};
      }
   }

Thanks,
Charan

This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.

Subscribe now for full access to Experts Exchange and get

Instant Access to this Solution

  • Plus...
  • 30 Day FREE access, no risk, no obligation
  • Collaborate with the world's top tech experts
  • Unlimited access to our exclusive solution database
  • Never be left without tech help again

Subscribe Now

Asked On
2009-08-09 at 09:05:34ID24638220
Tags

perl

Topic

Perl Programming Language

Participating Experts
3
Points
500
Comments
12

Trusted by hundreds of thousands everyday for fast, accurate and reliable tech support.

  • "The time we save is the biggest benefit of Experts Exchange to Warner Bros. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange." Mike Kapnisakis, Warner Bros.
  • "Our team likes having a resource that is more secure than just using Google and most experts using this service really know their stuff. It's nice to look here first versus using Google." Dayna Sellner, Lockheed Martin
  • "Anytime that I've been stumped with a problem, 9 out of 10 times Experts Exchange has either the accepted solution or an open discussion of the potential solution to the problem." Kenny Red, eBay Inc.

See what Experts Exchange can do for you.

Got a question?

We've got the answer.

Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.

Screenshot of Experts Exchange Knowledgebase

Need individual assistance?

Our experts are ready to help.

If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.

Screenshot of Experts Exchange Knowledgebase

Want to learn from the best?

Read articles from industry experts.

Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.

Screenshot of an Article

Working on a long term project?

Store your work and research.

Save solutions to your questions, answers you’ve discovered through searching plus helpful articles in your personal knowledgebase for easy future access.

Screenshot of Experts Exchange Knowledgebase

Access the answers to your technology questions today.

Subscribe Now

30-day free trial. Register in 60 seconds.

What Makes Experts Exchange Unique?

Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Trusted by the world's most respected brands.

image of each brand's logo

Faithfully serving IT professionals since 1996.

Experts Exchange Logo

Try it out and discover for yourself.

Subscribe Now

30-day free trial. Register in 60 seconds.

Related Solutions

  1. Precompiling mod_perl?
    I have a mod_perl script which I want top protect from prying eyes, even those that might have rrot on the machine on which it is running. Is there a way to have the code procomplied and then have Apache/mod_perl load it at startup as complied code....? Is this clear?
  2. PERL IO / CGI question
    What I want this script to do is this, first, the user provides the values year month day and notes (for that day). The end goal is for this to indirectly interface with a flash calander web app. So, I want the POST from the webform to pass these to the perl script. The perl ...
  3. what is the use of qw and qq in perl
    Hi, Can anyone help me in knowing the use of qw and qq and their difference. I would appreciate if you provide me with examples. Thanks
  4. perl module
    i have a perl module names GRS.pm package GRS; use strict; require Exporter; our @ISA = qw(Exporter); our @EXPORT_OK = qw(); sub print_hello{ print "hello\n"; } 1; and a test.pl #!/usr/bin/perl -w use strict; use XML::Simple; use lib qw(C:\\Users\\Angel...

Free Tech Articles

  1. WARNING: 5 Reasons why you should NEVER fix a computer for free.
    It is in our nature to love the puzzle. We are obsessed. The lot of us. We love puzzles. We love the challenge. We thrive on finding the answer. We hate disarray. It bothers us deep in our soul. W...
  2. SCCM OSD Basic troubleshooting
    SCCM 2007 OSD is a fantastic way to deploy operating systems, however, like most things SCCM issues can sometimes be difficult to resolve due to the sheer volume of logs to sift through and the dispe...
  3. Migrate Small Business Server 2003 to Exchange 2010 and Windows 2008 R2
    This guide is intended to provide step by step instructions on how to migrate from Small Business Server 2003 to Windows 2008 R2 with Exchange 2010. For this migration to work you will need the fo...
  4. Create a Win7 Gadget
    This article shows you how to create a simple "Gadget" -- a sort of mini-application supported by Windows 7 and Vista. Gadgets can be dropped anywhere on the desktop to provide instant information, ...
  5. Outlook continually prompting for username and password
    There have been a lot of questions recently regarding Outlook prompting for a username and password whilst using Exchange 2007. There are a few reasons why this would happen and I will try to cover t...
  6. Backup Exchange 2010 Information Store using Windows Backup
    There seems to be quite a lot of confusion around the ability to backup Exchange 2010 using the built in Windows Backup feature. This stems from the omission of this feature prior to Exchange 2007 s...

Cloud Class Webinars

  1. Avoiding Bugs in Microsoft Access
    Alison Balter takes and in-depth look at avoiding bugs in Access. In this webinar you will learn about using the immediate window to debug your applications, invoking the debugger, using breakpoints to troubleshoot, stepping through code, setting the next statement to execute, ...
  2. Top 10 Best New Features in Visio 2010
    Scott Helmers gives live demonstrations of the top 10 new features in Visio 2010. This webinar will teach you how to create compelling diagrams by adding shapes to the page with a single click, linking the shapes in a diagram to data in Excel (or SQL Server, or SharePoint), ...
  3. IT Consultant Business Secrets Revealed
    Michael Munger, Experts Exchange tech pro and IT consultant, pulls back the curtain on his very successful businesses and answers question on every IT consultant and business owner should know about. He shares secrets on what he did to solve the 5 most common problems in IT, ...
  4. Disaster Recovery and Business Continuity
    Quest CTO, Mike Billon, gives an overview of the steps involved in building a dunamic disaster recovery plan. Through case studies and an examination of software/hardware tooles for monitoring and testing, you'll gain a better understandin of where you are, where you want ...
  5. Organize Your Visio Diagrams with Containers and Lists
    Scott Helmers uses cross functional flowcharts, wireframe diagrams, data graphic legends and seating charts to teach you: how to ustilize all three new structured diagram components in Visio 2010, the best practices for organizeing shapes in previous version of Visio, how to organize ...
  6. How to Us Objects, Properties, Events and Methods in Microsoft Access
    Alison Dalter gives an in-depbth look at objects, properties, events and methods in Microsoft Access. In this webinar you will learn about using the object browser, referring to objects, working with properties and methods, working with object variables, understanding the ...

Join the Community

Give a Little. Get a Lot.

Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.

Join the Community

Answers

 

by: FishMongerPosted on 2009-08-09 at 09:43:10ID: 25054769

 

by: CharanRPosted on 2009-08-09 at 09:56:51ID: 25054816

I know nothing about perl
please see these errors when executed the script you gave me in the first link.

./nmapperl.pl: line 1: use: command not found
./nmapperl.pl: line 5: my: command not found
./nmapperl.pl: line 6: syntax error near unexpected token `('
./nmapperl.pl: line 6: `  $scanner->tcp_syn_scan();'

 

by: CharanRPosted on 2009-08-09 at 09:58:30ID: 25054825

these are the errors for the second link

./nmap2.pl: line 1: use: command not found
./nmap2.pl: line 2: my: command not found
./nmap2.pl: line 4: syntax error near unexpected token `('
./nmap2.pl: line 4: `  $np->parsescan($nmap_path, $nmap_args, @ips);'

 

by: FishMongerPosted on 2009-08-09 at 10:05:29ID: 25054858

Line 1 of the Perl script is the "shebang line" and it points to the perl interpreter.

Every Perl script you write should include the strict and warnings pragma.
So, your script would start like this: (adjust the path to perl as needed).

#!/usr/bin/perl
 
use strict;
use warnings;
use Nmap::Scanner;
 
my $scanner = new Nmap::Scanner;
                                              
1:
2:
3:
4:
5:
6:
7:

Select allOpen in new window

 

by: CharanRPosted on 2009-08-09 at 10:08:43ID: 25054868

this is the new error
Can't locate Nmap/Scanner.pm in @INC (@INC contains: /usr/lib64/perl5/5.8.5/x86_64-linux-thread-multi /usr/lib/perl5/5.8.5 /usr/lib64/perl5/site_perl/5.8.5/x86_64-linux-thread-multi /usr/lib64/perl5/site_perl/5.8.4/x86_64-linux-thread-multi /usr/lib64/perl5/site_perl/5.8.3/x86_64-linux-thread-multi /usr/lib64/perl5/site_perl/5.8.2/x86_64-linux-thread-multi /usr/lib64/perl5/site_perl/5.8.1/x86_64-linux-thread-multi /usr/lib64/perl5/site_perl/5.8.0/x86_64-linux-thread-multi /usr/lib/perl5/site_perl/5.8.5 /usr/lib/perl5/site_perl/5.8.4 /usr/lib/perl5/site_perl/5.8.3 /usr/lib/perl5/site_perl/5.8.2 /usr/lib/perl5/site_perl/5.8.1 /usr/lib/perl5/site_perl/5.8.0 /usr/lib/perl5/site_perl /usr/lib64/perl5/vendor_perl/5.8.5/x86_64-linux-thread-multi /usr/lib64/perl5/vendor_perl/5.8.4/x86_64-linux-thread-multi /usr/lib64/perl5/vendor_perl/5.8.3/x86_64-linux-thread-multi /usr/lib64/perl5/vendor_perl/5.8.2/x86_64-linux-thread-multi /usr/lib64/perl5/vendor_perl/5.8.1/x86_64-linux-thread-multi /usr/lib64/perl5/vendor_perl/5.8.0/x86_64-linux-thread-multi /usr/lib/perl5/vendor_perl/5.8.5 /usr/lib/perl5/vendor_perl/5.8.4 /usr/lib/perl5/vendor_perl/5.8.3 /usr/lib/perl5/vendor_perl/5.8.2 /usr/lib/perl5/vendor_perl/5.8.1 /usr/lib/perl5/vendor_perl/5.8.0 /usr/lib/perl5/vendor_perl .) at ./nmapperl.pl line 5

 

by: CharanRPosted on 2009-08-09 at 10:14:36ID: 25054909

can't I use this script

after running this script, it gave me the following error

syntax error at ./portstate.pl line 12, near "?;"
syntax error at ./portstate.pl line 17, near "?;"
Execution of ./portstate.pl aborted due to compilation errors

#!/usr/bin/perl
 
use strict;
use warnings;
 
    sub handler {
       my($ent_hr,$host,$port,$proto,$timeout) = @_;
 
       # use $host, $port, $protocol, and $timeout to determine what you want however you like here
 
       # at a minimum do these two:
       $ent_hr->{open} = ???; # do what you like to set its open status to 1 or 0
       $ent_hr->{note} = 'my handler()';
 
       # set any other info you wanted here also...
       if(!$ent_hr->{open}) {
          $ent_hr->{closed_reason} = ???; # do what you like to set details about why its not open (blocked, not running, etc)
       }
    }
                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:

Select allOpen in new window

 

by: CharanRPosted on 2009-08-09 at 10:17:25ID: 25054925

if the aove script works then how do I edit the script to add the range of ports and the result should go to a file

 

by: CharanRPosted on 2009-08-09 at 17:38:53ID: 25056621

does this mean it cannot find the modules

Can't locate Net/Telnet.pm in @INC (@INC contains: /usr/lib64/perl5/5.8.5/x86_64-linux-thread-multi /usr/lib/perl5/5.8.5 /usr/lib64/perl5/site_perl/5.8.5/x86_64-linux-thread-multi /usr/lib64/perl5/site_perl/5.8.4/x86_64-linux-thread-multi /usr/lib64/perl5/site_perl/5.8.3/x86_64-linux-thread-multi /usr/lib64/perl5/site_perl/5.8.2/x86_64-linux-thread-multi /usr/lib64/perl5/site_perl/5.8.1/x86_64-linux-thread-multi /usr/lib64/perl5/site_perl/5.8.0/x86_64-linux-thread-multi /usr/lib/perl5/site_perl/5.8.5 /usr/lib/perl5/site_perl/5.8.4 /usr/lib/perl5/site_perl/5.8.3 /usr/lib/perl5/site_perl/5.8.2 /usr/lib/perl5/site_perl/5.8.1 /usr/lib/perl5/site_perl/5.8.0 /usr/lib/perl5/site_perl /usr/lib64/perl5/vendor_perl/5.8.5/x86_64-linux-thread-multi /usr/lib64/perl5/vendor_perl/5.8.4/x86_64-linux-thread-multi /usr/lib64/perl5/vendor_perl/5.8.3/x86_64-linux-thread-multi /usr/lib64/perl5/vendor_perl/5.8.2/x86_64-linux-thread-multi /usr/lib64/perl5/vendor_perl/5.8.1/x86_64-linux-thread-multi /usr/lib64/perl5/vendor_perl/5.8.0/x86_64-linux-thread-multi /usr/lib/perl5/vendor_perl/5.8.5 /usr/lib/perl5/vendor_perl/5.8.4 /usr/lib/perl5/vendor_perl/5.8.3 /usr/lib/perl5/vendor_perl/5.8.2 /usr/lib/perl5/vendor_perl/5.8.1 /usr/lib/perl5/vendor_perl/5.8.0 /usr/lib/perl5/vendor_perl .) at ./portscan.pl line 5.

                                              
1:

Select allOpen in new window

 

by: FishMongerPosted on 2009-08-09 at 20:16:57ID: 25056981

Yes, that's what it means.

Net::Telnet is not a core module so it needs to be installed.  Do you know how to install modules?

http://www.cpan.org/modules/INSTALL.html
http://perl.about.com/od/packagesmodules/qt/perlcpan.htm
http://www.perlmonks.org/index.pl?node_id=128077

 

by: ahoffmannPosted on 2009-08-13 at 04:25:31ID: 25087073

> I need a script which can find the blocked ports

could you please define "blocked"
do you mean "not open" (aka "closed"), or do you mean "blocked" (by firewall, IDS, the server itself), or "denied" (by firewall, IDS, the server itself)

There're so many possibilities what "blocked" could mean in reality ...

 

by: arober11Posted on 2009-08-14 at 11:50:23ID: 25101168

Hi

Do you still require a Perl varint of Tintin bash script?

http://www.experts-exchange.com/OS/Unix/Q_24618250.html

20120131-EE-VQP-002

3 Ways to Join

30-Day Free Trial

The Experts

98% positive feedback on 31,087 answers since March 2000. angeliii is a Microsoft Most Valuable Professional for his work with MS SQL Server & Develoment.

He has also proven his knowledge of Visual Basic Programming, PHP Scripting and Oracle Databases.

The Experts

97% positive feedback on 10,752 answers since July 2000. lrmoore has more than 18 years experience in the networking industry.

The six-time Mircosoft MVPs specialties include firewalls, virtual private networking, and network management.

Testimonials

"...and excellent source for support... Kind of like having your very own IT dept." Electriciansnet

Testimonials

"I was apprehensive at signing up at first. However... it has already made my life as an IT administrator much easier." JaCrews

Testimonials

"WOW! You guys have great, active, and knowledgeable people on here." moore50

Business Clients

Business Clients

In the Press

"If you’ve got a question... Experts Exchange can supply an answer.”

In the Press

"...an invaluable aid for both IT professionals and those who require tech support."

In the Press

"where IT professionals provide quick answers on just about any topic"

Business Account Plans

Loading Advertisement...