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

时间:2023-02-24 22:48:39

介绍

本文介绍如何通过Telnet协议连接到远程Shell,执行脚本,并获取执行结果;

相关文章:
《【Jsch】使用SSH协议连接到远程Shell执行脚本》http://www.cnblogs.com/ssslinppp/p/6244653.html 
其他示例:

maven仓库

使用Apache Commons-net通用库;
  1. <dependency>
  2. <groupId>commons-net</groupId>
  3. <artifactId>commons-net</artifactId>
  4. <version>3.4</version>
  5. </dependency>

《Apache Commons Net示例》http://commons.apache.org/proper/commons-net/  
包括:
  • FTP/FTPS
  • FTP over HTTP (experimental)
  • NNTP
  • SMTP(S)
  • POP3(S)
  • IMAP(S)
  • Telnet
  • TFTP
  • Finger
  • Whois
  • rexec/rcmd/rlogin
  • Time (rdate) and Daytime
  • Echo
  • Discard
  • NTP/SNTP
  • Backgr

具体步骤

  • 步骤1: 使用TelnetClient创建连接:connect();
  • 步骤2: 设置Telnet属性:如 回显选项/SUPPRESS GO AHEAD/终端类型等;
  • 步骤3: 获取输入/输出流:getInputStream()/getOutputStream();
  • 步骤4: 使用username和password进行登录;
  • 步骤5: 执行Shell脚本,获取执行结果;
  • 步骤6: 关闭资源:输入/输出流,TelnetClient连接等;

程序

步骤1~步骤6
【Telnet】使用Telnet协议连接到远程Shell执行脚本

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

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

