Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Java Telnet Client

Options
  • 12-11-2005 6:27pm
    #1
    Registered Users Posts: 326 ✭✭


    Hey everyone,

    I need help with a basic Java telnet client as part of my final year project. I have found bundles and bundles of code out there that claims to work with telnet but I cant seem to get any of them to work. They all login alright but getting it to execute a command and return the output seems next to impossible, although I know it shouldnt be.

    I'm wondering if anyone here has any small example of Java logging into a telnet server, issuing a command, outputting returned data from server and then disconnecting.

    All I need is a simple class to connect, run command and output results but I cant find any examples of what I want. Plenty of API's and wrappers but very few examples of how to use them and I find API docs very hard to follow without examples.

    Any help greatly appreciated as I need to figure this out before next week as I must make a discision between C# and Java for my project. Java is in the lead atm because I have gotten Java to SET SNMP MIB Objects.

    Conor.


Comments

  • Closed Accounts Posts: 110 ✭✭Adblock


    I have never programmed a Java telnet client so i cant help much but i saw this a few days ago so i said id tell you

    http://www.mud.de/se/jta/html/AppletTest.html
    or [same group]
    http://javassh.org/space/start

    its an open source Java telnet/ssh. it might be of some use


  • Registered Users Posts: 4,003 ✭✭✭rsynnott


    You shouldn't be using telnet. Use SSH.


  • Closed Accounts Posts: 1,502 ✭✭✭MrPinK


    rsynnott wrote:
    You shouldn't be using telnet. Use SSH.
    If I had to write my own as part of my project, I'd go with telnet. SSH is a lot harder to implement and would nearly be a project in itself.


  • Registered Users Posts: 4,003 ✭✭✭rsynnott


    MrPinK wrote:
    If I had to write my own as part of my project, I'd go with telnet. SSH is a lot harder to implement and would nearly be a project in itself.

    Since he's looking for client libraries, though, it seems that he DOESN'T have to write his own.


  • Registered Users Posts: 885 ✭✭✭clearz


    I have been running a game server on a linux box for a while which I update quite often. When I update something I have to log in through Putty/SSH find the Process ID (ps x) and kill the server process (kill -9 <PID>). Then restart it after the changes have been made. I got bored of using the shell to do this fairly quick so I downloaded a SSH library for Java and created a simple GUI app that does this job for me and sits in my taskbar until needed. It took me an hour to do this and would have probaby have taking the same using telnet.

    My point is that if you can implement SSH/Telnet in Java in an hour its harldy sutible for a final year project.


  • Advertisement
  • Registered Users Posts: 4,003 ✭✭✭rsynnott


    clearz wrote:
    I have been running a game server on a linux box for a while which I update quite often. When I update something I have to log in through Putty/SSH find the Process ID (ps x) and kill the server process (kill -9 <PID>).

    That isn't SERIOUSLY how you have to kill the poor little server, is it?


  • Registered Users Posts: 885 ✭✭✭clearz


    rsynnott wrote:
    That isn't SERIOUSLY how you have to kill the poor little server, is it?
    It sure is. And then I call round to the server center and kick the living sh1t outa the hardware for good messures. SERIOUSLY but rsynnott since you SERIOUSLY seem to have the knowlege can you please enlighten me on how to kill a process in linux without potentially wiping out half of Europe's population as your SERIOUSLY sensationalist post suggests might happen.


  • Registered Users Posts: 4,003 ✭✭✭rsynnott


    clearz wrote:
    It sure is. And then I call round to the server center and kick the living sh1t outa the hardware for good messures. SERIOUSLY but rsynnott since you SERIOUSLY seem to have the knowlege can you please enlighten me on how to kill a process in linux without potentially wiping out half of Europe's population as your SERIOUSLY sensationalist post suggests might happen.

    'kill -9' is a last resort and should only be used when nothing else works. 'kill' will send a SIGTERM, and works quite nicely. The program can (and usually does) catch the SIGTERM, and commit suicide correctly and safely. And no need to get annoyed...


  • Registered Users Posts: 7,498 ✭✭✭BrokenArrows


    clearz wrote:
    It sure is. And then I call round to the server center and kick the living sh1t outa the hardware for good messures. SERIOUSLY but rsynnott since you SERIOUSLY seem to have the knowlege can you please enlighten me on how to kill a process in linux without potentially wiping out half of Europe's population as your SERIOUSLY sensationalist post suggests might happen.


    now thats scarcasim


  • Moderators, Education Moderators Posts: 1,863 Mod ✭✭✭✭Slaanesh


    In response to the OP:
    import org.apache.commons.net.telnet.*;
    import java.io.*;
    
    public class runTelnetCommands
    {
    	TelnetClient telnet = null;
    	InputStream in = null;
    	PrintStream out = null;
    	public runTelnetCommands()
    	{
    		telnet = new TelnetClient();
    		try
    		{
    			telnet.connect("192.168.0.1",2222);
    			// Get input and output stream references
    			in = telnet.getInputStream();
    	 		out = new PrintStream( telnet.getOutputStream() );
    
    	 		// Log the user on
    	 		readUntil( "login: " );
    
    	 		write("username");
    	 		readUntil( "Password: " );
    
    	 		write("password");
    	 		readUntil( "$ " );
    
    	 		write("su");
    	 		readUntil("Password: ");
    
    	 		write("supass");
    	 		readUntil("# ");
    	 		
    	 		write("ls");
    	 		readUntil( "# " );
    
    	 		telnet.disconnect();
    		}
    		catch(Exception e)
    		{
    			System.out.println(e);
    		}
    	}
    	public void write( String value )
    	{
    		try
    		{
    			out.println( value );
    			out.flush();
    			System.out.println( value );
    		}
    		catch( Exception e )
    		{
    			e.printStackTrace();
    		}
    	}
      public String readUntil( String pattern )
      {
       try
       {
    	 char lastChar = pattern.charAt( pattern.length() - 1 );
    	 StringBuffer sb = new StringBuffer();
    	 boolean found = false;
    	 char ch = ( char )in.read();
    	 while( true ) {
    	  System.out.print( ch );
    	  sb.append( ch );
    	  if( ch == lastChar ) {
    	    if( sb.toString().endsWith( pattern ) ) {
    		 return sb.toString();
    	    }
    	  }
    	  ch = ( char )in.read();
    	 }
       }
       catch( Exception e ) {
    	 e.printStackTrace();
       }
       return null;
      }
    
    	public static void main(String args[])
    	{
    		runTelnetCommands rtc = new runTelnetCommands();
    	}
    }
    
    
    

    This code sample uses the apache commons.net API, google it.

    I wrote this today because I needed a program to set the date/time on a linux box remotely.

    The TelnetClient extends SocketClient. The connect method is used to connect to a telnet server.

    The thing about my approach is that I know what response I am going to get from the server, the readUntil() method reads from the InputStream until it reaches a specified String.

    So you can see from the example that I wait for the "username" String until I write the required String to the output stream.

    If you want to write a client, you can have a Thread that constantly reads from the input stream and then another thread to write commands to the output stream.

    No biggie.


  • Advertisement
  • Registered Users Posts: 326 ✭✭schrodinger


    Cheers for that Slaanesh,

    I actually had looked at Jakarta Commons Net before but I seemed to have some mad example that wouldnt compile but I got one to work for me now.

    Really impressed with Jakarta commons - sorry I had never seen it before, some of my previous projects could have been better and alot easier. ;)

    I have this stuck up in Net/Comms but I may aswell throw it up here too.

    I have a questionnaire for my project that I need to get end users to fill in (blame my lecturer) ;), so if anyone here uses Cisco would they mind filling it in.

    Post is here:
    http://www.boards.ie/vbulletin/showthread.php?t=2054849993

    And the questionnaire is here:
    http://www.it-netsoc.ie/~schrodinger/cisco/q.php

    Thanks again Slaanesh.
    rsynnott wrote:
    You shouldn't be using telnet. Use SSH.
    I an using telnet because "most" Cisco router will run telnet and my lectuer told to concentrate on telnet.


  • Registered Users Posts: 885 ✭✭✭clearz


    rsynnott wrote:
    'kill -9' is a last resort and should only be used when nothing else works. 'kill' will send a SIGTERM, and works quite nicely. The program can (and usually does) catch the SIGTERM, and commit suicide correctly and safely. And no need to get annoyed...


    OK tnx I never knew that. Ill be sure to use kill in future.


Advertisement