commons.net.telnet使用示例

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

import java.io.IOException;

public class TelnetDemo {

    public static void main(String[] args) throws IOException {
        TelnetClient telnet = new TelnetClient();
        String remoteip = "10.1.1.159";
        int remoteport = 9999;
        telnet.connect(remoteip, remoteport);
        System.out.println(telnet.isAvailable());
        System.out.println(telnet.isConnected());

        IOUtil.readWrite(telnet.getInputStream(), telnet.getOutputStream(),
                System.in, System.out);
        telnet.disconnect();
        System.exit(0);
    }
}
import org.apache.commons.net.io.Util;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

/**
 * This is a utility class providing a reader/writer capability required
 * by the weatherTelnet, rexec, rshell, and rlogin example programs.
 * The only point of the class is to hold the static method readWrite
 * which spawns a reader thread and a writer thread.  The reader thread
 * reads from a local input source (presumably stdin) and writes the
 * data to a remote output destination.  The writer thread reads from
 * a remote input source and writes to a local output destination.
 * The threads terminate when the remote input source closes.
 * *
 */

public final class IOUtil {

    public static final void readWrite(final InputStream remoteInput,
                                       final OutputStream remoteOutput,
                                       final InputStream localInput,
                                       final OutputStream localOutput) {
        Thread reader, writer;

        reader = new Thread() {
            @Override
            public void run() {
                int ch;

                try {
                    while (!interrupted() && (ch = localInput.read()) != -1) {
                        remoteOutput.write(ch);
                        remoteOutput.flush();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        };

        writer = new Thread() {
            @Override
            public void run() {
                try {
                    Util.copyStream(remoteInput, localOutput);
                } catch (IOException e) {
                    e.printStackTrace();
                    System.exit(1);
                }
            }
        };

        writer.setPriority(Thread.currentThread().getPriority() + 1);
        writer.start();
        reader.setDaemon(true);
        reader.start();

        try {
            writer.join();
            reader.interrupt();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

}
时间: 2024-08-10 15:10:28

commons.net.telnet使用示例的相关文章

【Telnet】使用Telnet协议连接到远程Shell执行脚本

介绍 本文介绍如何通过Telnet协议连接到远程Shell,执行脚本,并获取执行结果: 相关文章: <[Jsch]使用SSH协议连接到远程Shell执行脚本>http://www.cnblogs.com/ssslinppp/p/6244653.html 其他示例: http://commons.apache.org/proper/commons-net/examples/telnet/TelnetClientExample.java http://www.programcreek.com/ja

java操作telnet远程登录

import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.net.SocketException; import org.apache.commons.net.telnet.TelnetClient; public class Client { public static void main(S

Apache Commons 工具类介绍及简单使用

Apache Commons包含了很多开源的工具,用于解决平时编程经常会遇到的问题,减少重复劳动.下面是我这几年做开发过程中自己用过的工具类做简单介绍.   组件 功能介绍 BeanUtils 提供了对于JavaBean进行各种操作,克隆对象,属性等等. Betwixt XML与Java对象之间相互转换. Codec 处理常用的编码方法的工具类包 例如DES.SHA1.MD5.Base64等. Collections java集合框架操作. Compress java提供文件打包 压缩类库. C

Apache Commons 工具集介绍

Apache Commons包含了很多开源的工具,用于解决平时编程经常会遇到的问题,减少重复劳动.下面是我这几年做开发过程中自己用过的工具类做简单介绍. 组件 功能介绍 BeanUtils 提供了对于JavaBean进行各种操作,克隆对象,属性等等. Betwixt XML与Java对象之间相互转换. Codec 处理常用的编码方法的工具类包 例如DES.SHA1.MD5.Base64等. Collections java集合框架操作. Compress java提供文件打包 压缩类库. Con

Apache Commons工具集简介

Apache Commons包含了很多开源的工具,用于解决平时编程经常会遇到的问题,减少重复劳动.下面是我这几年做开发过程中自己用过的工具类做简单介绍. 组件 功能介绍 BeanUtils 提供了对于JavaBean进行各种操作,克隆对象,属性等等. Betwixt XML与Java对象之间相互转换. Codec 处理常用的编码方法的工具类包 例如DES.SHA1.MD5.Base64等. Collections java集合框架操作. Compress java提供文件打包 压缩类库. Con

Java 通过telnet协议 操作远程交换机

/**  *@date 2015年2月4日 下午3:15:13  *@author XUQIANG  *@filename TelnetConnection.java  *@package com.merit.monitor.device.parameter.sdk  */ package com.merit.monitor.device.parameter.sdk; import java.io.BufferedReader; import java.io.IOException; impor

java通过telnet远程至windows机器执行dos命令

准备工作,远程windows机器中开启telnet服务,将远程登录用户添加至telnetClients用户组 核心代码: import java.io.IOException; import java.io.InputStream; import java.io.PrintStream;import java.io.UnsupportedEncodingException;import org.apache.commons.net.telnet.TelnetClient;import org.t

memcached的使用

一.windows下安装 环境:win7专业版+memcached_win32_1.4.5 步骤: 1.cmd切换到指定解压目录 2.memcached -d install安装 3.输入memcached –h,出现下图窗口说明已经安装成功 4.telnet测试是否正常运行 telnet 127.0.0.1 11211 如下图: 5.stats命令查看运行状态如下图: 二.linux下安装 环境:redhat5+memcached-1.4.25 所需安装包: 步骤: 1.解压libevent

JVM性能分析工具jstack介绍

JDK本身提供了很多方便的JVM性能调优监控工具,除了集成式的VisualVM和jConsole外, 还有jps.jstack.jmap.jhat.jstat.hprof等小巧的工具,每一种工具都有其自身的特点, 用户可以根据你需要检测的应用或者程序片段的状况,适当的选择相应的工具进行检测, 先通过一个表格形式简要介绍下这几个命令的作用和使用方法.本文重点介绍jstack的使用方法. 命令 作用 jps 基础工具 jstack 查看某个Java进程内的线程堆栈信息 jmap jmap导出堆内存,