導航:首頁 > 編程語言 > javatelnet客戶端

javatelnet客戶端

發布時間:2023-07-05 23:42:57

『壹』 java socket編程中,telnet到server客戶端輸入如退格(backspace)或上下左右等只顯示字元而不是實現其功能

不是,你的刪除鍵也是一個鍵值,所以會記下例如[a
如果你想刪除的請按ctrl+backspace來刪除

『貳』 java連接telnet環境的代碼,(不要通過結尾字元來判斷的),

建議設置讀取中斷時長,如果超過時長表示,該輸入用戶名/密碼了。
tc.setSoTimeout(timeout); tc是 TelnetClient

『叄』 java使用telnet連接遠程伺服器調用cmd命令時 報堆溢出錯誤是什麼原因

  1. 如果執行一次沒問題,執行多次後,出現內存溢出,檢查代碼,查看流是否都已經關閉,沒有疏漏。

  2. 如果程序啟動後,第一次執行就出現內存溢出錯誤,設置你的ide,調高分配內存。

『肆』 用java實現ssh/telnet跳轉登錄

Runtime.getRuntime().exec("")

獲取輸入輸出流,模擬登錄

『伍』 急,java中telnet訪問遠程電腦,然後執行cmd命令,再獲取返回值。

需要個jar包:commons-net-3.3-bin.zip

下載地址:http://commons.apache.org/proper/commons-net/download_net.cgi


你找的代碼我修改過了,復制直接使用。linux系統需要先開啟telnet服務

//執行的命令
System.out.println(she.sendCommand("ll"));

這個輸入linux 命令

importjava.io.InputStream;
importjava.io.PrintStream;

importorg.apache.commons.net.telnet.TelnetClient;

publicclassShell{
privateTelnetClienttelnet=newTelnetClient();

privateInputStreamin;

privatePrintStreamout;

privatecharprompt='$';//普通用戶結束

publicShell(Stringip,intport,Stringuser,Stringpassword){
try{
telnet.connect(ip,port);
in=telnet.getInputStream();
out=newPrintStream(telnet.getOutputStream());
//根據root用戶設置結束符
this.prompt=user.equals("root")?'#':'>';
login(user,password);
}catch(Exceptione){
e.printStackTrace();
}
}

/**
*登錄
*
*@paramuser
*@parampassword
*/
publicvoidlogin(Stringuser,Stringpassword){
//read()Until("login:");
readUntil("login:");
write(user);
readUntil("Password:");
write(password);
readUntil(prompt+"");
}

/**
*讀取分析結果
*
*@parampattern
*@return
*/
publicStringreadUntil(Stringpattern){
try{
charlastChar=pattern.charAt(pattern.length()-1);
StringBuffersb=newStringBuffer();
charch=(char)in.read();
while(true){
sb.append(ch);
if(ch==lastChar){
if(sb.toString().endsWith(pattern)){
returnsb.toString();
}
}
ch=(char)in.read();
System.out.print(ch);
}
}catch(Exceptione){
e.printStackTrace();
}
returnnull;
}

/**
*寫操作
*
*@paramvalue
*/
publicvoidwrite(Stringvalue){
try{
out.println(value);
out.flush();
}catch(Exceptione){
e.printStackTrace();
}
}

/**
*向目標發送命令字元串
*
*@paramcommand
*@return
*/
publicStringsendCommand(Stringcommand){
try{
write(command);
returnreadUntil(prompt+"");
}catch(Exceptione){
e.printStackTrace();
}
returnnull;
}

/**
*關閉連接
*/
publicvoiddisconnect(){
try{
telnet.disconnect();
}catch(Exceptione){
e.printStackTrace();
}
}

publicstaticvoidmain(String[]args){
// TelnetClienttelnet=newTelnetClient();
try{
Shellshe=newShell("192.168.1.10",23,"root","123456");
System.out.println(she);
//執行的命令
System.out.println(she.sendCommand("ll"));
she.disconnect();

}catch(Exceptione){
//TODO:handleexception
}

}
}

『陸』 java代碼訪問telnet,並受到返回值

那就自己重寫方法,把返回的CMD信息字元串截取,截取你想要的信息。做一個工具類,直接調用工具類方法就行了,然後你還可以把工具類放到CSDN上賺積分,哈哈。

『柒』 如何用java實現telnet的登錄及實現命令

參考一下代碼:

用telnet是這樣:telnet time-A.timefreq.bldrdoc.gov 13

用socket是這樣:

1. import java.io.*;
2. import java.net.*;
3.
4. /**
5. This program makes a socket connection to the atomic clock
6. in Boulder, Colorado, and prints the time that the
7. server sends.
8. */
9. public class SocketTest
10. {
11. public static void main(String[] args)
12. {
13. try
14. {
15. Socket s = new Socket("time-A.timefreq.bldrdoc.gov",
16. 13);
17.
18. BufferedReader in = new BufferedReader
19. (new InputStreamReader(s.getInputStream()));
20. boolean more = true;
21. while (more)
22. {
23. String line = in.readLine();
24. if (line == null)
25. more = false;
26. else
27. System.out.println(line);
28. }
29.
30. }
31. catch (IOException e)
32. {
33. e.printStackTrace();
34. }
35. }
36. }

『捌』 java實現Telnet功能。

/*我想這就是你想要的telnet吧,既然我下面用的是apache開源包,你下來自己
*看看能不能自己重寫。方法已經給你提供了。
*要是可以的話結題吧.哈O(∩_∩)
*/
import java.io.InputStream;
import java.io.PrintStream;
import org.apache.commons.net.telnet.TelnetClient;

/**
* 利用apache net 開源包,使用telnet方式獲取AIX主機信息
* @version 1.2
*/
public class NetTelnet {

// Telnet對象
private TelnetClient telnet = new TelnetClient();

private InputStream in;

private PrintStream out;

// 提示符。具體請telnet到AIX主機查看
private char prompt = '#';

// telnet埠
private String port;

// 用戶
private String user;

// 密碼
private String password;

// IP地址
private String ip;

public NetTelnet() {

try {
// AIX主機IP
this.ip = "219.243.12.10";
this.password = "05933663007";
this.user = "administrator";
this.port = "23";
telnet.connect(ip, Integer.parseInt(port));
System.out.println("開始獲取輸入流...");
in = telnet.getInputStream();
out = new PrintStream(telnet.getOutputStream());
// 登錄
/* readUntil("login: ");
write(user);
readUntil("Password: ");
write(password);
readUntil(prompt + " ");*/
} catch (Exception e) {
e.printStackTrace();
}
}

/**
* 讀取分析結果
*
* @param pattern
* @return
*/
public String readUntil(String pattern) {
try {
char lastChar = pattern.charAt(pattern.length() - 1);
StringBuffer sb = new StringBuffer();

char ch = (char) in.read();
while (true) {

sb.append(ch);
if (ch == lastChar) {
if (sb.toString().endsWith(pattern)) {
return sb.toString();
}
}
ch = (char) in.read();
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}

/**
* 寫
*
* @param value
*/
public void write(String value) {
try {
out.println(value);
out.flush();

} catch (Exception e) {
e.printStackTrace();
}
}

/**
* 向目標發送命令字元串
*
* @param command
* @return
*/
public String sendCommand(String command) {
try {
write(command);
return readUntil(prompt + " ");
} catch (Exception e) {
e.printStackTrace();
}
return null;
}

/**
* 關閉連接
*
*/
public void disconnect() {
try {
telnet.disconnect();
} catch (Exception e) {
e.printStackTrace();
}
}

public static void main(String[] args) {
try {
System.out.println("開始執行telnet......");
NetTelnet telnet = new NetTelnet();
// 通過aix的命令「查找主機名稱」獲取數據
// 命令是 "hostname"
// 不熟悉命令的參考<<AIX網路管理手冊>>
System.out.println("開始發送hostname命令");
String result = telnet.sendCommand("hostname");
System.out.println("顯示結果");
System.out.println(result);
// 最後一定要關閉
telnet.disconnect();
} catch (Exception e) {
e.printStackTrace();
}
}
}

閱讀全文

與javatelnet客戶端相關的資料

熱點內容
編程和運營哪個更適合創業 瀏覽:893
尤里x怎麼升級 瀏覽:399
做業務績效考核需要哪些數據 瀏覽:433
dnf85版本劍魔刷圖加點 瀏覽:407
手機硬碟測試架可以讀取哪些數據 瀏覽:704
ug前後處理結算結果找不到文件 瀏覽:769
網頁框架拆分代碼 瀏覽:382
未來十年網路安全有什麼影響 瀏覽:362
win10更新後進不了劍靈 瀏覽:243
iphone471激活出錯 瀏覽:648
怎麼把文件拷到u盤 瀏覽:620
中伊簽署文件視頻 瀏覽:661
電信光寬頻網路不穩定 瀏覽:504
網路崗軟路由 瀏覽:995
黑莓z10在哪裡下載app 瀏覽:310
net批量下載文件 瀏覽:696
怎麼把蘋果一體機文件拷貝 瀏覽:117
sql文件怎麼寫 瀏覽:9
帝豪ec718導航升級 瀏覽:257
如何運用編程進行統計 瀏覽:570

友情鏈接