1、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();
}
}
}
2、telnet客戶端和telnet伺服器有何區別?
TELNET是一個服務
客戶端 是用來連接伺服器來請求數據的
他們的區別 就和 網站伺服器 與 IE 瀏覽器得關系
3、我想通過JAVA程序實現讓訪問我的客戶端執行telnet命令(我是伺服器端),請問能實現么
本質還是soket編程,至於你執行什麼命令這個你完全都可以定義,因為這個是你自己的程序負責解析的命令,沒有什麼關系
4、如何用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. }
5、急,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 命令
import java.io.InputStream;6、java中telnet到遠程伺服器,然後執行cmd命令,求代碼,謝謝了
package place.in.javaeye;
import java.io.InputStream;
import java.io.PrintStream;
import org.apache.commons.net.telnet.TelnetClient;
public class Shell
{
private TelnetClient telnet = new TelnetClient();
private InputStream in;
private PrintStream out;
private char prompt = '$';// 普通用戶結束
public Shell(String ip, int port, String user, String password)
{
try
{
telnet.connect(ip, port);
in = telnet.getInputStream();
out = new PrintStream(telnet.getOutputStream());
// 根據root用戶設置結束符
this.prompt = user.equals("root") ? '#' : '>';
login(user, password);
}
catch (Exception e)
{
e.printStackTrace();
}
}
/**
* 登錄
*
* @param user
* @param password
*/
public void login(String user, String password)
{
// readUntil("login:");
readUntil("login:");
write(user);
readUntil("Password:");
write(password);
readUntil(prompt + "");
}
/**
* 讀取分析結果
*
* @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();
System.out.print(ch);
}
}
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) {
TelnetClient telnet = new TelnetClient();
try {
Shell she =new Shell("10.**.***.***", 23, "***", "***");
System.out.println(she);
System.out.println(she.sendCommand("ls"));
she.disconnect();
}catch (Exception e) {
// TODO: handle exception
}
}
}
7、java如何用socket實現telnet功能?急!!!
樓主,apache 的 common-net是這樣的客戶端,官網也有例子
org.apache.commons.net.telnet.TelnetClient;
8、利用java來telnet到某個伺服器的程序運行不了,如何修改?
您好,您這樣:
package place.in.javaeye;
import java.io.InputStream;
import java.io.PrintStream;
import org.apache.commons.net.telnet.TelnetClient;
public class Shell
{
private TelnetClient telnet = new TelnetClient();
private InputStream in;
private PrintStream out;
private char prompt = '$';// 普通用戶結束
public Shell(String ip, int port, String user, String password)
{
try
{
telnet.connect(ip, port);
in = telnet.getInputStream();
out = new PrintStream(telnet.getOutputStream());
// 根據root用戶設置結束符
this.prompt = user.equals("root") ? '#' : '>';
login(user, password);
}
catch (Exception e)
{
e.printStackTrace();
}
}
/**
* 登錄
*
* @param user
* @param password
*/
public void login(String user, String password)
{
// readUntil("login:");
readUntil("login:");
write(user);
readUntil("Password:");
write(password);
readUntil(prompt + "");
}
/**
* 讀取分析結果
*
* @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();
System.out.print(ch);
}
}
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) {
TelnetClient telnet = new TelnetClient();
try {
Shell she =new Shell("10.**.***.***", 23, "***", "***");
System.out.println(she);
System.out.println(she.sendCommand("ls"));
she.disconnect();
}catch (Exception e) {
// TODO: handle exception
}
}
}