2008. 9. 17. 15:11
Firewall Checker Computing에 관한 독백2008. 9. 17. 15:11
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketTimeoutException;
public class FirewallChecker {
public static void main(String[] args) {
if (2 != args.length) {
System.err.println("java FirewallChecker <ip or hostname> <port>");
System.exit(-1);
}
Socket theSocket = null;
BufferedReader br = null;
String input = null;
System.out.println("Trying...");
try {
theSocket = new Socket();
theSocket.connect(new InetSocketAddress(args[0], Integer.parseInt(
args[1], 10)), 1000 * 60);
System.out.println("Connected to " + args[0] + ".");
System.out.println("Escape character is 'q'");
br = new BufferedReader(new InputStreamReader(System.in));
do {
input = br.readLine();
} while (!"q".equals(input));
if (null != br)
br.close();
if (!theSocket.isClosed())
theSocket.close();
} catch (NumberFormatException nfe) {
System.err.println(args[0] + ": bad port number");
System.exit(-2);
} catch (IllegalArgumentException uhe) {
System.err.println(args[0] + ": bad port number");
System.exit(-2);
} catch (SocketTimeoutException ste) {
System.err
.println("FirewallChecker: connect: A remote host did not respond within the timeout period.");
System.exit(-4);
} catch (IOException e) {
e.printStackTrace();
System.exit(-5);
}
System.out.println("Connection closed.");
}
}
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketTimeoutException;
public class FirewallChecker {
public static void main(String[] args) {
if (2 != args.length) {
System.err.println("java FirewallChecker <ip or hostname> <port>");
System.exit(-1);
}
Socket theSocket = null;
BufferedReader br = null;
String input = null;
System.out.println("Trying...");
try {
theSocket = new Socket();
theSocket.connect(new InetSocketAddress(args[0], Integer.parseInt(
args[1], 10)), 1000 * 60);
System.out.println("Connected to " + args[0] + ".");
System.out.println("Escape character is 'q'");
br = new BufferedReader(new InputStreamReader(System.in));
do {
input = br.readLine();
} while (!"q".equals(input));
if (null != br)
br.close();
if (!theSocket.isClosed())
theSocket.close();
} catch (NumberFormatException nfe) {
System.err.println(args[0] + ": bad port number");
System.exit(-2);
} catch (IllegalArgumentException uhe) {
System.err.println(args[0] + ": bad port number");
System.exit(-2);
} catch (SocketTimeoutException ste) {
System.err
.println("FirewallChecker: connect: A remote host did not respond within the timeout period.");
System.exit(-4);
} catch (IOException e) {
e.printStackTrace();
System.exit(-5);
}
System.out.println("Connection closed.");
}
}
용도: 방화벽 룰 설정을 제대로 했는지 검사는 해야 하는데 보안 정책 때문에 telnet 등을 쓰지 못하게 해놓았을 경우. JDK 1.5에서 작성/구동하였음.
이 글은 스프링노트에서 작성되었습니다.