Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
Win a copy of Head First Agile this week in the Agile forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Auto Telent logging..Plz help  RSS feed

 
Madhusudana Rao
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,
I am very much new to Java programming.I am looking for some help in writing some telnet code wher the program will directly logs into the remote system and writes the commands i w'd like to and directs output to some text file.No GUI/Swing components are required.Please help me out.
Thanks in advance.
 
David Harkness
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You'll want to use a java.net.Socket to connect to the telnet server (I believe port 23, but double check). The Socket exposes an OutputStream for sending your commands, and you may want to wrap it with a PrintWriter to make it easier. The Socket also has an InputStream from which you can read the output of the commands.

You can use a FileWriter to write that output to a File, and you might find using a PipedInput/OutputStream to be useful since that's effectively what you're doing. You want to pipe everything coming from the Socket's InputStream to the File's OutputStream.

Read the JavaDocs for each of these classes, give it a try, and let us know where you get to. Try to complete one step at a time before moving on to the next.
 
Madhusudana Rao
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi David,
Thanks for your reply.Actually I am succesfull in connecting to the remote server using socket,but the problem is the connection is getting closed before i write something on the socket using printwriter.This happens when i send it as character or string stream..and if i send the userid/password details as bytes the information is writing on the socket after every bytes sent by the remote server which obviously ending with authentication failed response from the remote system.Please help.
 
David Harkness
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you post some of the code you're using to set up and run the connection? Without that, I can't even guess. Also, what does "the information is writing on the socket after every bytes sent by the remote server" mean?
 
Madhusudana Rao
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,Here I am putting my so far code...

import java.io.*;
import java.lang.*;
import java.net.*;

class headsoc
{
public static void main(String args[])throws IOException
{

int c;
Socket s=new Socket("172.16.2.105",23);
InputStream in=s.getInputStream();
OutputStream out=s.getOutputStream();
PrintWriter pout=new PrintWriter(s.getOutputStream());

String user="madhu";
String pwd="xyz123";

while((c=in.read())!=-1)
{

System.out.print((char)c);
byte buf[]=user.getBytes();

System.out.print(user);//This is not working,this will print after the connetion closed.

out.write(buf);//Writes User ID

byte buf2[]=pwd.getBytes();
out.write(buf2);//Writes Password





}

s.close();


}//End of Main

}// End of Class Headsoc

Writing information in the sense,once the socket connection is made..the remote system will send "USERCODE:" for that i have feed with user id "madhu",then the remote system will send "PASSWORD:" then i have to write "xyz123",whats happening is..user id writing into the socket after"U","S","E","R",...so on till ":".I tried storing the bytes in to array,checked the condition if arrays last byte value is":",but getting error as "cant be derefenced".Please help.
 
David Harkness
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay a couple things. First, you need to send a linefeed (a.k.a. enter, '\n') after your username and password, otherwise the server will close the connection after waiting for a while without getting your values. Second, you need to make sure to flush() the output stream (PrintWriter in this case) so the server will see your values.

Take advantage of your use of PrintWriter by sending Strings instead of bytes; they'll be converted to bytes automatically. Creating it with the second optional parameter autoFlush set to true will flush the stream on every call to println().

The trickier part is being able to pull data from the server at the same time as you're inputting data. One simple solution would be to create a separate thread that simply reads from the Socket's InputStream and dumps the output somewhere (the screen or a file or nowhere). This leaves the main thread free to handle sending data to the server.

Note that this may cause issues with timing, but I assume the tasks you need to accomplish on the server will work fine if you slam all the commands to the server as fast as you can, letting the server buffer the input.I haven't compiled or tested this code, so give it a whirl and let us know how it goes.
 
Madhusudana Rao
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks David,
Ill check it out and come back whats exactly is happening.
Thanks again.
 
Madhusudana Rao
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tons,tons and tons of thanks David.
The code is working.Actually it throwed an error that while loop should be used in try and catch block as it is expecting some IOException.I put the try catch block and its working.Only log to be created,but no probs ill take care of that.But you are great.Thanks a lot.Thanks a lot for detalied explnation and kind help.Long live David....
 
David Harkness
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Excellent! I'm glad it worked for you and that you caught that IOException issue. Good luck!
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!