我需要通过java (如果可能的话,jsch)运行show interfaces status到Cisco/Alcatel switch
我使用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();我需要连接,发送命令,并记录命令的结果。如果我手动执行它,结果是OK的。我在虚拟机中尝试我的代码,结果与物理交换机不同。
发布于 2018-10-29 17:51:35
您可以尝试这样做:
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
}https://stackoverflow.com/questions/53042620
复制相似问题