Dino Geek, try to help you

How to run NMAP from Python?


NMAP (Network Mapper) is an open source tool for network exploration and security auditing. It can discover hosts and services on a computer network, thus building a “map” of the network.

When it comes to running NMAP from Python, there is a third-party library available that enables you to do just that – python-nmap. python-nmap is a Python library which enables NMAP script execution and parsing its output programmatically from Python (Github, python-nmap: https://github.com/savon-noir/python-nmap).

Let’s dive in on how you can install and use this library to run NMAP from Python. The first thing you need to do is to install python-nmap. You can do this via pip, which is a package manager for Python. Open your terminal and type in the following command: `pip install python-nmap`.

After successfully installing python-nmap, you can import it into your Python script and use it to run NMAP. Here’s a basic example:

```
import nmap

nm = nmap.PortScanner()

  1. To scan the TCP ports of a single host
    nm.scan(‘127.0.0.1’, ’22-443’)
    nm.command_line() # get command line used for the scan
    nm.scaninfo() # get information about the scan
    nm.all_hosts() # get all hosts that were scanned
    nm[‘127.0.0.1’].state() # get state of host (up)
    nm[‘127.0.0.1’].all_protocols() # get all scanned protocols [‘tcp’]
    nm[‘127.0.0.1’][‘tcp’].keys() # get all ports for tcp protocol
    nm[‘127.0.0.1’].has_tcp(22) # has tcp on port 22
    nm.csv() # get result as CSV
    ```

In the above example, we first import the nmap module, then create a new PortScanner object. After that, we call the scan method on this object, passing the host and the range of ports we want to scan as arguments (The Python Standard Library, nmap – Port scanning: https://docs.python.org/3/library/nmap.html).

You can even leverage NMAP’s scripting feature right from python-nmap. For example, you can execute the “banner” script to grab the banner information of a service:

```
import nmap
nm = nmap.PortScanner()
nm.scan(‘127.0.0.1’, arguments=’-p 22 —script banner’)
```

In this way, you can integrate and leverage the full power of NMAP in your Python programs. A word of caution: as with NMAP, you should only use python-nmap responsibly and on networks that you have permission to scan. Unauthorized scanning is illegal and can get you into legal trouble. (NMAP, Legal Issues: https://nmap.org/book/legal-issues.html).


Simply generate articles to optimize your SEO
Simply generate articles to optimize your SEO





DinoGeek offers simple articles on complex technologies

Would you like to be quoted in this article? It's very simple, contact us at dino@eiki.fr

CSS | NodeJS | DNS | DMARC | MAPI | NNTP | htaccess | PHP | HTTPS | Drupal | WEB3 | LLM | Wordpress | TLD | Domain name | IMAP | TCP | NFT | MariaDB | FTP | Zigbee | NMAP | SNMP | SEO | E-Mail | LXC | HTTP | MangoDB | SFTP | RAG | SSH | HTML | ChatGPT API | OSPF | JavaScript | Docker | OpenVZ | ChatGPT | VPS | ZIMBRA | SPF | UDP | Joomla | IPV6 | BGP | Django | Reactjs | DKIM | VMWare | RSYNC | Python | TFTP | Webdav | FAAS | Apache | IPV4 | LDAP | POP3 | SMTP

| Whispers of love (API) | Déclaration d'Amour |






Legal Notice / General Conditions of Use