完整程序

  1. package com.sssppp.Communication;
  2. import java.io.IOException;
  3. import java.io.InputStream;
  4. import java.io.OutputStream;
  5. import java.net.InetAddress;
  6. import java.net.SocketTimeoutException;
  7. import org.apache.commons.net.telnet.EchoOptionHandler;
  8. import org.apache.commons.net.telnet.SuppressGAOptionHandler;
  9. import org.apache.commons.net.telnet.TelnetClient;
  10. import org.apache.commons.net.telnet.TerminalTypeOptionHandler;
  11. public class TelentCommUtil {
  12. /**
  13. * 测试程序
  14. *
  15. * @param args
  16. */
  17. public static void main(String[] args) {
  18. String ip = "10.180.137.221";
  19. int port = 23;
  20. String localIp = null;
  21. int localPort = 0;
  22. int timeOut = 3000;
  23. String userName = "xxxxx";
  24. String password = "xxxxx";
  25. String[] cmds = new String[] { "ifconfig | grep eth0\n",
  26. "cat /etc/redhat-release\n" };
  27. String[] result = null;
  28. try {
  29. result = execShellCmdByTelnet(ip, port, localIp, localPort, timeOut,
  30. userName, password, cmds);
  31. } catch (Exception e) {
  32. e.printStackTrace();
  33. }
  34. if (result != null) {
  35. for (String string : result) {
  36. System.out.println(string);
  37. System.out.println("-------------------");
  38. }
  39. }
  40. }
  41. /**
  42. * 使用Telnet协议,连接到Linux Shell,执行脚本命令,并获取结果
  43. *
  44. * @param dstIp
  45. * @param dstPort
  46. * @param localIp
  47. * @param localPort
  48. * @param timeOut
  49. * @param userName
  50. * @param password
  51. * @param cmds
  52. * @return
  53. * @throws Exception
  54. */
  55. public static String[] execShellCmdByTelnet(String dstIp, int dstPort,
  56. String localIp, int localPort, int timeOut, String userName,
  57. String password, String... cmds) throws Exception {
  58. TelnetClient tc = new TelnetClient();
  59. InputStream is = null;
  60. OutputStream os = null;
  61. try {
  62. //设置:RFC 1091 TELNET终端类型选项
  63. tc.addOptionHandler(new TerminalTypeOptionHandler("VT100", false,
  64. false, true, false));
  65. //设置:RFC 857 TELNET ECHO 回显选项
  66. tc.addOptionHandler(new EchoOptionHandler(true, false, true, false));
  67. //设置:RFC 858 TELNET SUPPRESS GO AHEAD(抑制继续进行)选项
  68. tc.addOptionHandler(new SuppressGAOptionHandler(true, true, true,
  69. true));
  70. tc.setConnectTimeout(timeOut);
  71. if (localIp == null) {
  72. tc.connect(dstIp, dstPort);
  73. } else {
  74. tc.connect(InetAddress.getByName(dstIp), dstPort,
  75. InetAddress.getByName(localIp), localPort);
  76. }
  77. is = tc.getInputStream();
  78. os = tc.getOutputStream();
  79. //输入用户名和密码
  80. if (sendCommand(is, os, "\n").contains("login:")) {
  81. if (sendCommand(is, os, userName + "\n").contains("assword:")) {
  82. if (sendCommand(is, os, password + "\n").contains(
  83. "incorrect")) {
  84. throw new Exception("Auth error");
  85. }
  86. }
  87. }
  88. String[] result = new String[cmds.length];
  89. for (int i = 0; i < cmds.length; i++) {
  90. result[i] = sendCommand(is, os, cmds[i]);
  91. }
  92. return result;
  93. } catch (SocketTimeoutException e) {
  94. throw new Exception("SocketTimeoutException error");
  95. } catch (Exception e) {
  96. throw e;
  97. } finally {
  98. try {
  99. is.close();
  100. } catch (Exception e) {
  101. }
  102. try {
  103. os.close();
  104. } catch (Exception e) {
  105. }
  106. try {
  107. tc.disconnect();
  108. } catch (IOException e) {
  109. }
  110. }
  111. }
  112. /**
  113. * 执行Shell命令,并获取执行结果
  114. *
  115. * @param is
  116. * @param os
  117. * @param cmd
  118. * @return
  119. * @throws IOException
  120. */
  121. private static String sendCommand(InputStream is, OutputStream os,
  122. String cmd) throws IOException {
  123. os.write(cmd.getBytes());
  124. os.flush();
  125. StringBuffer sb = new StringBuffer();
  126. int beat = 0;
  127. while (true) {
  128. if (beat > 3) {
  129. break;
  130. }
  131. if (is.available() > 0) {
  132. byte[] b = new byte[is.available()];
  133. is.read(b);
  134. sb.append(new String(b));
  135. beat = 0;
  136. } else {
  137. if (sb.length() > 0) {
  138. beat++;
  139. }
  140. try {
  141. Thread.sleep(sb.toString().trim().length() == 0 ? 1000
  142. : 300);
  143. } catch (InterruptedException e) {
  144. }
  145. }
  146. }
  147. return sb.toString();
  148. }
  149. }


























