Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Is there an nmap line that will auto-detect my current IP address and subnet mask, and run a ping-scan on all? For example:

#> nmap -sP

Instead of manually:

#> nmap -sP
share|improve this question
add comment

2 Answers

up vote 3 down vote accepted

I don't think that there is a way with doing that with nmap alone, but you could script it: Here is a quick and dirty solution:


IP_AND_MASK=`ifconfig | grep "inet addr" | head -n1 | sed 's|.*addr:\([0-9\.]*\).*Mask:\([0-9\.]*\)|\1/\2|g'`
NETWORK=`ipcalc "$IP_AND_MASK" | grep "Network:" | sed 's|^Network:\s*\([0-9/\.]*\).*|\1|g'`
nmap -sP "$NETWORK"

You have to install ipcalc to make that solution work.


share|improve this answer
add comment

I have created a function for this (.bashrc)

#Quick nmap tcp/udp scan (quite agressive / will trigger IDS and adaptative firewall) / oN ouput allows you to resume an incomplete scan
function qnmap
    echo -e "qnmap accepts up to two arguments supplied in the command lines :\t\te.g: $ qnmap -sU -vv"
    ipinfo 1>/dev/null
    output=`date | awk {'print $3"_"$2"_"$6'}`
    cmd="sudo nmap -n -T4 -sV -sS -oN $HOME/Desktop/$output.nmapScan --exclude $myip $cidr $1 $2"
    echo -e "\$ $cmd"; $cmd

ipinfo script (I placed it in /usr/bin)

i=${1-en1}; #Specify your interface
myip=`ifconfig $i | grep "inet " | awk 'NR==1 {print $2}'`
gw=`netstat -rn | grep 'default' | awk 'NR==1 {print $2}'`
bct=`ifconfig $i | awk '/broadcast[ ]/{print $6}'`
cidr=$(while read y; do echo ${y%.*}".0/$(m=0; while read -n 1 x && [ $x = f ]; do m=$[m+4]; done < <(ifconfig $i | awk '/mask/{$4=substr($4,3); print $4}'); echo $m )"; done < <(ifconfig $i | awk '/inet[ ]/{print $2}'))
if [ -n "$myip" ]; then
    echo -e "Interface en1 (WiFi)"
    echo -e "IP Address\t\$myip = $myip"
    echo -e "Gateway\t\t\$gw = $gw"
    echo -e "Broadcast\t\$bct = $bct"
    echo -e "Network\t\t\$cidr = $cidr"
    myip=${myip:?"Cannot fetch IP for interface $i"};
share|improve this answer
add comment

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.