急求java实现telnet登陆全过程的源码

mxx123 2009-06-10 10:59:29
小弟急求java实现telnet登陆全过程的源码!
我目前简单写了下,可是总通不过,以下就是我写的了

import java.io.*;
import java.net.Socket;

public class TelnetExample {
public static int hand(InputStream is, OutputStream os) throws IOException {
while(true) {
int ch = is.read();
if(ch < 0 || ch != 255)
return ch;
int cmd = is.read();
int opt = is.read();
switch(opt) {
case 1: // echo协商选项,本程序未处理
break;
case 3: // supress go-ahead(抑制向前选项)
break;
case 24: // terminal type(终端类型选项)
if(cmd == 253) {
os.write(255);
os.write(251);
os.write(24);

os.write(255);
os.write(250);
os.write(24);
os.write(0);
os.write("dumb".getBytes());
os.write(255);
os.write(240);
} else if (cmd == 250) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int svr = is.read();
while(svr != 240) {
baos.write(svr);
svr = is.read();
}
System.out.println("Svr:" + baos.toString());
}
break;
default:
if (cmd == 253) {
os.write(255);
os.write(252);
os.write(opt);
}
}
}
}
public static void main(String[] args) throws Exception {
Socket s = new Socket("192.168.180.20",23);
InputStream is = s.getInputStream();
OutputStream os = s.getOutputStream();
System.out.println(hand(is,os));

os.write("administrator".getBytes());
os.write("zxcvbnm123\n".getBytes());
BufferedReader br = new BufferedReader(new InputStreamReader(is));
char[] buf = new char[1024];
int read = br.read(buf);
while(read > 0) {
System.out.println(new String(buf,0,read));
read = br.read(buf);
}
}
}


上面你这段代码每次都是到让输入用户名密码这里过不去啊,控制台里显示“login: administratorzxcvbnm123”就卡住了,在控制台里输入其他信息也没有响应,求大虾们帮忙解决一下
能实现完整telnet登陆过程,并能在控制台交互就可以啊,如有更好的源程序也希望能提供下啊,谢谢了!
...全文
570 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
xiongzh19 2010-01-08
  • 打赏
  • 举报
回复
请问楼上org.apache.commons.net.telnet的这个包从哪里来的?我查看了下tomcat5的lib里面并没有发现,可以的话最好能提供下载地址,谢谢!
alpsdyk2001 2009-08-06
  • 打赏
  • 举报
回复


import org.apache.commons.net.telnet.*;
import java.io.*;


