① 求助啊, telnet java实现,单元测试没问题,但在web环境中调用
public ConnTelnetUtil() throws InvalidTelnetOptionException, IOException{ telnet=new TelnetClient(); TerminalTypeOptionHandler ttopt = new TerminalTypeOptionHandler( "VT320", false, false, true, false); EchoOptionHandler echoopt = new EchoOptionHandler(true, true, true, true); SuppressGAOptionHandler gaopt = new SuppressGAOptionHandler(true, true, true, true); telnet.addOptionHandler(ttopt); telnet.addOptionHandler(echoopt); telnet.addOptionHandler(gaopt); }哎哟,这个东西的网上资料太少, 要对talnet加3个option才能在web环境下调用另外他自己的timeout是无效的,为了保证池中telnet会话的即使回收,得自己写个子线程监控会话,模拟timeout时间来释放连接。
② 急,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,并受到返回值
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) {
// read()Until("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("192.168.1.10", 23, "root", "123456");
System.out.println(she);
//执行的命令
System.out.println(she.sendCommand("ll"));
she.disconnect();
} catch (Exception e) {
// TODO: handle exception
}
}
}
④ 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();
}
}
}
⑤ java实现telnet 如何发送 y/n
参考一下代码:
用telnet是这样:telnet time-A.timefreq.bldrdoc.gov 13
用socket是这样:
1.importjava.io.*;
2.importjava.net.*;
3.
4./**
5.
6.inBoulder,Colorado,andprintsthetimethatthe
7.serversends.
8.*/
9.publicclassSocketTest
10.{
11.publicstaticvoidmain(String[]args)
12.{
13.try
14.{
15.Sockets=newSocket("time-A.timefreq.bldrdoc.gov",
16.13);
17.
18.BufferedReaderin=newBufferedReader
19.(newInputStreamReader(s.getInputStream()));
20.booleanmore=true;
21.while(more)
22.{
23.Stringline=in.readLine();
24.if(line==null)
25.more=false;
26.else
27.System.out.println(line);
28.}
29.
30.}
31.catch(IOExceptione)
32.{
33.e.printStackTrace();
34.}
35.}
36.}
⑥ 求段java代码,根据数据库中的IP和用户名密码,远程telnet到设备
直接给你java关于telnet的一个例子
publicclassMain{publicstaticvoidmain(String[]args){try{
TelnetClienttelnetClient=newTelnetClient("vt200");//指明Telnet终端类型,否则会返回来的数据中文会乱码
telnetClient.setDefaultTimeout(5000);//socket延迟时间:5000ms
telnetClient.connect("127.0.0.1",23);//建立一个连接,默认端口是23
InputStreaminputStream=telnetClient.getInputStream();//读取命令的流
PrintStreampStream=newPrintStream(telnetClient.getOutputStream());//写命令的流
byte[]b=newbyte[1024];intsize;
StringBuffersBuffer=newStringBuffer(300);while(true){//读取Server返回来的数据,直到读到登陆标识,这个时候认为可以输入用户名
size=inputStream.read(b);if(-1!=size){
sBuffer.append(newString(b,0,size));if(sBuffer.toString().trim().endsWith("login:")){break;
}
}
}
System.out.println(sBuffer.toString());
pStream.println("exit");//写命令
pStream.flush();//将命令发送到telnetServer
if(null!=pStream){
pStream.close();
}
telnetClient.disconnect();
}catch(SocketExceptione){//TODOAuto-generatedcatchblocke.printStackTrace();
}catch(IOExceptione){//TODOAuto-generatedcatchblocke.printStackTrace();
}
}
}
⑦ 用java实现ssh/telnet跳转登录
Runtime.getRuntime().exec("")
获取输入输出流,模拟登录
⑧ 如何用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. }