首页 > 新闻系统 > 编程天地 > 文章正文

检测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);
9 7 3 1 2 3 4 8 :

精彩推荐
焦点大图推荐
本类热门文章

论坛美图

广告联系 | 版权说明 | 意见建议 | 加入收藏 | 军网站群 [ 军软件园 - 军软件商城 - 军软件园论坛 ]

电信与信息服务业务经营许可证:京ICP证050203