我正在使用 Java ganymed 库通过 SSH2 连接到我们的交换机。我可以毫无问题地连接 Catalyst 交换机,但是当我连接到 Nexus 交换机时,我无法从命令中获得任何输出。
有人用过这个库来连接 Nexus 交换机吗?
具体代码部分如下:
try {
Connection conn = new Connection(IP);
conn.connect();
boolean isAuthenticated = conn.authenticateWithPassword(username, password);
if (isAuthenticated) {
Session sess = conn.openSession();
sess.startShell();
InputStream stdout = new StreamGobbler(sess.getStdout());
BufferedReader br = new BufferedReader(new InputStreamReader(stdout));
OutputStream stdin = new BufferedOutputStream(sess.getStdin());
BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(stdin));
bw.write("sh cdp ne");
bw.flush();
stdin.write(13);
stdin.flush();
bw.write("exit");
bw.flush();
stdin.write(13);
stdin.flush();
while (true)
{
String line = br.readLine();
System.out.println(line);
if (line == null)
break;
}
} // close if (isAuthenticated)
} // close try
最佳答案
显然问题是“没有分配伪终端,而你的命令 cdp 需要一个。”正如@SubOptimal 警告的那样。 所以我为 session 分配了一个伪终端,问题就解决了:
Session sess = conn.openSession();
sess.requestDumbPTY(); // Allocate a pseudo-terminal for this session.
sess.startShell(); // Start a shell on the remote machine.
感谢大家的评论。
关于java - SSH 到 Cisco Nexus 交换机,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35620604/