Python3 Nmap-APİ

90 views Asked by At

Nmap API is written in Python and -v, -o, -A, --script, -Pn will run these parameters and ask for IP addresses from the user and will transfer the outputs to the "targets.txt" file with the -IL parameter. can you write the code?

I used nmap3 but I could not run the codes with their parameters.

1

There are 1 answers

0
Muhammad Bilal On BEST ANSWER

Make sure to install the python-nmap library if you haven't already by running:

pip install python-nmap

Now, you can use the following Python code:

 import nmap

def run_nmap_scan(ip_address):
    # Create an Nmap PortScanner object
    nm = nmap.PortScanner()

    # Define Nmap scan parameters
    scan_arguments = "-v -oN - -A --script=default -Pn"

    # Perform the Nmap scan
    nm.scan(ip_address, arguments=scan_arguments)

    # Get the scan results
    scan_results = nm.csv()

    # Save the scan results to "targets.txt"
    with open("targets.txt", "w") as file:
        file.write(scan_results)

    print(f"Scan results saved to targets.txt for {ip_address}")

if __name__ == "__main__":
    # Prompt the user for an IP address
    target_ip = input("Enter the target IP address: ")

    # Run the Nmap scan with the specified parameters
    run_nmap_scan(target_ip)

This code defines a function run_nmap_scan that takes an IP address as an argument, runs an Nmap scan with the specified parameters, and saves the results to "targets.txt." The main block prompts the user for an IP address and then calls the run_nmap_scan function with the provided IP address.

Note: Adjust the scan_arguments variable to include the parameters you need for your specific use case. The provided example includes some common parameters such as -v, -oN, -A, --script, and -Pn.