大家帮我看看这段JB下连接交换机的程序有什么错误。
erupt 2005-05-14 10:20:41 //在JB下Telnet连接交换机
public void checkport()
{
try{
if (connect("192.168.253.3", 23) == 1) {
int error;
String sendW = new String();
error = waits("assword:");
if (error < 1) {
this.jTextArea1.append("交换机连接出错");
}
sendW = "cisco";//密码
for (int i = 0; i < sendW.length(); i++) {
output.write(sendW.charAt(i));
output.flush();
}
if (error < 1) {
this.jTextArea1.append("交换机连接出错");
}
error = waits(">");
if (error < 1) {
this.jTextArea1.append("交换机连接出错");
}
sendW = "en";//输入enable,交换机下要求的命令
for (int i = 0; i < sendW.length(); i++) {
output.write(sendW.charAt(i));
output.flush();
}
if (error < 1) {
this.jTextArea1.append("交换机连接出错");
}
error = waits("assword:");
if (error < 1) {
this.jTextArea1.append("交换机连接出错");
}
sendW = "cisco";//再次输入密码
for (int i = 0; i < sendW.length(); i++) {
output.write(sendW.charAt(i));
output.flush();
}
error = waits("#");
if (error < 1) {
this.jTextArea1.append("交换机连接出错");
}
sendW = "sh interfaces status";//输入交换机命令
for (int i = 0; i < sendW.length(); i++) {
output.write(sendW.charAt(i));
output.flush();
}
} else
this.jTextArea1.append("交换机不可达");
}catch(IOException e)
{this.jTextArea1.append(e.toString());}
}
//connect the switch
public int connect(String swName,int port)
{
int k=1;
try{
sk=new Socket(swName,port);
input=new TelnetInputStream(sk.getInputStream(),true);
output=new TelnetOutputStream(sk.getOutputStream(),true);
}catch(IOException e){k=-1;}
return k;
}
public int timeout = 50000;//等待延迟
//等待交换机响应
public int waits(String prompt)
{
String tmp = "";
String tmpmess=tmp;
int nt=1;
do {
try { tmp = new String(receive()); }
catch(Exception e) { this.jTextArea1.append(e.toString());return -1; }
nt=nt+1;
if (nt>timeout){return 0; }
if (tmp.length()>0)
{
tmpmess=tmp;
}
} while(tmp.indexOf(prompt) == -1);
this.jTextArea1.append(tmpmess);
return 1;
}
//接受来自交换机的信息
public byte[] receive() throws IOException {
int count = input.available();
byte buf[] = new byte[count];
count = input.read(buf);
String x=new String(buf);
return buf;
}
输出结果:User Access Verification
Password: 交换机连接出错交换机连接出错交换机连接出错交换机连接出错
好像是inputstream不太对。交换机接受不到密码。所以出错的。可是我找不到错误在哪里,能否帮我看看。谢谢了!