日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

util.java_TelnetUtil.java

發(fā)布時間:2025/3/8 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 util.java_TelnetUtil.java 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

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; // 輸入流,接收服務(wù)端的返回信息

OutputStream outputStream = null; // 輸出流,向服務(wù)端寫命令

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

* 服務(wù)器IP地址

* @param port

* telnet端口

* @param username

* 用戶名

* @param password

* 密碼

* @throws Exception

*/

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

// 連接服務(wù)器

conn(hostname, port);

// 獲得輸入流對象

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

// 獲得輸出流對象

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

login(username, password);

}

/**

* 關(guān)閉連接

*

* @throws Exception

*/

public void close() throws Exception {

this.client.disconnect();

}

/**

* 連接到服務(wù)器

*

* @param hostname

* 服務(wù)器IP地址

* @param port

* 端口

* @throws Exception

*/

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

this.client.connect(hostname, port);

}

/**

* 登錄服務(wù)器

*

* @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);

}

/**

* 往服務(wù)器輸入命令

*

* @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()));

}

}

創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎勵來咯,堅持創(chuàng)作打卡瓜分現(xiàn)金大獎

總結(jié)

以上是生活随笔為你收集整理的util.java_TelnetUtil.java的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。