Check I can connect to internet services
I often want to check can I connect to a service running on different machines. There are a number of ways to do this. I typically default to using telnet. This isn't ideal and overkill when I just want to check a port is open (e.g. no firewall blocking the port). I decided today to look around for alternatives and have come up with the following script:
https://raw.githubusercontent.com/oisinmulvihill/my-bash-helpers/master/lib/tcptest.sh
 This uses netcat if available or bash socket connections if not. You can use the bash function as follows:
# Download and source the script. eval "$(curl https://raw.githubusercontent.com/oisinmulvihill/my-bash-helpers/master/lib/tcptest.sh)" # Connections over HTTP $ tcptest google.com 80 Connection ok to 'google.com:80' # Connections over HTTPS $ tcptest google.com 443 Connection ok to 'google.com:443'
No try a port google won't have open:
$ tcptest google.com 38475 No one is listening on 'google.com:38475'
This is really only a quick check as the connection could be slow. The tcptest will timeout after 5 seconds. The tcptest function is based on a mashup of three different stackoverflow answers:
http://stackoverflow.com/questions/4922943/how-to-test-if-remote-tcp-port-is-opened-from-shell-script
http://stackoverflow.com/questions/9609130/quick-way-to-find-if-a-port-is-open-on-linux
http://www.catonmat.net/blog/tcp-port-scanner-in-bash/
© Copyright 2014 by Oisin Mulvihill. Content licensed under the Creative Commons attribution-noncommercial-sharealike License.











