package com.ailk.ess.webapp2.servermng.net;

import java.io.InputStream;

import java.io.OutputStream;

import java.util.ArrayList;

import java.util.List;

import org.apache.commons.net.telnet.TelnetClient;

public class TelnetUtil {

//telnet客户端对象VT220/VT52

TelnetClient client = new TelnetClient("VT52");

StringBuffer buffer = new StringBuffer();

InputStream inputStream = null; // 输入流,接收服务端的返回信息

OutputStream outputStream = null; // 输出流,向服务端写命令

private static List defaultPromt = new ArrayList();

private static List user = new ArrayList();

private static List pass = new ArrayList();

//默认端口

public static int defaultport = 23;

static {

defaultPromt.add("#");

defaultPromt.add(">");

defaultPromt.add("%");

user.add("ogin:");

pass.add("assword:");

}

/**

* @param hostname

* 服务器IP地址

* @param port

* telnet端口

* @param username

* 用户名

* @param password

* 密码

* @throws Exception

*/

public TelnetUtil(String hostname, int port, String username, String password) throws Exception {

// 连接服务器

conn(hostname, port);

// 获得输入流对象

this.inputStream = this.client.getInputStream();

// 获得输出流对象

this.outputStream = this.client.getOutputStream();

login(username, password);

}

/**

* 关闭连接

*

* @throws Exception

*/

public void close() throws Exception {

this.client.disconnect();

}

/**

* 连接到服务器

*

* @param hostname

* 服务器IP地址

* @param port

* 端口

* @throws Exception

*/

private void conn(String hostname, int port) throws Exception {

this.client.connect(hostname, port);

}

/**

* 登录服务器

*

* @param username

* 用户名

* @param password

* 密码

* @throws Exception

*/

private void login(String username, String password) throws Exception {

sendCommand(username, user);

List temp = new ArrayList();

temp.add(":");

String result = getResult(temp);

if (!(result.trim().endsWith("word:"))) {

throw new Exception("Invalid user:" + username);

}

temp.add("#");

temp.add(">");

temp.add("%");

sendCommand(password, pass);

result = getResult( temp );

if ((result.trim().endsWith("word:"))

|| (result.trim().endsWith("ogin:"))) {

throw new Exception("Invalid username or password:" + username

+ " " + password);

}

}

public void sendCommand(String command) throws Exception {

sendCommand(command, defaultPromt);

}

public String getResult() throws Exception {

return getResult(defaultPromt);

}

/**

* 往服务器输入命令

*

* @param command

* 命令指令

* @param wantedEndString

* @throws Exception

*/

public void sendCommand(String command, List wantedEndString)

throws Exception {

waitForString(wantedEndString);

this.buffer.delete(0, this.buffer.length());

// 输出输入的命令值

// System.out.println(command + "\n");

this.outputStream.write((command + "\n").getBytes());

this.outputStream.flush();

}

public String getResult(List endString) throws Exception {

waitForString(endString);

return this.buffer.toString();

}

private void waitForString(List wantedEndString) throws Exception {

int aword = 0;

boolean matchOne = false;

while (!(matchOne)) {

for (int i = 0; i < wantedEndString.size(); ++i) {

String back = this.buffer.toString().trim();

if ((back.endsWith((String)wantedEndString.get(i))) && (this.inputStream.available() == 0)){

matchOne = true;

}

}

if (matchOne) {

return;

}

aword = this.inputStream.read();

// System.out.print((char) aword);

if (aword < 0) {

throw new Exception("Connection disconnect...");

}

this.buffer.append((char) aword);

}

}

public boolean isClosed() {

return (!(this.client.isConnected()));

}

}

