Saturday, December 20, 2008

Telnet client with java...

Telnet is very important utiliy we use.We can call remote server commands and use different services running on perticular port.Mostly telnet server runs on 23,but can be differed .

Through java, if we want to make remote connection over some port then we have two different ways.

1. Use windows/linux telnet command line utility (which is same on both win and linux... :).)

OR

2. Use socket programming using java.net package.

In first way, we need to get hands on the process.We can achieve that using
Process p = Runtime.getRuntime().exec(<system command or exe path>)
But i don't think this should be used for telnet like communication,because telnet requires continuous session running on remote server and executing you commands.Sometimes it can be so that after executing several commands,you need to rollback so you can cancle all the commands.so its a continuous and sequential process most of the times.The Process class used can be out of our hands anytime if the executable crashes or if anything bad happen(Mostly..the exe crash...).


So, we have another,'Socket' way here, which connects to perticular host on given port number and try to open TCP connection with it.If the host is listening on port and connection accepted,then we can send our commands to the host.(I know, i know, that we all know sockets since our starting graduation days :) ,but let me tell guys... ).

There are two ways to make a telnet client using Sockets.
1.Make it CLI based,means which accepts every input from Command Line Interface entered by user.
2.Make it automated,which takes command list from some file or database,generates a script or list of commands and run it whole on host.

First one is quite easy,i think you all might have done that :).Just let me tell you little what to do...

1.CLI based Telnet...
Socket soc = new Socket(host,port);
DataInputStream din=new DataInputStream(soc.getInputStream());
PrintWriter dout = new PrintWriter(soc.getOutputStream(),true);

BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
See,we have taken InputStream of socket and made a PrintWriter on the OutputStreamof it to send commands.Also we have taken BufferedReader for user inputs from CL.

...
....

do
{
char[] buffer = new char[8192];
int mark = bReader.read(buffer);
StringBuffer sb = null;
for(int i=0;i<buffer.length;i++){
char c = buffer[i];
if(c!='\0'){ //To avoid null characters printed...
if(sb == null){
sb = new StringBuffer(buffer.length);
}
sb.append(c);
}
}
System.out.print((sb!=null)?sb.toString():"-- No Response --");
Command=br.readLine(); //takes commands from CL by user...
if(!Command.equals("quit"))
{
dout.println(Command);
}
}while(!Command.equals("quit")); //exit the loop if user sends 'quit'...
...


This gives you same telnet as you got in your OS, so i think its simple but good to know :)


2.Automated Telnet...

It will be almost same,except we will have a ArrayList/Array of commands in hand before we create Socket.And then change the loop onto the list to execute all the commands one after another...

int j=-1;
String[] commands = new String[5];

...
....

do
{
j++;
char[] buffer = new char[8192];
int mark = bReader.read(buffer);
StringBuffer sb = null;
for(int i=0;i<buffer.length;i++){
char c = buffer[i];
if(c!='\0'){//To avoid null characters printed...
if(sb == null){
sb = new StringBuffer(buffer.length);
}
sb.append(c);
}
}
System.out.print((sb!=null)?sb.toString():"-- No Response --");
Command = commands[j];
if(Command==null || Command.length()<0)
break;
dout.println(Command+"\n");
}while(j<commands.length);
...

So this will executes all commands in commands Array of String.


So folks,this ends our telnet socket :).Same like this we can make client or scripts for FTP automation.It helps in real time situation when you need to make some jobs for backup,export/import files on remote host etc...Waiting for you responses...

3 Comments:

Anonymous said...

Good Tutorial..
But can u pls explain in detail how can we launch a telnet session using java

Anonymous said...

good job,..
May I know 'command' belongs to which data type in the code?

Parth Barot said...

@anusha

Command is a String type, which will be sent over the connection. Look i am taking it from the String[] which contains all the commands to be sent for telnet session in sequence.. :)

thanks for reading.

@anonymous

For telnet session, you can take user inputs from System.in, same as we do in DOS/Bash prompts..that way, your client can be fully interactive...

regards.