如何通过java运行"show interfaces status"到思科/阿尔卡特交换机(如果可能的话,jsch)



我需要通过java(如果可能的话,jsch(将show interfaces status运行到Cisco/AAlcatel交换机

我使用SSH访问此交换机。在我尝试之后

Channel channel = session.openChannel("exec");
((ChannelExec) channel).setCommand(command);
channel.setInputStream(null);
((ChannelExec) channel).setErrStream(System.err);
channel.setOutputStream(System.out);
InputStream in = channel.getInputStream();
channel.connect();

我尝试这个(如果我在控制台中手动运行命令,可以(

Channel channel = session.openChannel("shell");
channel.setInputStream(null);
channel.connect();

我需要连接、发送命令并记录命令的结果。如果我手动执行,结果是可以的。我在虚拟机中尝试代码,结果与物理交换机不同。

你可以试试这个:

Channel channel=null;
String result = null;
try
{
channel = session.openChannel("exec");
String Command = "show interfaces status";   // Command
//System.out.println("Command : "+Command);
((ChannelExec)channel).setCommand(Command);
InputStream in=channel.getInputStream();
byte[] tmp=new byte[1024];
channel.connect();
if(channel.isConnected())
{
//System.out.println("Channel connected");
}
while(true)
{    // geeting the result in this loop
while(in.available()>0)
{
int i=in.read(tmp, 0, 1024);
if(i<0)break;
result += new String(tmp, 0, i);    // result
}
if(channel.isClosed())
{
break;
}
else
{
try{ Thread.sleep(1000); }catch(Exception ee){}
}
}
}
catch(JSchException e)
{
// your exception message
}

相关内容

最新更新