Nmap check if port is open in bash
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.
-
[[
expression]]
requires spaces inside the brackets. See Brackets in if condition: why am I getting syntax errors without whitespace? -
|
is the pipe operator between commands. It isn't an operator in conditional expressions. In fact[[ foo | bar ]]
is parsed as the command[[ foo
piped into the commandbar ]]
, which doesn't do anything useful. - The
-eq
operator compares integers, but what you put around it aren't integers.
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
Related videos on Youtube
![Peter Savnik](https://lh4.googleusercontent.com/-Ojbg_01n34A/AAAAAAAAAAI/AAAAAAAAADk/PWPSFWdVnFU/photo.jpg?sz=256)
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, 2022Comments
-
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 almost 7 yearswhen i run the nc -z router.eu.thethings.network 1700 it does not return. Will it behave different in a script?
-
Paranoid almost 7 yearsThis 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--- almost 7 yearsAdd the
-w1
option tonc
to try the connection for one second. This will let you get a quick failure if the host is not up. -
Peter Savnik almost 7 yearsGreat explanation and very good reading!