LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-27-2008, 05:57 PM   #1
clb
Member
 
Registered: Sep 2004
Location: UK
Distribution: Ubuntu
Posts: 117

Rep: Reputation: 16
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
 
Old 12-29-2008, 11:12 AM   #2
jcookeman
Member
 
Registered: Jul 2003
Location: London, UK
Distribution: FreeBSD, OpenSuse, Ubuntu, RHEL
Posts: 417

Rep: Reputation: 33
Can you post some code and show which method is not returning? I assume you are using org.apache.commons.net.telnet?
 
Old 12-29-2008, 01:09 PM   #3
clb
Member
 
Registered: Sep 2004
Location: UK
Distribution: Ubuntu
Posts: 117

Original Poster
Rep: Reputation: 16
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.

Last edited by clb; 12-29-2008 at 01:35 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
a problem with continuous dropping of my internet connection Brynn Linux - Networking 1 07-18-2008 02:58 PM
telnet supplying arguments to telnet command nasirdaudahmad Linux - Networking 2 04-22-2007 03:12 AM
telnet connection refused Marcusj2 Linux - Networking 22 09-07-2006 10:35 PM
remote command execute telnet with 1 command MPowers Linux - Networking 2 06-30-2005 06:31 AM
telnet: Connection refused hnash53 Linux - Networking 2 01-23-2004 02:12 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:32 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration