检测SSH服务是否正常工作代码2008-04-16 09:41:34 来源:中国自学编程网 作者:佚名 点击:
![]() 连接检测SSH服务是否正常工作
import java.io.BufferedReader; 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; public class TestSSH ...{ public static void main(String[] args)...{ int retry = 1; int timeout = 3000; // Port // int port = 22; String strBannerMatch = "SSH"; // Get the address instance. // InetAddress ipv4Addr = null; try ...{ ipv4Addr = InetAddress.getByName("fedora.unix-center.net"); } catch (UnknownHostException e1) ...{ // TODO Auto-generated catch block e1.printStackTrace(); } // Give it a whirl // 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("SshMonitor: connected to host: " + ipv4Addr + " on port: " + port); 相关文章: |