public class TmhTelnet {
private static TelnetClient tc = null;
private InputStream in;
private PrintStream out;
public static void main(String args[]) {
new TmhTelnet("192.168.0.37", 23, "administrator", "222");
}

public TmhTelnet(String host, int port, String username, String password) {
byte[] a = new byte[1024];
if (intconnect(host, port)) {
write(username);
write(password);
writescript("command.txt");//dir
closeconnect();

}
}

private void writescript(String filename) {
try {
if (new File(filename).exists()) {
FileReader f = new FileReader(filename);
BufferedReader reader = new BufferedReader(f);
String command = "";
while (command != null) {
command = reader.readLine();
write(command);
}
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

public void write(String command) {
try {
System.out.println("command:>>>>>>>>>" + command);
out.println(command);
out.flush();
printresponse2();
//for(int i=0;i<800000000;i++);
//应该使用线程同步使程序在接受到对方相应后再执行下一条命令这里使用for循环来模拟对方的相应时间
} catch (Exception e) {
e.printStackTrace();
}
}

private boolean intconnect(String host, int port) {
try {
tc = new TelnetClient();
TerminalTypeOptionHandler ttopt = new TerminalTypeOptionHandler(
"VT100", false, false, true, false);
EchoOptionHandler echoopt = new EchoOptionHandler(true, false,
true, false);
SuppressGAOptionHandler gaopt = new SuppressGAOptionHandler(true,
true, true, true);

tc.addOptionHandler(ttopt);
tc.addOptionHandler(echoopt);
tc.addOptionHandler(gaopt);
tc.connect(host, port);
in = tc.getInputStream();
out = new PrintStream(tc.getOutputStream());
return true;
} catch (Exception e) {
e.printStackTrace();
try {
tc.disconnect();
in.close();
out.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
return false;
}
}

private void closeconnect() {
try {
tc.disconnect();
in.close();
//out.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}

private String printresponse() {
try {
byte[] buff = new byte[1024];
int ret_read = 0;
do {
ret_read = in.read(buff);
String a = new String(buff, 0, ret_read);
System.out.print("=" + a);
if (a.endsWith(":") | a.endsWith(">") | a.endsWith("]")) {
System.out.print(a);
return null;
}

} while (ret_read >= 0);
} catch (Exception e) {
System.err.println("Exception while reading socket:"
+ e.getMessage());
}

try {
tc.disconnect();
} catch (Exception e) {
System.err.println("Exception while closing telnet:"
+ e.getMessage());
}
return null;
}

private String printresponse2() {
try {
byte[] buff = new byte[1024];
int ret_read = 0;
ret_read = in.read(buff);
while (ret_read >= 0) {
String a = new String(buff, 0, ret_read);
a = a.trim();
System.out.print("=" + a);
if (a.endsWith(":") | a.endsWith(">") | a.endsWith("]")) {
System.out.print(a);
return null;
}
}
} catch (Exception e) {
System.err.println("Exception while reading socket:"
+ e.getMessage());
}

try {
tc.disconnect();
} catch (Exception e) {
System.err.println("Exception while closing telnet:"
+ e.getMessage());
}
return null;
}

}

可以连接成功
[5;1H[K[6;1H[K[7;1H[K[8;1H[K[9;1H[K[10;1H[K[11;1H[K[12;1H[K[13;1H[K[14;1H[K[15;1H[K[16;1H[K[17;1H[K[18;1H[K[19;1H[K[20;1H[K
谁该看下
Linux校园社区 2009-06-11
  • 打赏
  • 举报
回复
Adebayor 2009-06-11
  • 打赏
  • 举报
回复
帮顶
mxx123 2009-06-10
  • 打赏
  • 举报
回复
哪位大侠有能实现的代码可以提供一下啊?急啊!
mxx123 2009-06-10
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 jackson416 的回复:]
楼主的代码不是同这个吗?

http://topic.csdn.net/t/20050606/15/4062913.html
[/Quote]
是啊,我就是想把这个家一些其他的功能啊!
jackson416 2009-06-10
  • 打赏
  • 举报
回复
楼主的代码不是同这个吗?

http://topic.csdn.net/t/20050606/15/4062913.html
healer_kx 2009-06-10
  • 打赏
  • 举报
回复
很简单,你知道知道telnet的协议就行了,很简单。然后UDP。
knightzhuwei 2009-06-10
  • 打赏
  • 举报
回复
参考 http://www.javaeye.com/problems/957
yanhan0615 2009-06-10
  • 打赏
  • 举报
回复
因为你的整个程序声明周期在第一次连接完毕的时候就消亡了,所以只能执行这个过程,具体代码我没有仔细研究,但是肯定要加上一个线程来循环处理,应该就可以实现你的需要了
sforiz 2009-06-10
  • 打赏
  • 举报
回复
UP
mxx123 2009-06-10
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 mxx123 的回复:]
另求,把网络的文件输入输出流的数据,存为本地二进制文件的方法
[/Quote]
更正一下,是求网络的输入输出流的数据,存为本地二进制文件的方法!而不是把某个容器控件内的文本存为文件!
mxx123 2009-06-10
  • 打赏
  • 举报
回复
另求,把网络的文件输入输出流的数据,存为本地二进制文件的方法
czp3158 2009-06-10
  • 打赏
  • 举报
回复
顶一个学习……
zhouxingyu896 2009-06-10
  • 打赏
  • 举报
回复
学习

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