aspose file tools
The moose likes Sockets and Internet Protocols and the fly likes Auto Telent logging..Plz help Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Reply Bookmark "Auto Telent logging..Plz help" Watch "Auto Telent logging..Plz help" New topic
Author

Auto Telent logging..Plz help

Madhusudana Rao
Greenhorn

Joined: Apr 25, 2005
Posts: 5
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

Joined: Aug 07, 2003
Posts: 1646
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

Joined: Apr 25, 2005
Posts: 5
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

Joined: Aug 07, 2003
Posts: 1646
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

Joined: Apr 25, 2005
Posts: 5
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

Joined: Aug 07, 2003
Posts: 1646
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

Joined: Apr 25, 2005
Posts: 5
Thanks David,
Ill check it out and come back whats exactly is happening.
Thanks again.
Madhusudana Rao
Greenhorn

Joined: Apr 25, 2005
Posts: 5
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

Joined: Aug 07, 2003
Posts: 1646
Excellent! I'm glad it worked for you and that you caught that IOException issue. Good luck!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Auto Telent logging..Plz help
 
Similar Threads
Need Help
regular expression for new line not in double quotes
Equivalent of 'Detect Capture Devices' in JMF Registry
Need Testers for Shareware
HFSJ 2nd MOCK