注册
 找回密码
 注册
江西广告网
查看: 287|回复: 0
打印 上一主题 下一主题

检测SSH服务是否正常工作代码

[复制链接]

该用户从未签到

1
跳转到指定楼层
发表于 2009-1-14 08:27:04 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有帐号?注册

x
连接检测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); // We’re connected, so upgrade status to unresponsive serviceStatus = -2; if (strBannerMatch == null || strBannerMatch.equals("*")) ...{ serviceStatus = 1; break; } BufferedReader rdr = new BufferedReader(new InputStreamReader(socket.getInputStream())); // Tokenize the Banner Line, and check the first // line for a valid return. String response = rdr.readLine(); responseTime = System.currentTimeMillis() - sentTime; if (response == null) continue; System.out.println("poll: banner = " response); System.out.println("poll: responseTime= " responseTime "ms"); if (response.indexOf(strBannerMatch) > -1) ...{ serviceStatus = 1; } else serviceStatus = -1; } catch (Exception e) ...{ e.printStackTrace(); } finally ...{ try ...{ // Close the socket if (socket != null) socket.close(); } catch (IOException e) ...{ e.printStackTrace(); } } } } }
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表