함수 is port open()


개요

is_port_open
is_port_listen
  • test set
8.8.8.8, 21 → false
8.8.8.8, 53 → true

Bash

IP=8.8.8.8
PORT=53
TIMEOUT=2
IS_PORT_OPEN=`nc -z -w$TIMEOUT $IP $PORT`
if [ "$IS_PORT_OPEN" = "" ]
then
	IS_PORT_OPEN=false
else
	IS_PORT_OPEN=true
fi
echo $IS_PORT_OPEN
# true

C#

private bool isPortOpen(string host, int port) {
   try {
       TcpClient tc = new TcpClient();
       tc.Connect(host, port);
   }
   catch {
       return false;
   }
   return true;
}

PHP

function is_port_open($host, $port, $timeout=2) {
  $result = @fsockopen($host, $port, $errno, $errstr, $timeout);
  if($result)	return true;
  return false;
}

같이 보기