pop
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.InterruptedIOException;
import java.net.ConnectException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.NoRouteToHostException;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.StringTokenizer;
public class TestPop3 {
public static void main(String[] args){
int retry = 1;
int port = 110;
int timeout = 3000;
InetAddress ipv4Addr = null;
try {
ipv4Addr = InetAddress.getByName("192.168.0.8");
} catch (UnknownHostException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
int serviceStatus = -1;
long responseTime = -1;
for (int attempts = 0; attempts <= retry && serviceStatus != 1; attempts++) {
Socket socket = null;
try {
//
// create a connected socket
//
long sentTime = System.currentTimeMillis();
socket = new Socket();
socket.connect(new InetSocketAddress(ipv4Addr, port), timeout);
socket.setSoTimeout(timeout);
System.out.println("Pop3Monitor: connected to host: " + ipv4Addr + " on port: " + port);
// We're connected, so upgrade status to unresponsive
serviceStatus = -1;
BufferedReader rdr = new BufferedReader(new InputStreamReader(socket.getInputStream()));
//
// Tokenize the Banner Line, and check the first
// line for a valid return.
//
// Server response should start with: "+OK"
//
String banner = rdr.readLine();
responseTime = System.currentTimeMillis() - sentTime;
if (banner == null)
continue;
StringTokenizer t = new StringTokenizer(banner);
if (t.nextToken().equals("+OK")) {
//
// POP3 server should recoginize the QUIT command
//
String cmd = "QUIT ";
socket.getOutputStream().write(cmd.getBytes());
//
// Parse the response to the QUIT command
//
// Server response should start with: "+OK"
//
t = new StringTokenizer(rdr.readLine());
if (t.nextToken().equals("+OK")) {
serviceStatus = 1;
}
}
// If we get this far and the status has not been set
// to available, then something didn't verify during
// the banner checking or QUIT command process.
if (serviceStatus != 1) {
serviceStatus = -1;
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
// Close the socket
if (socket != null)
socket.close();
} catch (IOException e) {
System.out.println("poll: Error closing socket."+ e);
}
}
}
}
}
smtp
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.StringTokenizer;
import java.util.regex.Pattern;
public class TestSmtp {
public static void main(String[] args){
int retry = 1;
int timeout = 3000;
int port = 25;
// Get interface address from NetworkInterface
InetAddress ipv4Addr = null;
try {
ipv4Addr = InetAddress.getByName("192.168.0.8");
} catch (UnknownHostException e1) {
e1.printStackTrace();
}
int serviceStatus = -1;
long responseTime = -1;
for (int attempts = 0; attempts <= retry && serviceStatus != 1; attempts++) {
Socket socket = null;
try {
// create a connected socket
//
long sentTime = System.currentTimeMillis();
socket = new Socket();
socket.connect(new InetSocketAddress(ipv4Addr, port), timeout);
socket.setSoTimeout(timeout);
System.out.println("SmtpMonitor: connected to host: " + ipv4Addr + " on port: " + port);
// We're connected, so upgrade status to unresponsive
serviceStatus = -2;//SERVICE_UNRESPONSIVE;
BufferedReader rdr = new BufferedReader(new InputStreamReader(socket.getInputStream()));
//
// Tokenize the Banner Line, and check the first
// line for a valid return.
//
String banner = rdr.readLine();
String LOCALHOST_NAME = null;
if (banner == null)
continue;
System.out.println(banner.toString());
while (banner != null && !Pattern.matches("^[0-9]{3}.*",banner.toString())){
System.out.println(banner.toString());
banner = rdr.readLine();
}
if (banner == null)
continue;
try {
LOCALHOST_NAME = InetAddress.getLocalHost().getHostName();
} catch (UnknownHostException uhE) {
LOCALHOST_NAME = "localhost";
}
System.out.println("poll: banner = " + banner);
StringTokenizer t = new StringTokenizer(banner);
int rc = Integer.parseInt(t.nextToken());
if (rc == 220) {
//
// Send the HELO command
//
String cmd = "HELO " + LOCALHOST_NAME + " ";
socket.getOutputStream().write(cmd.getBytes());
//
// get the returned string, tokenize, and
// verify the correct output.
//
String response = rdr.readLine();
responseTime = System.currentTimeMillis() - sentTime;
if (response == null)
continue;
while (banner != null && !Pattern.matches("^[0-9]{3}.*",response)){
System.out.println(response.toString());
response = rdr.readLine();
}
if (response == null)
continue;
t = new StringTokenizer(response);
rc = Integer.parseInt(t.nextToken());
if (rc == 250) {
cmd = "QUIT ";
socket.getOutputStream().write(cmd.getBytes());
// get the returned string, tokenize, and
// verify the correct output.
response = rdr.readLine();
if (response == null)
continue;
// read until we hit the last line of the multi-line
// response
while (banner != null && !Pattern.matches("^[0-9]{3}.*",response)){
System.out.println(response.toString());
response = rdr.readLine();
}
if (response == null)
continue;
t = new StringTokenizer(response);
rc = Integer.parseInt(t.nextToken());
if (rc == 221) {
serviceStatus = 1;
// Store response time in RRD
System.out.println("response time="+responseTime);
}
}
}
// If we get this far and the status has not been set
// to available, then something didn't verify during
// the banner checking or HELO/QUIT comand process.
if (serviceStatus != 1) {
serviceStatus = -1;
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
// Close the socket
if (socket != null)
socket.close();
} catch (IOException e) {
e.fillInStackTrace();
System.out.println("poll: Error closing socket."+ e);
}
}
}
}
}