0

I have developed a Java program using Telnet to establish connections with switches and send commands to them. However, when executing the program, I'm encountering an issue where I am not receiving any output from the switches. Despite successfully establishing the Telnet connection, the program doesn't display the expected response or output from the switches. I have verified the connectivity and credentials, but still, no output is shown. I'm wondering what could be causing this problem and how I can resolve it. Your insights and suggestions for troubleshooting would be greatly appreciated. Below is my code

/*
 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
 * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Main.java to edit this template
 */
package telnetclient;


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintStream;
import org.apache.commons.net.telnet.TelnetClient;

public class Telnet {

    private static final String USERNAME_PROMPT = "login:";
    private static final String PASSWORD_PROMPT = "Password:";
    private static final String COMMAND_PROMPT = "#";

    public static void executeRemoteCommands(String host, int port, String username, String password) {
        try {
            TelnetClient telnet = new TelnetClient();
            telnet.connect(host, port);

            InputStream in = telnet.getInputStream();
            PrintStream out = new PrintStream(telnet.getOutputStream());

            // Read until username prompt
            readUntil(in, USERNAME_PROMPT);

            // Send username
            write(out, username);

            // Read until password prompt
            readUntil(in, PASSWORD_PROMPT);

            // Send password
            write(out, password);

            // Read until command prompt
            readUntil(in, COMMAND_PROMPT);

            // Execute commands
            BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
            String command;
            while (true) {
                // Prompt for command
                System.out.print("Enter a command (or 'exit' to quit): ");
                command = reader.readLine();
                if (command.equalsIgnoreCase("exit")) {
                    break;
                }

                // Send command
                write(out, command);

                // Read command output
                String output = readUntil(in, COMMAND_PROMPT);

                // Print the output
                System.out.println(output);
            }

            // Disconnect
            telnet.disconnect();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static String readUntil(InputStream in, String pattern) throws IOException {
        StringBuilder sb = new StringBuilder();
        byte[] buffer = new byte[1024];
        int bytesRead;
        while ((bytesRead = in.read(buffer)) > -1) {
            String chunk = new String(buffer, 0, bytesRead);
            sb.append(chunk);
            if (chunk.contains(pattern)) {
                return sb.toString();
            }
        }
        return sb.toString();
    }

    private static void write(PrintStream out, String value) {
        out.println(value);
        out.flush();
    }

    public static void main(String[] args) {
        String host = "192.168.3.3";
        int port = 23;
        String username = "moazan";
        String password = "moazan123456";

        executeRemoteCommands(host, port, username, password);
    }
}

I attempted to run my Telnet program to connect to switches and send commands. I expected to receive the output or response from the switches corresponding to the executed commands. However, the program did not display any output from the switches, despite establishing the Telnet connection successfully.

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
  • I would advise you to study the correct usage of the API you're using, or you're likely to get blocking behaviour on the output you're reading, which will result in locking of your main thread and the 'no output' thing you're talking about. e.g. there is [this](https://commons.apache.org/proper/commons-net/apidocs/org/apache/commons/net/telnet/TelnetInputListener.html) – g00se May 18 '23 at 11:05
  • You are not using 'Telnet' the command line tool but the Apache Commons TelnetClient class. – aled May 18 '23 at 11:26
  • Also make sure your prompt is exactly what you think it is. Your code works for me once I change the prompt – g00se May 18 '23 at 11:59
  • yeah thanks it was prompt issue. now i just need to make a dashboard frontend. i am not very good at it what do you think is best for frontend in java – Muzan Kibutsuji May 19 '23 at 21:23

0 Answers0