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();
}
} }

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

  1. Java调用Telnet示例

    import java.io.IOException; import java.io.InputStream; import java.io.PrintStream; import java.io.U ...

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

    介绍 本文介绍如何通过Telnet协议连接到远程Shell,执行脚本,并获取执行结果: 相关文章: <[Jsch]使用SSH协议连接到远程Shell执行脚本>http://www.cnbl ...

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

    转自:http://www.cnblogs.com/younggun/p/3247261.html Apache Commons包含了很多开源的工具,用于解决平时编程经常会遇到的问题,减少重复劳动.下 ...

  4. Apache Commons 工具类简单使用

    Apache Commons包含了很多开源的工具,用于解决平时编程经常会遇到的问题,减少重复劳动.下面是我这几年做开发过程中自己用过的工具类做简单介绍. 组件 功能介绍 BeanUtils 提供了对于 ...

  5. Apache Commons 工具集介绍

    Apache Commons包含了很多开源的工具,用于解决平时编程经常会遇到的问题,减少重复劳动.下面是我这几年做开发过程中自己用过的工具类做简单介绍. 组件 功能介绍 BeanUtils 提供了对于 ...

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

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

  7. Apache Commons 工具类介绍及简单使用(转载)

    原文链接 http://www.cnblogs.com/younggun/p/3247261.html Apache Commons包含了很多开源的工具,用于解决平时编程经常会遇到的问题,减少重复劳动 ...

  8. java操作telnet远程登录

    import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import jav ...

  9. libevent安装及使用

    一.安装libevent 官网:http://libevent.org/ 选择最新版本下载,我选择的是libevent-2.0.22-stable.tar.gz,然后安装README文件中描述的方法编 ...

随机推荐

  1. HDU 3966 Aragorn&#39;s Story(树链剖分)

    HDU Aragorn's Story 题目链接 树抛入门裸题,这题是区间改动单点查询,于是套树状数组就OK了 代码: #include <cstdio> #include <cst ...

  2. EEPlat PaaS VS Saleforce force.com

    综述 EEPlatPaaS和Saleforce的Force.com都是元数据驱动应用的解决方式.整体而言,Force.com提供了更上层的解决方式,屏蔽了SQL语句.数据库:EEPlat更加底层,有更 ...

  3. EasyUI - Menu 菜单

    效果: html代码: <div id="mm" class="easyui-menu"> <div id =">New< ...

  4. MFC画二维动态图表[GDI]

    源博客:http://www.codeproject.com/Articles/9350/2D-Animated-Charts 源代码:http://download.csdn.net/detail/ ...

  5. accept系统调用内核实现

    用户态对accept的标准使用方法: if ((client_fd = accept(sockfd, (struct sockaddr *)&remote_addr, &sin_siz ...

  6. 【慎思堂】之JS牛腩总结

    一 JS基础 1-定义 Javascript是一种脚本语言/描述语言,是一种解释性语言.用于开发交互式web网页,使得网页和用户之间实现了一种实时性的.动态的.交互性的关系,使网页包含更多活跃的元素和 ...

  7. vim 操作指令1

    1.vim 在命令行中输入vim,进入vim编辑器 2.i 按一下i键,下端显示 –INSERT–插入命令,在vim中可能任意字符都有作用 3.Esc 退出i(插入)命令进行其它命令使用 4.:r f ...

  8. windows消息处理(强烈推荐,收藏)

    由于看了一下,比较好理解,暂时先放到这里,待有空再翻译.只是在每节后大致介绍一下讲的内容. 感觉写的比较全,无论从消息的原理还是从MFC操作上来说,值得一看,我也在此做个收藏. (一) 说明:以下首先 ...

  9. JavaScript函数节流与函数去抖

    介绍 首先解释一下这两个概念: 函数节流(throttle):是让一个函数无法在很短的时间间隔内连续调用,当上一次函数执行后过了规定的时间间隔,才能进行下一次该函数的调用. 函数去抖(debounce ...

  10. Node-Webkit作者王文睿:桌面应用的全新开发方式

    摘要:最近两年,Node.js技术越来越火,基于它所开发的应用也纷纷出现在大家面前,其中Node-Webkit就是这样的一个开源框架,它允许开发者使用Web技术开发桌面应用. Node-Webkit是 ...