大家帮我看看这段JB下连接交换机的程序有什么错误。

erupt erupt 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
//在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不太对。交换机接受不到密码。所以出错的。可是我找不到错误在哪里,能否帮我看看。谢谢了!
...全文
评论 3
收藏 已收藏
分享
返回顶部
erupt
Java 51,407 成员 86,130 内容