Skip to content

Conversation

Copy link

Copilot AI commented Nov 4, 2025

The codebase contained ~88 lines of duplicated code between main_v1.6.py and main_v1.7.py, including banner display, user input prompts, and scan type selection logic.

Changes

Created nmap_utils.py - Shared module with 3 extracted functions:

  • display_banner() - pyfiglet banner and welcome message
  • get_target_info() - prompts for IP address and port/range input
  • choose_scan_type() - displays scan menu (1-12) and validates selection

Refactored main_v1.6.py - 119 → 81 lines (32% reduction)

  • Imports shared functions from nmap_utils
  • Retains simple Unix-focused implementations of check_root(), run_scan(), construct_nmap_command()

Refactored main_v1.7.py - 221 → 183 lines (17% reduction)

  • Imports shared functions from nmap_utils
  • Retains enhanced implementations with Windows support, shlex parsing, and detailed error handling

Added .gitignore - Excludes __pycache__/, virtual environments, IDE files

Before/After

# Before: Duplicated in both files
banner = colored(pyfiglet.figlet_format("Nmap Scanning Tool"), "green")
print(banner)
print(colored("\n******************** Welcome... ********************", "cyan"))

def get_target_info():
    ip_address = input("\nEnter the IP address to scan: ").strip()
    port = input("Enter the port... [Leave blank for all ports]: ").strip()
    if not port:
        port = "1-65535"
    return ip_address, port

# After: Single implementation in nmap_utils.py
from nmap_utils import display_banner, get_target_info, choose_scan_type

display_banner()

Design Notes

Each version intentionally maintains separate construct_nmap_command() implementations due to different architectural needs:

  • v1.6: Simple list construction, always includes port flag
  • v1.7: Conditional port handling, shlex parsing for custom args, port flag deduplication

Left main_v1.1.py unchanged as it's a minimal reference implementation.

Original prompt

Find and refactor duplicated code


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI and others added 2 commits November 4, 2025 20:16
Co-authored-by: SagarBiswas-MultiHAT <192402369+SagarBiswas-MultiHAT@users.noreply.github.com>
Co-authored-by: SagarBiswas-MultiHAT <192402369+SagarBiswas-MultiHAT@users.noreply.github.com>
Copilot AI changed the title [WIP] Refactor duplicated code for better maintainability Refactor: Extract duplicated code into shared nmap_utils module Nov 4, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants