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
}
}
}