使用java實(shí)現(xiàn)telnet-client工具分享
telnet-client太費(fèi)盡了,比ssh-client費(fèi)盡的多,搞了一天,湊合能用,還得改。
org.apache.commons.net.telnet.TelnetClien --使用了apache的commons-net包,commons-net-3.0.1-bin.zip。
package org.sl.util;
import org.apache.commons.net.telnet.TelnetClient;
import java.io.*;
import java.nio.ByteBuffer;
public class TelnetUtil {
String charset = null;
byte[] buff = new byte[2048];
TelnetClient telnetClient = new TelnetClient();
BufferedReader telnetReader = null;
BufferedWriter telnetWirter = null;
InputStream telnetIn = null;
OutputStream telnetOut = null;
public TelnetUtil() {
telnetClient = new TelnetClient();
}
/**
* 連接至服務(wù)器
* @param ip
* @param port
* @throws UnsupportedEncodingException
* @throws IOException
*/
public void connect(String ip, int port) throws UnsupportedEncodingException,IOException {
telnetClient.connect(ip,port);
setIOStream();
}
/**
* 連接至服務(wù)器
* @param ip
* @throws UnsupportedEncodingException
* @throws IOException
*/
public void connect(String ip) throws UnsupportedEncodingException,IOException {
telnetClient.connect(ip);
setIOStream();
}
void setIOStream() throws UnsupportedEncodingException {
telnetIn = telnetClient.getInputStream();
telnetOut = telnetClient.getOutputStream();
if(null==charset){
telnetReader = new BufferedReader(new InputStreamReader(telnetIn));
telnetWirter = new BufferedWriter(new OutputStreamWriter(telnetOut));
}else{
telnetReader = new BufferedReader(new InputStreamReader(telnetIn, charset));
telnetWirter = new BufferedWriter(new OutputStreamWriter(telnetOut, charset));
}
}
/**
* 登錄
* @param user
* @param passwd
* @return 是否登錄成功.
* @throws IOException
*/
public boolean login(String user,String passwd) throws IOException {
String read = readString();
for(int i=0; ; i++){
if(-1==read.indexOf("login")){
read = readString();
}else{
break;
}
}
writeText(user);
read = readString();
for(int i=0; ; i++){
if(-1==read.indexOf("Password")){
read = readString();
}else{
break;
}
}
writeText(passwd);
for(;;){
read = readString();
//System.out.println("last:"+read);
if(-1!=read.indexOf("Last")){
return true;
}else if(-1!=read.indexOf("incorrect")){
return false;
}
}
}
/**
* 這是一個(gè)測(cè)試方法,隨便寫。
* @throws IOException
*/
public void show() throws IOException {
// System.out.println(readString());
// System.out.println(readString());
// ByteBuffer tmp = ByteBuffer.allocate(1024);
// byte[] buff = new byte[1024];
// while(telnetIn.available()>0){
// int readLen = readBytes(buff,0,1024);
// tmp.put(buff,0,readLen);
// }
// System.out.println(new String(tmp.array()));
System.out.println("1 "+readString());
System.out.println("2 "+readString());
System.out.println("3 "+readString());
writeText("root");
System.out.println("4 " + readString());
writeText("123456");
System.out.println("5 "+readString());
// System.out.println("6 "+readString());
// System.out.println("7 "+readString());
}
public int readBytes(byte[] buff, int offset, int len) throws IOException {
return telnetIn.read(buff,offset,len);
}
/**
* 讀取字符串<br/>
* 相當(dāng)于readByte()轉(zhuǎn)為字符串
* @return
* @throws IOException
*/
public String readString() throws IOException {
int readLen = readBytes(this.buff, 0, this.buff.length);
if(0<readLen)
return new String(buff,0,readLen).trim();
else
return "";
}
/**
* 讀取一行<br/>
* 如果服務(wù)器與客戶端不是同一種操作系統(tǒng),可能導(dǎo)致此方法計(jì)行失敗。
* @return
* @throws IOException
*/
public String readLine() throws IOException {
String read = telnetReader.readLine();
return null==read?"":read.trim();
}
public void writeBytes(byte[] buff, int offset, int len) throws IOException {
telnetOut.write(buff,offset,len);
}
/**
* 向服務(wù)器寫字符串
* @param text
* @throws IOException
*/
public void writeText(String text) throws IOException {
telnetWirter.write(text);
telnetWirter.write('\r');
telnetWirter.write('\n');
telnetWirter.flush();
}
/**
* 執(zhí)行命令,并返回結(jié)果<br/>
* 相當(dāng)于: <br>
* writeText(); <br/>
* return readString();
* @param cmd
* @return
* @throws IOException
*/
public String exec(String cmd) throws IOException {
writeText(cmd);
return readString();
}
String login1(String user,String passwd) throws IOException {
String read = null;
readString();
readString();
read = readString();
if(-1!=read.indexOf("login")){
writeText(user);
}
read = readString();
if(-1!=read.indexOf("Password")){
writeText(passwd);
}
read = readString();
read += readString();
return read;
// StringBuffer sb = new StringBuffer();
// while(null!= (read = readString())){
// sb.append(read);
// }
//
// return sb.toString();
}
/**
* 關(guān)閉
*/
public void close(){
try{
writeText("exit");
writeText("exit");
writeText("exit");
}catch(Exception ex){
}
try {
if(null!=telnetIn) telnetIn.close();
} catch (Exception e) {
}
try {
if(null!=telnetOut) telnetOut.close();
} catch (Exception e) {
}
try {
if(null!=telnetClient)telnetClient.disconnect();
} catch (Exception e) {
}
}
/**
* 設(shè)置telnet通信時(shí)的字符集<br/>
* 注:此字符集與服務(wù)器端字符集沒(méi)有必然關(guān)系<br/>
* 此方法需在connect()前調(diào)用
* @param charset
*/
public void setCharset(String charset ){
this.charset = charset;
}
/**
* 重新設(shè)置buff大小,默認(rèn)為2048字節(jié).
* @param size
*/
public void setBufferSize(int size){
this.buff = new byte[size];
}
}
測(cè)試類
static void t4(){
TelnetUtil tu = new TelnetUtil();
try {
tu.connect("192.168.2.154");
System.out.println(tu.login("root", "123456"));
//tu.show();
//System.out.println(tu.readString());
//System.out.println(tu.exec("pwd"));
System.out.println(tu.exec("echo \"123456789\">1.txt"));
System.out.println(tu.exec("cat 1.txt"));
} catch (IOException e) {
e.printStackTrace();
}
tu.close();
}
static void t1(){
TelnetUtil tu = new TelnetUtil();
try {
tu.connect("192.168.2.154");
System.out.println(tu.login("sl1", "coffee8215"));
//tu.show();
//System.out.println(tu.readString());
System.out.println(tu.exec("pwd"));
} catch (IOException e) {
e.printStackTrace();
}
tu.close();
}
相關(guān)文章
SpringBoot feign動(dòng)態(tài)設(shè)置數(shù)據(jù)源(https請(qǐng)求)
這篇文章主要介紹了SpringBoot如何在運(yùn)行時(shí)feign動(dòng)態(tài)添加數(shù)據(jù)源,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2021-08-08詳解SpringCloud Gateway 2020.0.2最新版
這篇文章主要介紹了SpringCloud Gateway 2020.0.2最新版,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-04-04SpringBoot使用validation做參數(shù)校驗(yàn)的實(shí)現(xiàn)步驟
這篇文章主要介紹了SpringBoot使用validation做參數(shù)校驗(yàn)的實(shí)現(xiàn)步驟,幫助大家更好的理解和學(xué)習(xí)使用SpringBoot,感興趣的朋友可以了解下2021-05-05springboot整合redis實(shí)現(xiàn)發(fā)送郵箱并驗(yàn)證
大家好,本篇文章主要講的是springboot整合redis實(shí)現(xiàn)發(fā)送郵箱并驗(yàn)證,感興趣的同學(xué)趕快來(lái)看一看吧,對(duì)你有幫助的話記得收藏一下2022-01-01SpringBoot項(xiàng)目配置postgresql數(shù)據(jù)庫(kù)完整步驟(配置多數(shù)據(jù)源)
PostgreSQL是一種特性非常齊全的自由軟件的對(duì)象-關(guān)系型數(shù)據(jù)庫(kù)管理系統(tǒng)(ORDBMS),下面這篇文章主要給大家介紹了關(guān)于SpringBoot項(xiàng)目配置postgresql數(shù)據(jù)庫(kù)(配置多數(shù)據(jù)源)的相關(guān)資料,需要的朋友可以參考下2023-05-05Java中static修飾的靜態(tài)變量、方法及代碼塊的特性與使用
這篇文章主要介紹了Java中static修飾的靜態(tài)變量、方法及代碼塊的特性與使用,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-04-04java多線程加鎖以及Condition類的使用實(shí)例解析
這篇文章主要介紹了java多線程加鎖以及Condition類的使用實(shí)例解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-11-11