Nmap Development mailing list archives

Re: Nmap & Bash Auto-Completion (was: Am I Missing Something?)


From: David Fifield <david () bamsoftware com>
Date: Mon, 23 Nov 2009 22:24:20 -0700

On Mon, Nov 23, 2009 at 07:10:32PM -0500, Patrick Donnelly wrote:
On Mon, Nov 23, 2009 at 6:53 PM, Ron <ron () skullsecurity net> wrote:
A couple ideas, though:
- Have bash autocomplete for script names (not sure how that works, but

This is a great idea I think. How hard would it be to do this for all
of Nmap's options in addition to script names?

The documentation for Bash completion is here:

http://www.gnu.org/software/bash/manual/html_node/Programmable-Completion.html
http://www.gnu.org/software/bash/manual/html_node/Programmable-Completion-Builtins.html

Using chgrp from Debian's /etc/bash_completion as an example, I made a
completion example that knows some option names and how to look up
scripts.

_nmap()
{
        local cur prev

        COMPREPLY=()
        cur=$(COMP_WORDS[COMP_CWORD])
        prev=${COMP_WORDS[COMP_CWORD-1])

        if [[ "$cur" == -* ]]; then
                COMPREPLY=( $(compgen -W '--script --script-args \
                --script-updatedb --traceroute \
                -sS -sP -sA -sM -sW -sV -O' -- $cur) )
                return 0
        fi

        if [ "$prev" == "--script" ]; then
                COMPREPLY=( $(cd /usr/share/nmap/scripts && compgen -f -- $cur) )
                return 0
        fi

        return 0
}
complete -F _nmap nmap

Save the above to a file and then source it like this:
        . ./nmap_comp

For example,
$ nmap -<tab><tab>
-O                 --script-args      -sP                -sW
-sA                --script-updatedb  -sS                --traceroute
--script           -sM                -sV
$ nmap --<tab><tab>
--script           --script-args      --script-updatedb  --traceroute
$ nmap --script ssh<tab><tab>
ssh-hostkey.nse  sshv1.nse

It would be neat if someone takes this idea further.

David Fifield
_______________________________________________
Sent through the nmap-dev mailing list
http://cgi.insecure.org/mailman/listinfo/nmap-dev
Archived at http://seclists.org/nmap-dev/


Current thread: