{"id":1113,"date":"2020-07-15T14:34:28","date_gmt":"2020-07-15T14:34:28","guid":{"rendered":"https:\/\/www.nicktailor.com\/?p=1113"},"modified":"2022-10-21T11:23:41","modified_gmt":"2022-10-21T11:23:41","slug":"how-to-check-if-ports-are-open-on-a-array-of-servers","status":"publish","type":"post","link":"https:\/\/nicktailor.com\/tech-blog\/how-to-check-if-ports-are-open-on-a-array-of-servers\/","title":{"rendered":"How to check if ports are open on an array of servers"},"content":{"rendered":"<p style=\"margin-top: 0pt; padding-top: 0; margin-bottom: 8pt; padding-bottom: 0; line-height: 1.3;\">Okay now there is a whole bunch of ways you can do this. This is just the way I played around with to save myself a bunch of time, using NCAT. Also previously known as NETCAT.<\/p>\n<div class=\"ol\" style=\"margin: 0;\">\n<div class=\"li\" style=\"margin: 0;\">\n<div style=\"margin-top: 0pt; padding-top: 0; margin-bottom: 0; padding-bottom: 0; line-height: 1.3; margin-left: 36pt;\">\n<p><span style=\"display: inline-block; position: relative; text-indent: -18pt;\">1.<\/span>Ensure your Jumphost can ssh to all your newely deployed machines. Either you will use a root password or ssh key of some sort.<\/p>\n<\/div>\n<\/div>\n<div class=\"li\" style=\"margin: 0;\">\n<div style=\"margin-bottom: 0; padding-bottom: 0; line-height: 1.3; margin-left: 36pt;\"><span style=\"display: inline-block; position: relative; text-indent: -18pt;\">2.<\/span>You will also need to install ncat<\/div>\n<div class=\"ol\" style=\"margin: 0;\">\n<div class=\"li\" style=\"margin: 0;\">\n<div style=\"margin-bottom: 0; padding-bottom: 0; line-height: 1.3; margin-left: 72pt;\"><em><span style=\"display: inline-block; position: relative; text-indent: -18pt;\">a.<\/span>Yum install nmap-ncat (redhat\/centos)<\/em><br \/>\n<strong><\/strong><strong>Note (ensure you have this install on all the new servers)<\/strong>&nbsp;<\/p>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"li\" style=\"margin: 0;\">\n<div style=\"margin-bottom: 0; padding-bottom: 0; line-height: 1.3; margin-left: 36pt;\"><span style=\"display: inline-block; position: relative; text-indent: -18pt;\">3.<\/span>Open your editor and copy and paste this script below and save the file<\/div>\n<div class=\"ol\" style=\"margin: 0;\">\n<div class=\"li\" style=\"margin: 0;\">\n<div style=\"margin-bottom: 0; padding-bottom: 0; line-height: 1.3; margin-left: 72pt;\"><em><span style=\"display: inline-block; position: relative; text-indent: -18pt;\">b.<\/span>Vi portcheckscriptnick.sh &amp; save<\/em><\/div>\n<\/div>\n<div class=\"li\" style=\"margin: 0;\">\n<div style=\"margin-bottom: 8pt; padding-bottom: 0; line-height: 1.3; margin-left: 72pt;\"><em><span style=\"display: inline-block; position: relative; text-indent: -18pt;\">c.<\/span>Chmod +x portcheckscriptnick.sh (change permissioned to executable)<\/em><\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p style=\"margin-top: 0pt; padding-top: 0; margin-bottom: 8pt; padding-bottom: 0; line-height: 1.3; margin-left: 18pt;\"><strong><em>portcheckscriptnick.sh \u2013 this will check to see if your new server can talk to all the hosts below and check to see if those ports are up or down on each<\/em><\/strong><\/p>\n<p style=\"margin-top: 0pt; padding-top: 0; margin-bottom: 8pt; padding-bottom: 0; line-height: 1.3;\">============================<\/p>\n<p style=\"margin-top: 0pt; padding-top: 0; margin-bottom: 8pt; padding-bottom: 0; line-height: 1.3;\"><em>#!\/bin\/bash<\/em><\/p>\n<p style=\"margin-top: 0pt; padding-top: 0; margin-bottom: 8pt; padding-bottom: 0; line-height: 1.3;\"><em>host=&#8221;nick1 nick2 nick3 nick4&#8243;<\/em><\/p>\n<p style=\"margin-top: 0pt; padding-top: 0; margin-bottom: 8pt; padding-bottom: 0; line-height: 1.3;\"><em>for host in $host; do<\/em><\/p>\n<p style=\"margin-top: 0pt; padding-top: 0; margin-bottom: 8pt; padding-bottom: 0; line-height: 1.3;\"><em>for port in 22 53 67 68<\/em><\/p>\n<p style=\"margin-top: 0pt; padding-top: 0; margin-bottom: 8pt; padding-bottom: 0; line-height: 1.3;\"><em>do<\/em><\/p>\n<p style=\"margin-top: 0pt; padding-top: 0; margin-bottom: 8pt; padding-bottom: 0; line-height: 1.3;\"><em>if ncat -z $host $port<\/em><\/p>\n<p style=\"margin-top: 0pt; padding-top: 0; margin-bottom: 8pt; padding-bottom: 0; line-height: 1.3;\"><em>then<\/em><\/p>\n<p style=\"margin-top: 0pt; padding-top: 0; margin-bottom: 8pt; padding-bottom: 0; line-height: 1.3;\"><em>echo port $port $host is up<\/em><\/p>\n<p style=\"margin-top: 0pt; padding-top: 0; margin-bottom: 8pt; padding-bottom: 0; line-height: 1.3;\"><em>else<\/em><\/p>\n<p style=\"margin-top: 0pt; padding-top: 0; margin-bottom: 8pt; padding-bottom: 0; line-height: 1.3;\"><em>echo port $port $host is down<\/em><\/p>\n<p style=\"margin-top: 0pt; padding-top: 0; margin-bottom: 8pt; padding-bottom: 0; line-height: 1.3;\"><em>fi<\/em><\/p>\n<p style=\"margin-top: 0pt; padding-top: 0; margin-bottom: 8pt; padding-bottom: 0; line-height: 1.3;\"><em><span style=\"display: inline-block; height: 1em;\"><span style=\"display: none;\">.<\/span><\/span><\/em><\/p>\n<p style=\"margin-top: 0pt; padding-top: 0; margin-bottom: 8pt; padding-bottom: 0; line-height: 1.3;\"><em>done<\/em><\/p>\n<p style=\"margin-top: 0pt; padding-top: 0; margin-bottom: 8pt; padding-bottom: 0; line-height: 1.3;\"><em>done<\/em><br \/>\n========================================<\/p>\n<div class=\"ol\" style=\"margin: 0;\">\n<div class=\"li\" style=\"margin: 0;\">\n<div style=\"margin-top: 0pt; padding-top: 0; margin-bottom: 0; padding-bottom: 0; line-height: 1.3; margin-left: 36pt;\"><span style=\"display: inline-block; position: relative; text-indent: -18pt;\">4.<\/span>Next you want create an array for your for loop to cycle through and check if all those servers can communicate with those machine and ports<\/div>\n<div class=\"ol\" style=\"margin: 0;\">\n<div class=\"li\" style=\"margin: 0;\">\n<div style=\"margin-bottom: 0; padding-bottom: 0; line-height: 1.3; margin-left: 72pt;\"><span style=\"display: inline-block; position: relative; text-indent: -18pt;\">d.<\/span>Create a file called servers<\/div>\n<div class=\"ol\" style=\"margin: 0;\">\n<div class=\"li\" style=\"margin: 0;\">\n<div style=\"margin-bottom: 0; padding-bottom: 0; line-height: 1.3; margin-left: 108pt;\"><span style=\"display: inline-block; position: relative; text-indent: -9pt;\">i.<\/span>Vi servers<\/div>\n<\/div>\n<div class=\"li\" style=\"margin: 0;\">\n<div style=\"margin-bottom: 8pt; padding-bottom: 0; line-height: 1.3; margin-left: 108pt;\"><span style=\"display: inline-block; position: relative; text-indent: -9pt;\">ii.<\/span>Add a bunch of hosts in a single column<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p style=\"margin-top: 0pt; padding-top: 0; margin-bottom: 8pt; padding-bottom: 0; line-height: 1.3;\"><strong><span style=\"text-decoration: underline;\">Example:<\/span><\/strong><\/p>\n<p style=\"margin-top: 0pt; padding-top: 0; margin-bottom: 8pt; padding-bottom: 0; line-height: 1.3;\">Server1<\/p>\n<p style=\"margin-top: 0pt; padding-top: 0; margin-bottom: 8pt; padding-bottom: 0; line-height: 1.3;\">Server2<\/p>\n<p style=\"margin-top: 0pt; padding-top: 0; margin-bottom: 8pt; padding-bottom: 0; line-height: 1.3;\">Server3<\/p>\n<p style=\"margin-top: 0pt; padding-top: 0; margin-bottom: 8pt; padding-bottom: 0; line-height: 1.3;\">Server4<\/p>\n<div class=\"ol\" style=\"margin: 0;\">\n<div class=\"li\" style=\"margin: 0;\">\n<div style=\"margin-top: 0pt; padding-top: 0; margin-bottom: 8pt; padding-bottom: 0; line-height: 1.3; margin-left: 72pt;\"><span style=\"display: inline-block; position: relative; text-indent: -18pt;\">e.<\/span>Save the file <strong>servers<\/strong><\/div>\n<\/div>\n<\/div>\n<p style=\"margin-top: 0pt; padding-top: 0; margin-bottom: 8pt; padding-bottom: 0; line-height: 1.3;\"><span style=\"display: inline-block; height: 1em;\"><span style=\"display: none;\">.<\/span><\/span><\/p>\n<div class=\"ol\" style=\"margin: 0;\">\n<div class=\"li\" style=\"margin: 0;\">\n<div style=\"margin-top: 0pt; padding-top: 0; margin-bottom: 8pt; padding-bottom: 0; line-height: 1.3; margin-left: 36pt;\"><span style=\"display: inline-block; position: relative; text-indent: -18pt;\">5.<\/span>Now what were going to is have a for loop cycle through the list by logging into each host running that script and outputting the results to a file for us to look at.<\/div>\n<\/div>\n<\/div>\n<p style=\"margin-top: 0pt; padding-top: 0; margin-bottom: 8pt; padding-bottom: 0; line-height: 1.3;\"><span style=\"display: inline-block; height: 1em;\"><span style=\"display: none;\">.<\/span><\/span><\/p>\n<div class=\"ol\" style=\"margin: 0;\">\n<div class=\"li\" style=\"margin: 0;\">\n<div style=\"margin-top: 0pt; padding-top: 0; margin-bottom: 0; padding-bottom: 0; line-height: 1.3; margin-left: 36pt;\">\n<p><span style=\"display: inline-block; position: relative; text-indent: -18pt;\">6.<\/span>Run the following below check the servers and see if each server can communicate with the hosts and ports necessary. If you see the are down. Then you will need to check the firewalls to see why the host is unable to communicate.<\/p>\n<\/div>\n<div class=\"ul\" style=\"margin: 0;\">\n<div class=\"li\" style=\"margin: 0;\">\n<div style=\"margin-bottom: 8pt; padding-bottom: 0; line-height: 1.3; margin-left: 90pt;\">\n<p><span style=\"display: inline-block; position: relative; text-indent: -18pt; font-style: italic;\"><span style=\"position: absolute; top: -0.34em; left: 0; font-size: 2em;\">\u2022<\/span>&nbsp;<\/span><em>for HOST in $(cat server.txt) ; do ssh root@$HOST \u201cbash -s\u201d &lt; portcheckscriptnick.sh ; echo $HOST ; done 2&gt;&amp;1 | tee -a port.status<\/em><\/p>\n<p><strong><em>Note: the file port.status will be created on the jump host and you can simply look through to see if any ports were down on whichever hosts.<\/em><\/strong><\/p>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p style=\"margin-top: 0pt; padding-top: 0; margin-bottom: 8pt; padding-bottom: 0; line-height: 1.3;\"><span style=\"display: inline-block; height: 1em;\"><span style=\"display: none;\">.<\/span><\/span><\/p>\n<p style=\"margin-top: 0pt; padding-top: 0; margin-bottom: 8pt; padding-bottom: 0; line-height: 1.3;\"><em><strong>This is what the script looks like on one host if its working properly<\/strong><\/em><\/p>\n<p style=\"margin-top: 0pt; padding-top: 0; margin-bottom: 8pt; padding-bottom: 0; line-height: 1.3;\"><em>[root@nick ~]# .\/portcheckscriptnick.sh<\/em><\/p>\n<p style=\"margin-top: 0pt; padding-top: 0; margin-bottom: 8pt; padding-bottom: 0; line-height: 1.3;\"><em>port 22 192.168.1.11 is up<\/em><\/p>\n<p style=\"margin-top: 0pt; padding-top: 0; margin-bottom: 8pt; padding-bottom: 0; line-height: 1.3;\"><em>port 53 192.168.1.11 is down<\/em><\/p>\n<p style=\"margin-top: 0pt; padding-top: 0; margin-bottom: 8pt; padding-bottom: 0; line-height: 1.3;\"><em>port 67 192.168.1.11 is down<\/em><\/p>\n<p style=\"margin-top: 0pt; padding-top: 0; margin-bottom: 8pt; padding-bottom: 0; line-height: 1.3;\"><em>port 68 192.168.1.11 is down<\/em><\/p>\n<p style=\"margin-top: 0pt; padding-top: 0; margin-bottom: 8pt; padding-bottom: 0; line-height: 1.3;\"><em><span style=\"display: inline-block; height: 1em;\"><span style=\"display: none;\">.<\/span><\/span><\/em><\/p>\n<p style=\"margin-top: 0pt; padding-top: 0; margin-bottom: 8pt; padding-bottom: 0; line-height: 1.3;\"><em><strong>This is what it will look like when you run against your array of new hosts from your jumpbox<\/strong><\/em><\/p>\n<p style=\"margin-top: 0pt; padding-top: 0; margin-bottom: 8pt; padding-bottom: 0; line-height: 1.3;\"><em>[root@nick ~]# for HOST in $(cat servers.txt) ; do ssh root@$HOST &#8220;bash -s&#8221; &lt; portcheckscriptnick.sh ; echo $HOST ; done<\/em><\/p>\n<p style=\"margin-top: 0pt; padding-top: 0; margin-bottom: 8pt; padding-bottom: 0; line-height: 1.3;\"><em>root@192.168.1.11&#8217;s password:<\/em><\/p>\n<p style=\"margin-top: 0pt; padding-top: 0; margin-bottom: 8pt; padding-bottom: 0; line-height: 1.3;\"><em>port 22 nick1 is up<\/em><\/p>\n<p style=\"margin-top: 0pt; padding-top: 0; margin-bottom: 8pt; padding-bottom: 0; line-height: 1.3;\"><em>port 53 nick1 is down<\/em><\/p>\n<p style=\"margin-top: 0pt; padding-top: 0; margin-bottom: 8pt; padding-bottom: 0; line-height: 1.3;\"><em>port 67 nick1 is down<\/em><\/p>\n<p style=\"margin-top: 0pt; padding-top: 0; margin-bottom: 8pt; padding-bottom: 0; line-height: 1.3;\"><em>port 68 nick1 is down<\/em><\/p>\n<p style=\"margin-top: 0pt; padding-top: 0; margin-bottom: 8pt; padding-bottom: 0; line-height: 1.3;\"><em>port 22 nick2 is up<\/em><\/p>\n<p style=\"margin-top: 0pt; padding-top: 0; margin-bottom: 8pt; padding-bottom: 0; line-height: 1.3;\"><em>port 53 nick2 is down<\/em><\/p>\n<p style=\"margin-top: 0pt; padding-top: 0; margin-bottom: 8pt; padding-bottom: 0; line-height: 1.3;\"><em>port 67 nick2 is down<\/em><\/p>\n<p style=\"margin-top: 0pt; padding-top: 0; margin-bottom: 8pt; padding-bottom: 0; line-height: 1.3;\"><em>port 68 nick2 is down<\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Okay now there is a whole bunch of ways you can do this. This is just the way I played around with to save myself a bunch of time, using NCAT. Also previously known as NETCAT. 1.Ensure your Jumphost can ssh to all your newely deployed machines. Either you will use a root password or ssh key of some sort.<a href=\"https:\/\/nicktailor.com\/tech-blog\/how-to-check-if-ports-are-open-on-a-array-of-servers\/\" class=\"read-more\">Read More &#8230;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[58,138,124,131],"tags":[],"class_list":["post-1113","post","type-post","status-publish","format-standard","hentry","category-centos","category-linux","category-network-stuff","category-security"],"_links":{"self":[{"href":"https:\/\/nicktailor.com\/tech-blog\/wp-json\/wp\/v2\/posts\/1113","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nicktailor.com\/tech-blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nicktailor.com\/tech-blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nicktailor.com\/tech-blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nicktailor.com\/tech-blog\/wp-json\/wp\/v2\/comments?post=1113"}],"version-history":[{"count":15,"href":"https:\/\/nicktailor.com\/tech-blog\/wp-json\/wp\/v2\/posts\/1113\/revisions"}],"predecessor-version":[{"id":1591,"href":"https:\/\/nicktailor.com\/tech-blog\/wp-json\/wp\/v2\/posts\/1113\/revisions\/1591"}],"wp:attachment":[{"href":"https:\/\/nicktailor.com\/tech-blog\/wp-json\/wp\/v2\/media?parent=1113"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nicktailor.com\/tech-blog\/wp-json\/wp\/v2\/categories?post=1113"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nicktailor.com\/tech-blog\/wp-json\/wp\/v2\/tags?post=1113"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}