How do I write a simple PERL script to scan for open ports on a target machine

0 votes
I’m learning about network security and I want to create a simple port scanner using Perl. My goal is to scan a target machine and identify open ports. I’ve worked with Net::Ping in Perl before, but I’m not sure how to implement a basic port scan to check for open TCP ports on a range of IP addresses or a specific host.

Could someone provide a basic example of how I could write such a script? I’m interested in understanding how the code works, especially when it comes to handling connection attempts and timeouts.
Oct 17, 2024 in Cyber Security & Ethical Hacking by Anupam
• 17,200 points
391 views

No answer to this question. Be the first to respond.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
0 votes

In order to create a simple PERL script that can scan for open ports on a target machine, we can use the IO::Socket::INET module.

This module helps us to connect to specific ports and check whether they're open.

Here's a bash script that scans for a range of TCP ports on a target machine:

use strict;
use warnings;
use IO::Socket::INET;

my $target = '192.168.1.1';
my $start_port = 1;
my $end_port = 1000;

for my $port ($start_port .. $end_port) {
    my $socket = IO::Socket::INET->new(
        PeerAddr => $target,
        PeerPort => $port,
        Proto    => 'tcp',
        Timeout  => 2
    );
    if ($socket) {
        print "Port $port is open\n";
        close($socket);
    }
}

This bash script will check each port within the specified range and if any connection is made within the timeout period, the port is reported as open.

answered Oct 22, 2024 by CaLLmeDaDDY
• 31,260 points

edited Mar 6

Related Questions In Cyber Security & Ethical Hacking

0 votes
1 answer

How do I use Metasploit to perform NetBIOS enumeration on a target?

It's common practice to use Metasploit for ...READ MORE

answered Nov 18, 2024 in Cyber Security & Ethical Hacking by CaLLmeDaDDY
• 31,260 points
452 views
+1 vote
1 answer
+1 vote
1 answer
0 votes
1 answer

How to detect open ports on a web server using Python?

Conducting a security audit to identify open ...READ MORE

answered Feb 18 in Cyber Security & Ethical Hacking by CaLLmeDaDDY
• 31,260 points
259 views
0 votes
1 answer

How to write a Python script for XSS vulnerability detection?

Detecting Cross-Site Scripting (XSS) vulnerabilities is crucial ...READ MORE

answered Feb 19 in Cyber Security & Ethical Hacking by CaLLmeDaDDY
• 31,260 points
344 views
0 votes
1 answer

How to write a script to check for insecure HTTP headers?

Ensuring the security of your web application ...READ MORE

answered Feb 21 in Cyber Security & Ethical Hacking by CaLLmeDaDDY
• 31,260 points
181 views
+1 vote
1 answer

How do you decrypt a ROT13 encryption on the terminal itself?

Yes, it's possible to decrypt a ROT13 ...READ MORE

answered Oct 17, 2024 in Cyber Security & Ethical Hacking by CaLLmeDaDDY
• 31,260 points
1,263 views
+1 vote
1 answer

How does the LIMIT clause in SQL queries lead to injection attacks?

The LIMIT clause in SQL can indeed ...READ MORE

answered Oct 17, 2024 in Cyber Security & Ethical Hacking by CaLLmeDaDDY
• 31,260 points
671 views
+1 vote
1 answer

Is it safe to use string concatenation for dynamic SQL queries in Python with psycopg2?

The use of string concatenation while building ...READ MORE

answered Oct 17, 2024 in Cyber Security & Ethical Hacking by CaLLmeDaDDY
• 31,260 points
501 views
+1 vote
1 answer
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP