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. linux命令: sort

    参考 linux sort 命令详解 http://www.cnblogs.com/51linux/archive/2012/05/23/2515299.html 1 sort的工作原理 sort将文 ...

  2. <2>集腋成裘

    标量项: [root@wx03 2]# cat a1.pl unshift(@INC,"/root/big/2"); use Horse;; print $Horse::days; ...

  3. SVM(支持向量机)(二)—Lagrange Duality(拉格朗日对偶问题)

    (整理自AndrewNG的课件,转载请注明.整理者:华科小涛@http://www.cnblogs.com/hust-ghtao/) SVM有点让人头疼,但还是要弄明白.把这一大块搞懂了,会很有成就感 ...

  4. poj 2786 - Keep the Customer Satisfied

    Description   Simon and Garfunkel Corporation (SG Corp.) is a large steel-making company with thousa ...

  5. QNX---- interrupts 例程

    #include <sys/neutrino.h> int interruptID; const struct sigevent * intHandler (void *arg, int ...

  6. [课堂实践与项目]IOS优先级的计算器

    这个计算器主要是使用数组进行实现的.虽然没有使用前缀后缀表达式,但是是一种方法o. .h文件 // // LCViewController.h // 具有优先级的calculator // // Cr ...

  7. MongoDB学习笔记(四) 用MongoDB的文档结构描述数据关系

    MongoDB的集合(collection)可以看做关系型数据库的表,文档对象(document)可以看做关系型数据库的一条记录.但两者并不完全对等.表的结构是固定的,MongoDB集合并没有这个约束 ...

  8. HDOJ 2442 -bricks 状态压缩DP 一直TLE.打表过的..

    有5个砖块..加上一个空着不放..那么有6种状态..所以很明显的可以用6进制的状态DP... 不过这么做..我觉得我已经能优化的都优化了...还是超时..一看数据范围是100*6..打表先AC了.. ...

  9. 四张类图理一下Streams的用法

    首先是输出流 OutputStream.继承它的类有两种,一种是底层实现(纯继承OutputStream的类),一种是格式转换(组合了OutputStream的类). 所谓的底层实现,就是真正和物理存 ...

  10. Codeforces Round #218 (Div. 2)

    500pt, 题目链接:http://codeforces.com/problemset/problem/371/A 分析:k-periodic说明每一段长度为k,整个数组被分成这样长度为k的片段,要 ...