java - SSH 到 Cisco Nexus 交换机

标签 java cisco

我正在使用 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/

相关文章:

java - 使用 Java 对链表进行递归合并排序

python - 使用 python 查找具有特定配置的 Cisco IOS 接口(interface)

java - 如何向 Cisco Contact Center Express Identity Service 进行身份验证?

regex - Tcl:如何在多行字符串中有条件地替换

java - 保存 imageButton 可见性 [Android]

java - 在 Apache POI 中应用价格和百分比格式

linux - 无法通过 cron 连接到 Anyconnect

php - 使用 SNMP WALK 获取一个端口上的 MAC 地址列表

java - 为什么必须在 Java 中始终初始化包括原语在内的局部变量?

java - 如何将值映射回枚举?