Check a port's state on a given host

checkport() { sudo nmap -sS -p $1 $2 }
Check to see if a port is open or closed on a given host.
Sample Output
$ checkport 80 commandlinefu.com

Starting Nmap 5.21 ( http://nmap.org ) at 2011-12-13 11:41 GMT
Nmap scan report for commandlinefu.com (74.207.251.129)
Host is up (0.17s latency).
rDNS record for 74.207.251.129: li93-129.members.linode.com
PORT   STATE SERVICE
80/tcp open  http

Nmap done: 1 IP address (1 host up) scanned in 0.71 seconds

0
2011-12-13 11:46:15

These Might Interest You

  • Try to perform a fully TCP 3 way handshake on for a given host-port with a timeout of 1s. Show Sample Output


    6
    nc -zvw 1 host port
    akhilravidas · 2012-07-13 20:02:17 0
  • Shell function; returns 0 if the port is up, 1 otherwise (check $? after executing). First parameter: IP address/hostname Second parameter: port number There is no error checking for the input parameters.


    1
    cpo(){ [[ $# -lt 2 ]] && echo 'need IP and port' && return 2; [[ `wget -q "http://dnstools.com/?count=3&checkp=on&portNum=$2&target=$1&submit=Go\!" -O - |grep -ic "Connected successfully to port $2"` -gt 0 ]] && return 0 || return 1; }
    marek158 · 2011-09-26 12:11:51 1
  • host B (you) redirects a modem port (62220) to his local ssh. host A is a remote machine (the ones that issues the ssh cmd). once connected port 5497 is in listening mode on host B. host B just do a ssh 127.0.0.1 -p 5497 -l user and reaches the remote host'ssh. This can be used also for vnc and so on.


    9
    ssh -R 5497:127.0.0.1:22 -p 62220 user@public.ip
    pescio · 2010-12-29 18:44:59 0
  • This command telnet and and looks for a line starting with "SSH" - works for OpenSSH since the SSH banner is something like "SSH-2.0-OpenSSH_6.0p1 Debian-4+deb7u3". Then it triggers an action accordingly. It can be packed as a script file to echo 0/1 indicating the SSH service availability: if [[ "$(sleep 1 | telnet -c <host> <port> 2>&1 | grep '^SSH')" == SSH* ]]; then echo 1; else echo 0; fi; Alternative uses: Trigger an action when server is UP (using &&): [[ "$(sleep 1 | telnet -c <host> <port> 2>&1 | grep '^SSH')" == SSH* ]] && <command when up> Trigger an action when server is DOWN (using ||): [[ "$(sleep 1 | telnet -c <host> <port> 2>&1 | grep '^SSH')" == SSH* ]] || <command when down>


    0
    $if [[ "$(sleep 1 | telnet -c <host> <port> 2>&1 | grep '^SSH')" == SSH* ]]; then <command when up>; else <command when down>; fi;
    paulera · 2016-02-02 13:06:51 2

What Others Think

The voting system on here sucks! Really wish people would backup their negative voting with a short comment as to why.
peterRepeater · 338 weeks and 4 days ago

What do you think?

Any thoughts on this command? Does it work on your machine? Can you do the same thing with only 14 characters?

You must be signed in to comment.

What's this?

commandlinefu.com is the place to record those command-line gems that you return to again and again. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.

Share Your Commands



Stay in the loop…

Follow the Tweets.

Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.

» http://twitter.com/commandlinefu
» http://twitter.com/commandlinefu3
» http://twitter.com/commandlinefu10

Subscribe to the feeds.

Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):

Subscribe to the feed for: