org.apache.common.net.examples.telnet.MyTelnetClientExample.java Source code

Java tutorial

Introduction

Here is the source code for org.apache.common.net.examples.telnet.MyTelnetClientExample.java

Source

/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.apache.common.net.examples.telnet;

import org.apache.commons.net.telnet.TelnetClient;

import java.io.IOException;
import java.net.ConnectException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *         
 *      
 */
public class MyTelnetClientExample {
    //private static List<String> okList = new ArrayList<String>();
    private static List<String> errorList = new ArrayList<String>();

    private static final List<String> ips = Arrays.asList("10.69.32.32", "10.120.210.25");

    private static final Map<String, String> hosts = new HashMap<String, String>();

    static {
        hosts.put("vmwl2616.dc.mars", "10.69.32.32");

        hosts.put("mtolx728.dc.mars", "10.65.42.80");

        hosts.put("www.sitenka.ru", "10.100.85.146");

        hosts.put("uwb01.digex.na.mars", "10.100.161.9");

        hosts.put("www.sitenka.ru", "10.100.161.245");

        hosts.put("marsbpos03.effem.com", "10.126.1.202");

        hosts.put("auroradb.digex.na.mars", "10.100.104.9");

        hosts.put("vmwl2729-nat.dc.mars", "10.120.210.25");

        hosts.put("isxl1057.dc.mars", "10.121.16.49");

        hosts.put("vmwl2781-nat.dc.mars", "10.65.126.21");

        hosts.put("vmwl2782-nat.dc.mars", "10.120.210.27");

        hosts.put("vmwl2783-nat.dc.mars", "10.120.210.28");

        hosts.put("mtol1175.dc.mars", "10.69.16.131");

        // hosts.put("vmwl2729-nat.dc.mars", "10.120.210.25");

    }

    private static final List<Integer> ports = Arrays.asList(21, 22, 23, 80, 443, 7001, 7002, 7003, 7004, 7005,
            7006, 7007, 7008, 7009, 7010);

    private static final List<Integer> onePort = Arrays.asList(22);

    public static void main(String[] args) {
        for (Map.Entry<String, String> entry : hosts.entrySet()) {
            for (Integer port : ports) {
                checkByTelnet(entry, port);
            }
        }

        //checkByTelnet("www.google.ru", 80);
        //checkByTelnet("www.google.ru", 81);

        System.out.println(errorList);
    }

    private static void checkByTelnet(Map.Entry<String, String> entry, int port) {
        String remoteHost = entry.getKey();
        String remoteIp = entry.getValue();
        int remotePort = port;

        final TelnetClient tc = new TelnetClient();
        try {
            tc.connect(remoteHost, remotePort);
        } catch (ConnectException exception) {
            System.out.println("catch ConnectException to");
            errorList.add("can't connect to " + remoteIp + "(" + remoteHost + ")" + " by port " + port);
        } catch (IOException e) {
            e.printStackTrace();
        }

        try {
            tc.disconnect();
        } catch (IOException e) {
            System.err.println("Exception while connecting:" + e.getMessage());
        }
    }
}