{"id":6,"date":"2008-03-24T18:27:35","date_gmt":"2008-03-25T00:27:35","guid":{"rendered":"http:\/\/xux.in\/blog\/post\/monitor-open-ports-using-php-snippet\/"},"modified":"2008-09-22T12:34:47","modified_gmt":"2008-09-22T17:34:47","slug":"monitor-open-ports-using-php-snippet","status":"publish","type":"post","link":"http:\/\/xux.in\/blog\/post\/monitor-open-ports-using-php-snippet\/","title":{"rendered":"Monitor open ports using PHP (snippet)"},"content":{"rendered":"<p>How can you really know if a port is open or closed? Most of the scripts around the web fail doing it&#8217;s job, not because they are wrong but because they are not doing their job as they should.<\/p>\n<p>Am I on drugs? No, not now \ud83d\ude09 basicly, what all scripts do is:<\/p>\n<pre lang=\"bash\">[root@local]# telnet yourhost port<\/pre>\n<p>That will tell you if yourhost is open on that port but sometimes it will just hang there, why? there are many reasons why a server or service could hang (I&#8217;m not covering that part &#8230; at least not for free :P), but the only thing you really need to know is, IT HAPPENS&#8230; when? how? why? it will.<\/p>\n<p>So, if you are in the middle of coding some script that let&#8217;s you monitor your servers \/ services without worrying about that &#8220;small particular issue&#8221;, you are in the right place, check out the code:<\/p>\n<pre lang=\"php\">\r\nfunction check_port($ip=\"\",$port=\"\",$request=\"\",$replies=\"\") {\r\n  if (!$ip || !$port) {\r\n    echo \"No ip\/port to check\";\r\n\t  return;\r\n  }\r\n\tif (!($fp = @fsockopen($ip,$port,$junk,$junk,10)) {\r\n\t  echo \"Connection DOWN!\";\r\n\t\treturn;\r\n\t}\r\n\tif (!$request && !$replies) {\r\n\t  echo \"Connection UP! (open socket)\";\r\n\t\treturn;\r\n\t}\r\n\tif ($request) {\r\n\t  fputs($fp,$request,strlen($request));\r\n\t\tfputs($fp,\"\\r\\n\\r\\n\",8);\r\n\t}\r\n\tstream_set_timeout($fp,10);\r\n\tdo {\r\n\t\t$response .= fgets($fp);\r\n\t} while (!feof($fp));\r\n\t@fclose($fp);\r\n\t$response = preg_replace(\"\/\\n|\\r\/\",\"\",$response);\r\n\t$result = false;\r\n\t$error = $response;\r\n\t$array_replies = explode(\",\",$replies);\r\n\tif (is_array($array_replies)) {\r\n\t  foreach ($array_replies as $v) {\r\n\t\t  if (!$v) continue;\r\n\t\t\tif (preg_match(\"\/$v\/i\",$response)) {\r\n\t\t\t  $result = true;\r\n                          break;\r\n\t\t\t}\r\n\t\t}\r\n\t}\r\n\tif ($result) {\r\n\t  echo \"Connection UP!\";\r\n\t\treturn;\r\n\t}\r\n\techo \"Connection error: $response\");\r\n\treturn;\r\n}<\/pre>\n<p>That&#8217;s a mess! Yes I know, it is dirty and uggly but it works. That function takes 4 arguments, $ip (server&#8217;s IP), $port (server&#8217;s port), $request and $replies (you can use comma delimited here in case you need to receive one or more answers).<\/p>\n<p>How it works? Well copy that piece of code to any php file and call it this way:<\/p>\n<pre lang=\"php\">\/\/ This first example will tell us if google.com is up ;)\r\n\/\/ it sends the request \"HEAD \/ HTTP\/1.0\" to the IP 72.14.207.99 on the port 80\r\n\/\/ and expects 2 answers: \"200\" or \"OK\"\r\ncheck_port(\"72.14.207.99\",80,\"HEAD \/ HTTP\/1.0\",\"200,OK\");\r\n\/\/ it sends the request \"HEAD \/ HTTP\/1.0\" to the IP 72.14.207.99 on the port 80\r\n\/\/ and expects 2 answers: \"200\" or \"OK\"\r\n\r\n\/\/ another example?\r\ncheck_port(\"148.235.52.179\",110,\"\",\"\\+OK\");\r\n\/\/ this one will check port 110 (pop3) on that IP, it won't send a request but\r\n\/\/ it will sit till gets a \"\\+OK\"<\/pre>\n<p>Remember, all requests and replies depends on the server&#8217;s side, be aware of that \ud83d\ude09<\/p>\n","protected":false},"excerpt":{"rendered":"<p>How can you really know if a port is open or closed? Most of the scripts around the web fail doing it&#8217;s job, not because they are wrong but because they are not doing their job as they should. Am I on drugs? No, not now \ud83d\ude09 basicly, what all scripts do is: [root@local]# telnet [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[31,8,5],"tags":[15,17,16],"class_list":["post-6","post","type-post","status-publish","format-standard","hentry","category-code","category-linux","category-software","tag-php","tag-port-monitor","tag-snippet"],"_links":{"self":[{"href":"http:\/\/xux.in\/blog\/wp-json\/wp\/v2\/posts\/6","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/xux.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/xux.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/xux.in\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/xux.in\/blog\/wp-json\/wp\/v2\/comments?post=6"}],"version-history":[{"count":2,"href":"http:\/\/xux.in\/blog\/wp-json\/wp\/v2\/posts\/6\/revisions"}],"predecessor-version":[{"id":35,"href":"http:\/\/xux.in\/blog\/wp-json\/wp\/v2\/posts\/6\/revisions\/35"}],"wp:attachment":[{"href":"http:\/\/xux.in\/blog\/wp-json\/wp\/v2\/media?parent=6"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/xux.in\/blog\/wp-json\/wp\/v2\/categories?post=6"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/xux.in\/blog\/wp-json\/wp\/v2\/tags?post=6"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}