Nmap check if port is open in bash

11,145

Solution 1

The problem is that [[$NMAP | grep "Host is up" -ne ""]] is very far from valid bash syntax. The error messages don't tell you exactly how to fix it, but they are a hint that something is seriously wrong.

To test whether a string contains a substring, you can either use the == operator in a conditional expression, or a pipe through grep (which doesn't involve a conditional expression). With grep, you're not running $NMAP as a command, you want to pass this as input to grep, so you need to echo it into the pipe. Pass -q to grep since you only care about its return status, not about its output.

if echo "$NMAP" | grep "Host is up"  && echo "$NMAP" | grep "closed"; then …

With a conditional expression:

if [[ $NMAP == *"Host is up"* || $NMAP == *"closed"* ]]; then …

Do read Confused about operators [[ vs [ vs ( vs (( which explains about conditional expressions and how they aren't the only way to test whether a condition is true. Also, read Why does my shell script choke on whitespace or other special characters?

Solution 2

nmap -p 1700 router.eu.thethings.network

Will print "Host is up" if the hosts network is up and there is nothing listening on the port. Use nc instead:

if nc -w1 -z router.eu.thethings.network 1700; then
  echo "Port is listening"
else
  echo "Port is not listening"
fi
Share:
11,145

Related videos on Youtube

Peter Savnik
Author by

Peter Savnik

MSc Electrical Engineering with specialization in Automation and Control Design. Working part-time as Hardware/Software developer and part-time as PM/SME Consultant on implementation of Pharmaceutical Serialization.

Updated on September 18, 2022

Comments

  • Peter Savnik
    Peter Savnik almost 2 years

    I want to make a script which will check if a port is open on a server. If not open stay in a while. If open continue. The break conditions is use are if "host is up" is present and not "closed". I assume conenction is ok. The problem is that the grep is not working as expected.

    I have tried with following:

    while true; do
     NMAP=$(nmap -p 1700 router.eu.thethings.network)
     if [[$NMAP | grep "Host is up" -ne ""]] && [[$NMAP | grep "closed" -eq ""]]; then
        echo "connection!!!"
        break
     fi
    
    
     echo "waiting for connectiong"
     done
    

    I run it on a raspberry pi jessie system.

  • Peter Savnik
    Peter Savnik almost 7 years
    when i run the nc -z router.eu.thethings.network 1700 it does not return. Will it behave different in a script?
  • Paranoid
    Paranoid almost 7 years
    This is due to the way tcp/ip works. If it gets no response then it will resend and eventually timeout after about a minute. You could try a simple ping test before testing the port.
  • 111---
    111--- almost 7 years
    Add the -w1 option to nc to try the connection for one second. This will let you get a quick failure if the host is not up.
  • Peter Savnik
    Peter Savnik almost 7 years
    Great explanation and very good reading!