1. import org.apache.commons.net.telnet.TelnetClient;
  2.  
  3. import java.io.IOException;
  4.  
  5. public class TelnetDemo {
  6.  
  7. public static void main(String[] args) throws IOException {
  8. TelnetClient telnet = new TelnetClient();
  9. String remoteip = "10.1.1.159";
  10. int remoteport = 9999;
  11. telnet.connect(remoteip, remoteport);
  12. System.out.println(telnet.isAvailable());
  13. System.out.println(telnet.isConnected());
  14.  
  15. IOUtil.readWrite(telnet.getInputStream(), telnet.getOutputStream(),
  16. System.in, System.out);
  17. telnet.disconnect();
  18. System.exit(0);
  19. }
  20. }
  1. import org.apache.commons.net.io.Util;
  2.  
  3. import java.io.IOException;
  4. import java.io.InputStream;
  5. import java.io.OutputStream;
  6.  
  7. /**
  8. * This is a utility class providing a reader/writer capability required
  9. * by the weatherTelnet, rexec, rshell, and rlogin example programs.
  10. * The only point of the class is to hold the static method readWrite
  11. * which spawns a reader thread and a writer thread. The reader thread
  12. * reads from a local input source (presumably stdin) and writes the
  13. * data to a remote output destination. The writer thread reads from
  14. * a remote input source and writes to a local output destination.
  15. * The threads terminate when the remote input source closes.
  16. * *
  17. */
  18.  
  19. public final class IOUtil {
  20.  
  21. public static final void readWrite(final InputStream remoteInput,
  22. final OutputStream remoteOutput,
  23. final InputStream localInput,
  24. final OutputStream localOutput) {
  25. Thread reader, writer;
  26.  
  27. reader = new Thread() {
  28. @Override
  29. public void run() {
  30. int ch;
  31.  
  32. try {
  33. while (!interrupted() && (ch = localInput.read()) != -1) {
  34. remoteOutput.write(ch);
  35. remoteOutput.flush();
  36. }
  37. } catch (IOException e) {
  38. e.printStackTrace();
  39. }
  40. }
  41. };
  42.  
  43. writer = new Thread() {
  44. @Override
  45. public void run() {
  46. try {
  47. Util.copyStream(remoteInput, localOutput);
  48. } catch (IOException e) {
  49. e.printStackTrace();
  50. System.exit(1);
  51. }
  52. }
  53. };
  54.  
  55. writer.setPriority(Thread.currentThread().getPriority() + 1);
  56. writer.start();
  57. reader.setDaemon(true);
  58. reader.start();
  59.  
  60. try {
  61. writer.join();
  62. reader.interrupt();
  63. } catch (InterruptedException e) {
  64. e.printStackTrace();
  65. }
  66. }
  67.  
  68. }

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. Java内部类——局部内部类

    局部内部类,即放在“{块中}”,局部内部类,不能被成员修饰符static修饰,且内部类的成员也不能是static. 内部类中访问局部变量“{块中}”,需要将局部变量声明为final. 可以直接访问外部 ...

  2. linux+Qt程序如何打包发布

    源地址:http://zhidao.baidu.com/link?url=UTWEoXS21B4p1L5LJmYgGBMAr0dTdXfzmaGbWeltnwQLA3Uc9_K9RcDQFFIArbx ...

  3. kali之ARP欺骗获取图片流

    其实很简单,就两步: 1. 后接三个参数: 网卡:eth0    网关:10.0.0.1    攻击目标:10.0.0.128 2.启动监听 会弹出一个框 里面就会显示攻击目标通过浏览器访问的页面上的 ...

  4. Windows Azure入门教学系列 (一): 创建第一个WebRole程序

    原文 Windows Azure入门教学系列 (一): 创建第一个WebRole程序 在第一篇教学中,我们将学习如何在Visual Studio 2008 SP1中创建一个WebRole程序(C#语言 ...

  5. WPF Popup 置顶问题

    原文 WPF Popup 置顶问题 问题: 使用wpf的popup,当在popup中弹出MessageBox或者打开对话框的时候,popup总是置顶,并遮住MessageBox或对话框. 解决: 写如 ...

  6. 六款常用的linux C/C++ IDE

    摘要: 一.AnjutaAnjuta是一个多语言的IDE,它最大的特色是灵活,同时打开多个文件,内嵌代码级的调试器(调用gdb),应用程序向导(Application wizards)可以方便的帮助你 ...

  7. 几种快速傅里叶变换(FFT)的C++实现

    链接:http://blog.csdn.net/zwlforever/archive/2008/03/14/2183049.aspx一篇不错的FFT 文章,收藏一下. DFT的的正变换和反变换分别为( ...

  8. c#后台弹出提示

    Page.ClientScript.RegisterClientScriptBlock(typeof(string), "", @"<script>alert ...

  9. boost计算随机数和计算crc32简单示例

    boost计算随机数和计算crc32简单示例 - jwybobo2007的专栏 - 博客频道 - CSDN.NET boost计算随机数和计算crc32简单示例 2013-02-18 17:14 10 ...

  10. android ListView中CheckBox错位的解决

    貌似已经非常晚了,可是还是想记下笔记,想让今天完满. 在ListView中加了checkBox,但是发现点击改变其选中状态的时候,发现其位置错乱.状态改变的并非你选中的,百思不得其解.后面通过上网查资 ...