Port Scanner Script Malfunctioning

I'm feeling quite perplexed as to why my script is not functioning properly.

This particular script is designed to search for servers with port 19 open (CHARGEN).

You need to input a list of IPs in the following format:

1.1.1.1
2.2.2.2
3.3.3.3
4.4.4.4
5.5.5.5

Once the list is provided, the script scans each IP to determine if port 19 is open. If it finds an open port, it saves the IP to a file.

Below is the code snippet:

#!/usr/bin/env python

#CHARGEN Scanner
#Created by Expedient

import sys
import Queue
import socket
import threading

queue = Queue.Queue()

def check_ip(host, output_file, timeout):
    try:
        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        sock.settimeout(timeout)
        result = sock.connect_ex((host, 19))
        if result == 0:
            print "Found: %s" % host
            file = open(output_file, "a")
            file.write(host+"\n")
            file.close()
    except:
        pass

def add_to_queue(queue, host, output_file, timeout):
    queue.put(check_ip(host, output_file, timeout))

if len(sys.argv) < 4:
    print "Usage: %s <ip list> <output file> <timeout>" % sys.argv[0]
    sys.exit()

try:
    open(sys.argv[1])
except:
    print "Unable to open ip list."
    sys.exit()

print "Initiating Expedient's CHARGEN Scanner..."

with open(sys.argv[1]) as ip_list:
    for ip in ip_list:
        thread = threading.Thread(target=add_to_queue, args=(queue, ip, sys.argv[2], float(sys.argv[3])))
        thread.start()

Despite running the script on a list of servers obtained from nmap scan

(ensuring all have port 19 open), none of the IPs are written to the output file.

The expected outcome should be that every IP in the list with port 19 open gets saved, but this isn't happening.

I am at a loss regarding what might be causing this issue and would greatly appreciate any assistance or guidance on where I might be going wrong. Thank you.

Answer №1

The code snippet you provided is currently catching all exceptions in the check_ip function without providing any specific feedback (except: pass). This approach can lead to difficulties in pinpointing the root cause of any potential issues that may be triggering exceptions within the function. If an exception occurs in every instance of calling the function, your script will not yield any results and there will be no information logged or displayed on the nature of the failure.

For effective debugging purposes, it is recommended to update your exception handling mechanism to explicitly address specific exceptions that should be bypassed, while allowing other exceptions to remain unhandled. By doing so, you will be able to identify and evaluate your error conditions more effectively.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the best way to create a Python test case for a function with numerous print statements being executed?

I recently decided to dive into writing my own unit test cases. I successfully created tests for basic integer addition and subtraction functions, but now I wanted to tackle something more complex like a Password Checker. Here is the code snippet I've ...

Having trouble extracting data from Moz Bar through selenium using Python?

Having trouble retrieving the spam score from Moz bar using Selenium WebDriver? I've attempted various methods such as XPath, class, and tag name without success. Any assistance would be greatly appreciated: from selenium.webdriver.common.by import By ...

Obtain the name of the month from the given date input

Hey there! I've been working on a function that allows users to input their birthdate and receive relevant information, like the month name corresponding to the date they entered. However, I'm currently only getting the month number instead of th ...

Splinter: Extracting XPATH text fragments that do not comprise distinct elements

How can I extract and store the text of the first, underlined, and last parts of the question using Splinter? Refer to the HTML below. I aim to assign the following values to variables: first_part = "Jingle bells, jingle bells, jingle all the" second_par ...

Having trouble with the current Python system and wanting to set up Anaconda for a fresh Python experience - stepping into the world

Starting out in the world of programming and Python installation, I am in search of a solution to an installation issue that doesn't require much coding. This hurdle is preventing me from utilizing Python and obtaining tools like spyder and pandas tha ...

Interactive email with a dynamic dropdown feature

I am currently working on creating an HTML email that can display data depending on the selection made from a dropdown menu within the email itself. Despite my research, I have not come across a suitable solution for this specific scenario. My current app ...

Python Selenium - Having trouble navigating between pages

I am facing two issues with the script I want to use for scraping data and appending it to a csv file: 1) The script only scrapes items from a single page (the last page = 64) instead of crawling through all pages from 1 to 64. 2) When writing data to th ...

Encountering Error 405 with Django-Sentry while attempting to transfer error data to localhost on port 9000 at /store

My experience with Django-sentry has been challenging. I am attempting to send errors to localhost:9000/store but facing issues. Here is the error message: jamis$ python manage.py runserver Validating models... 0 errors found Django version 1.3.1, using ...

Having trouble locating the XPath for a button within a frame

My attempts to access the following website were unsuccessful: https://www.google.com/recaptcha/api2/demo I tried clicking on this button: https://i.stack.imgur.com/0q229.png Then, I attempted to click on this button: https://i.stack.imgur.com/LvTjG.pn ...

Open the JSON file on Amazon Alexa

Currently, I am in the process of developing an app that allows users to search for flight connections using Amazon Alexa. My main challenge lies in loading a json file containing airport information into a variable to retrieve data from it at a later stag ...

What is the best way to fetch the path of a module when I possess a class from within that module?

When working with Python, if you have a class named foo, you can use the foo.__module__ method to retrieve a string containing the name of the module it belongs to. Similarly, if you have a module called bar, you can use the bar.__file__ method to obtain ...

Exploring Pytorch's Layer Iteration Technique

Imagine I have an object in my code called m which represents a network model. Without knowing the specific details of how many layers this network contains, how can I create a for loop to cycle through each layer? I am aiming for a structure similar to ...

Pytesseract failing to recognize numbers in an image

Is it possible to utilize pytesseract in order to analyze the image depicted below? Although, it seems that it is categorizing it as "T". Check out the code snippet provided: txt = pytesseract.image_to_string(image, config='-psm 10') ...

Expanding the Python CSV File Header

I need help finding a way to expand the header of a CSV file. I have the existing file header stored in a dictionary called dict1, and another dictionary named dict2 with keys that overlap with some in dict1. My goal is to add these overlapping keys to th ...

Waiting for a tweet to load before scraping a website using BeautifulSoup, Python, and Selenium

I am currently facing a challenge in scraping a website to extract tweet links, specifically from DW. The issue I'm encountering is that the tweets do not load immediately, causing the request to execute before the page has fully loaded. Despite tryin ...

Need to transform a column within a Pyspark dataframe that consists of arrays of dictionaries so that each key from the dictionaries becomes its own

I currently have a dataset structured like this: +-------+-------+-------+-------+ | Index |values_in_dicts | +-------+-------+-------+-------+ | 1 |[{"a":4, "b":5}, | | |{"a":7, "b":9}] | +----- ...

Failed to find the element using Selenium

Can anyone help me find the username input section on the following website: ? I've included the code snippet I used below: from selenium import webdriver from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import ...

Error in decoding JSON while creating an index in Azure Search

I'm currently utilizing the django framework and attempting to set up an index in the Azure portal through their REST API guide. However, upon sending my post request, I encountered the following error: JSONDecodeError at /createIndex Here is the m ...

Error message: TypeError - The object provided must be an instance or subtype of the specified type in the super() function

After creating a python code with the following two classes: import torch import torch.nn as nn import torch.nn.functional as F class QNet_baseline(nn.Module): """ A simple MLP with 2 hidden layers observation_dim (int): number of o ...

Rotate images every 5 seconds, pulling them directly from the server

I am looking to update the users every seven seconds on my website. To do this, I need to retrieve all user information from the server using JavaScript. What is the most efficient way to accomplish this task? My initial thought is to send an HTTP request ...