LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Apache Commons telnet connection with a continuous command (https://www.linuxquestions.org/questions/programming-9/apache-commons-telnet-connection-with-a-continuous-command-693348/)

clb 12-27-2008 05:57 PM

Apache Commons telnet connection with a continuous command
 
Hi all.

Im currently trying to write an application that connects to a telnet server and tails a log file.
What I hope to achieve is to check new data as it arrives, record timings of various jobs and play an audible alert when certain phrases are picked up in the file.

I have created a telnet class to send and receive data using the Apache Commons libraries, this class works perfectly. I am able to use the application as an interface to the server, send commands and display the output.
The problem I am having is that when the tail -f command is issued, it never gets to the end of the output so the method never returns control to the main class. If a command like 'ls' is issued there is no problems.

I have a suspicion that the answer may be to use threads and have the command run in a seperate thread which can be killed when desired, but I know very little about threads. Does this seem a reasonable approach?

What I would really like if possible is a way of doing essentially the following:

Code:

while (connected)
{
    if(newDataReceived)
    {
          parseNewData();
          appendNewDataToTextArea();
    }
}

and to have that running until the disconnect button is pressed, thus ending the loop.
I have seen an SSH/Telnet class that seems to have a method that does essentially this (I believe it's a commercial offering by JScape), but the budget for this project is 0 and they want $599 for licencing.

If anyone has any advice on this then I would be grateful.

Thanks,
Chris

jcookeman 12-29-2008 11:12 AM

Can you post some code and show which method is not returning? I assume you are using org.apache.commons.net.telnet?

clb 12-29-2008 01:09 PM

The method that is being used to send the command is:
Code:

public String sendCommand(String command)
    {
        try
        {
            write(command);
            return readUntil(prompt + " ");
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return null;
    }

The write method it refers to is:
[CODE]
public void write(String value)
{
try
{
out.println(value);
out.flush();
}
catch (Exception e)
{
e.printStackTrace();
}
}
[/CODE}

Which is called by:
Code:

aOpenConnection = new AbstractAction("Open Connection")
        {

            public void actionPerformed(ActionEvent e)
            {
                try
                {
                  telnetConn =  new Telnet(SERVER, USER, PASSWORD);
                    outputPane.setText(telnetConn.sendCommand("tail -f " + LOGFILE));
                }
                catch (Exception ex)
                {
                    ex.printStackTrace();
                }
            }
        };

The code itself probably isn't particularly great, this is just a proof-of-concept program which will be refined if I can prove it works, and therefore justify spending works time on developing it.


All times are GMT -5. The time now is 03:15 PM.