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

时间:2024-05-16 21:36:32

介绍

本文介绍如何通过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. }