util.java_TelnetUtil.java相关推荐

  1. /travel/src/main/java/cn/itcast/travel/util/MailUtils.java:[1,1] 非法字符:

    [ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-c ...

  2. 泛微OA E9 weaver.conn.util.ProcChecker java.lang.ClassNotFoundException: com.weaver.procedure. 报错

    报错如下: weaver.conn.util.ProcChecker java.lang.ClassNotFoundException: com.weaver.procedure.test.Test_ ...

  3. java util concurrent_Error: java.util.concurrent.Execution

    看到这一串错误有点不想看,不过细分一下看了估计就是那个图片的问题. Error:java.util.concurrent.ExecutionException: com.android.ide.com ...

  4. java8 util.time_Java8 java.util.Date转换为java.time.ZonedDateTime

    尝试将java.util.Date转换为java.time.LocalDate时,我收到以下异常. java.time.DateTimeException: Unable to obtain Zone ...

  5. java.util.zip_[Java 基础] 使用java.util.zip包压缩和解压缩文件

    Java API中的import java.util.zip.*;包下包含了Java对于压缩文件的所有相关操作. 我们可以使用该包中的方法,结合IO中的相关知识,进行文件的压缩和解压缩相关操作. Zi ...

  6. java.util.Date java.sql.Date SimpleDateFormat String 转DATE

    涉及知识点: 1.SQL server  中   datetime   对应  java  中的  timeStamp  类型 PreparedStatement   |  CallableState ...

  7. java.util.Date java.sql.Date

    java.sql.Date dateSql = new java.sql.Date(new java.util.Date().getTime());System.out.println(dateSql ...

  8. com.xxl.rpc.util.XxlRpcException: java.lang.IllegalStateException: failed to create a child event lo

    Caused by: java.io.IOException: 打开的文件过多 生产上已经调整了linux的最大句柄数为65535 但是运行时间长或是xxl-admin重启有什么变动的情况下我们就会出 ...

  9. 【工具方法util】JAVA中将一个List等分成n个list的

    为什么80%的码农都做不了架构师?>>>    public static <T> List<List<T>> averageAssign(Lis ...

最新文章

  1. 更快的 Maven 来了!!!性能提升 300%
  2. 小白如何用免费GPU跑天池算法大赛!
  3. 码农口述:AI创业两年,积蓄花光,重回职场敲代码
  4. 网络编程(part8)--传输层服务之TCP和UDP
  5. oracle select 行数据_【赵强老师】什么是Oracle的数据字典?
  6. java富文本如何转义_富文本编辑器wangEditor中转义字符的问题
  7. oem监控mysql_OEM12c 安装配置MySQL Plug-in用来监控MySQL
  8. osm 搭建离线地图_使用离线OSM离线OpenLayers Web应用程序
  9. 3-6Tensor的属性稀疏的变量的编程
  10. 申通快递:1月快递服务业务收入25.65亿元 同比增长21.27%
  11. 利尔达e95蓝牙模块程序_Arduino使用HC05蓝牙模块与手机连接
  12. C++之文件操作探究(四):读文件——二进制文件
  13. SolarWinds 供应链攻击中的第四款恶意软件及其它动态
  14. 计算机操作系统详细学习笔记(三):存储管理
  15. 基于R语言时间序列的平稳时间序列模型预测
  16. win10安装jdk
  17. 2020年用于测试自动化的7种顶级编程语言
  18. 将Keras模型导出为SavedModel模型
  19. bugku never_give_up file_get_contents()有php://input漏洞 eregi \x00绕过
  20. 智慧城市建设方案建议书——如何打造智慧城市

热门文章

  1. axure web组件_AXURE原型设计:移动端选择器的应用
  2. 河南洛阳计算机操作题,洛阳中招理化生实验操作考试、信息技术考试下月1日开考...
  3. idea快捷键之记录
  4. 关于计算机软件系统分类能够匹配的有,以下关于计算机软件系统分类能够匹配的有:...
  5. db2存储过程 可以使用游标循环嵌套吗_DB2存储过程使用动态游标的例子
  6. switch()语句块的出口:break;
  7. ajax中res和req,javascript – 来自AJAX调用的res.redirect
  8. mysql 数据修改记录日志_mysql对数据的更新操作记录在哪个日志中?
  9. 中小学计算机教学大纲,中小学信息技术教材教法教学大纲
  10. html视频标签略缩图,JS可以截取video的标签视频缩略图吗?