【Telnet】使用Telnet协议连接到远程Shell执行脚本的更多相关文章

  1. 【Jsch】使用SSH协议连接到远程Shell执行脚本

    如果大家熟悉Linux的话,一定对ssh,sftp,scp等命令非常熟悉,ssh是一个安全协议,用来在不同系统或者服务器之间进行安全连接,SSH 在连接和传送的过程中会加密所有的数据. 但是SSH一般 ...

  2. linux-ssh远程后台执行脚本-放置后台执行问题(转)

    写了一个监控负载的小脚本(死循环,测试结束后再kill对应进程),因需要监控多台服务器,所以在一台服务器上使用ssh统一执行脚本遇到问题:使用ssh root@172.16.146.20 '/usr/ ...

  3. 一步一步学Python&lpar;2&rpar; 连接多台主机执行脚本

    最近在客户现场,每日都需要巡检大量主机系统的备库信息.如果一台台执行,时间浪费的就太冤枉了. 参考同事之前写的一个python脚本,配合各主机上写好的shell检查脚本,实现一次操作得到所有巡检结果. ...

  4. linux集群自动化搭建&lpar;生成密钥对&plus;分发公钥&plus;远程批量执行脚本&rpar;

    之前介绍过ansible的使用,通过ssh授权批量控制服务器集群 但是生成密钥和分发公钥的时候都是需要确认密码的,这一步也是可以自动化的,利用ssh + expect + scp就可以实现,其实只用这 ...

  5. shell脚本学习—Shell执行脚本

    Shell作用是解释执行用户的命令,用户输入一条命令,Shell就解释执行这一条,这种方式称为交互式,但还有另一种执行命令的方式称为批处理方式,用户事先写一个Shell脚本,Shell可以一次把这些命 ...

  6. Shell执行脚本

    Shell作用是解释执行用户的命令,用户输入一条命令,Shell就解释执行这一条,这种方式称为交互式,但还有另一种执行命令的方式称为批处理方式,用户事先写一个Shell脚本,Shell可以一次把这些命 ...

  7. python模拟shell执行脚本

    工作时候需要模拟shell来执行任务,借助包paramkio import paramiko class ShellExec(object): host = '127.0.0.1' port = 36 ...

  8. 通过Socket让远程电脑执行脚本

    实现功能: 客户端发送命令,服务器接收命令并执行 服务端: import socketserver, os class MyTCPHandler(socketserver.BaseRequestHan ...

  9. 简单shell执行脚本

    #!/bin/bash source /etc/profile APPLICATIONS_HOME="/opt/cpic_analy" APPLICATION_NAME=&quot ...

随机推荐

  1. VIM的一些操作小技巧

    vim的设计理念是:组合. 命令的组合,模式的组合,     普通模式 左: h 上:k 下:j 右 : l   i : 当前光标处插入 I: 到光标所在行的行首进入插入模式 a: 在当前光标的后一位 ...

  2. DLX &lpar;poj 3074&rpar;

    题目:Sudoku 匪夷所思的方法,匪夷所思的速度!!! https://github.com/ttlast/ACM/blob/master/Dancing%20Link%20DLX/poj%2030 ...

  3. JavaScript数据类型(转)

    JavaScript中有5种简单数据类型(也称为基本数据类型):Undefined.Null.Boolean.Number和String.还有1种复杂数据类型——Object,Object本质上是由一 ...

  4. C&num; 条形码 生成函数 &lpar;Code 128 标准

    C# 条形码 生成函数 (Code 128 标准参考:GB/T 18347-2001) 最近在做单据打印,发现客户要求用到条形码,在网上找了,发现只有一些条形码的标准,但打出来发现根本不能扫,还要加某 ...

  5. Android --------- 压缩图片的尺寸和大小

    压缩图片大小,尺寸不变 将已知路径的图片压缩至不大于目标大小,并保存至指定路径 /** * 质量压缩,通过给定的路径来压缩图片并保存到指定路径 * * @param srcPath * 资源图片的路径 ...

  6. P3601 签到题

    思路 注意到求的qiandao(x)就是\(x-\phi(x)\) 但是\(l,r\le 10^{12}\),所以不能直接杜教筛 但是\(r-l\le 10^{6}\),所以可以先筛出1e6(\(\s ...

  7. Android&colon; 网络随时需要在3G和Wifi切换,网络程序需要注意

    平时,3G和WIFI 都开着的时候,Android默认使用Wifi,但现实环境中不可能到处都有wifi,所以手机会经常自动切换网络. 有的时候,手机一开始使用wifi上网,当进入待机后10-30分钟, ...

  8. extern、static、restrict、volatile 关键字

    extern extern的两个作用: 修饰变量或函数,提示编译器此变量或函数是在其它文件中定义的,但要在此处引用: 进行链接指定,如: extern "C" void fun(i ...

  9. Python&plus;OpenCV图像处理(六)—— ROI与泛洪填充

    一.ROI ROI(region of interest),感兴趣区域.机器视觉.图像处理中,从被处理的图像以方框.圆.椭圆.不规则多边形等方式勾勒出需要处理的区域,称为感兴趣区域,ROI. 代码如下 ...

  10. mysql insert 事务相关&lpar;草稿&rpar;

    当 insert 多条语句时初步试了一下是自带事务机制的,如在一个这样的表中: 执行语句 INSERT INTO `t_mytest`(`id`) VALUES (1),(2),(3),(4),(5) ...