Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Java (http://www.velocityreviews.com/forums/f30-java.html)
-   -   telnet from Java (http://www.velocityreviews.com/forums/t135704-telnet-from-java.html)

Wilson Chew 07-29-2004 08:15 PM

telnet from Java
 
hi,

Say I am using a Windows machine, can I use Java to programmatically telnet
to a unix machine?
Here's what I did:

String[] cmd = new String[3];

cmd[0] = "cmd.exe";
cmd[1] = "/c";
cmd[2] = "telnet unixMachine";

process = Runtime.getRuntime().exec(cmd);

PrintWriter pr = new PrintWriter(process.getOutputStream());
pr.print("username"); //print username to process
pr.print("password"); //print password to process

This doesn't seems to work. Anybody has experience with this?

Thanks in advance !!

Wilson Chew



John Fereira 07-29-2004 09:21 PM

Re: telnet from Java
 
"Wilson Chew" <wchew@qualcomm.com> wrote in
news:ceblrs$mrn$1@fair.qualcomm.com:

> hi,
>
> Say I am using a Windows machine, can I use Java to programmatically
> telnet to a unix machine?
> Here's what I did:
>
> String[] cmd = new String[3];
>
> cmd[0] = "cmd.exe";
> cmd[1] = "/c";
> cmd[2] = "telnet unixMachine";
>
> process = Runtime.getRuntime().exec(cmd);
>
> PrintWriter pr = new
> PrintWriter(process.getOutputStream());
> pr.print("username"); //print username to process
> pr.print("password"); //print password to process
>
> This doesn't seems to work. Anybody has experience with this?


Take a look at commons-net package. It's based on the old NetComponents
package and supports Telnet, Ftp, SMTP, NNTP, and several other protocols.
I've used it in the past to automate FTP transfers and it works well.




marcus 07-30-2004 07:39 AM

Re: telnet from Java
 
This is a major pet peeve of mine . . .
What is the word "telnet"? What does it represent? Is it just the name
of a windows program? Hmm, could it be a protocol?

what does the word "protocol" mean? Is it a standard method of doing
something, like diplomatic protocol? An agreed standard of communication?

Hmm, maybe you should do something simple, like google "telnet protocol"
before demonstrating to any prospective employer who googles "Wilson
Chew" how blazingly inept you are at problem solving.

http://www.scit.wlv.ac.uk/~jphb/comms/telnet.html

Then, when you see how silly rewriting an entire communications protocol
would be, try googling c.l.j.p for telnet package.

Better yet, pick up a paintbrush.

-- clh

Wilson Chew wrote:
> hi,
>
> Say I am using a Windows machine, can I use Java to programmatically telnet
> to a unix machine?
> Here's what I did:
>
> String[] cmd = new String[3];
>
> cmd[0] = "cmd.exe";
> cmd[1] = "/c";
> cmd[2] = "telnet unixMachine";
>
> process = Runtime.getRuntime().exec(cmd);
>
> PrintWriter pr = new PrintWriter(process.getOutputStream());
> pr.print("username"); //print username to process
> pr.print("password"); //print password to process
>
> This doesn't seems to work. Anybody has experience with this?
>
> Thanks in advance !!
>
> Wilson Chew
>
>



KC Wong 07-30-2004 08:05 AM

Re: telnet from Java
 
@marcus:

The OP didn't ask to rewrite a telnet client in Java... look at the code the
OP provided. Runtime is created and PrintWriter is used to pump input into
it.

@Wilson:

I haven't tried that, but telnet isn't secure - you don't want to use it.
Use SSH instead... it also talks telnet, and a free client is available for
Win32, Unix and many other platforms.

It's called PuTTY: http://www.chiark.greenend.org.uk/~sgtatham/putty/

It supports commandline mode, where you can tell it to use a script file.



zoopy 07-30-2004 11:31 AM

Re: telnet from Java
 
On 29-7-2004 22:15, Wilson Chew wrote:
> hi,
>
> Say I am using a Windows machine, can I use Java to programmatically telnet
> to a unix machine?
> Here's what I did:
>
> String[] cmd = new String[3];
>
> cmd[0] = "cmd.exe";
> cmd[1] = "/c";
> cmd[2] = "telnet unixMachine";
>
> process = Runtime.getRuntime().exec(cmd);
>
> PrintWriter pr = new PrintWriter(process.getOutputStream());
> pr.print("username"); //print username to process
> pr.print("password"); //print password to process
>
> This doesn't seems to work. Anybody has experience with this?
>
> Thanks in advance !!
>
> Wilson Chew
>
>

Same question came up a few days ago. See
<http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&c2coff=1&threadm=f2f52eb3.0407260907.4616b67d%40 posting.google.com&rnum=1&prev=/groups%3Fhl%3Den%26lr%3D%26ie%3DUTF-8%26c2coff%3D1%26selm%3Df2f52eb3.0407260907.4616b6 7d%2540posting.google.com>
and
<http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&c2coff=1&threadm=f2f52eb3.0407270615.5d365217%40 posting.google.com&rnum=1&prev=/groups%3Fhl%3Den%26lr%3D%26ie%3DUTF-8%26c2coff%3D1%26selm%3Df2f52eb3.0407270615.5d3652 17%2540posting.google.com>

HTH,
Z.

marcus 07-31-2004 04:25 AM

Re: telnet from Java
 
and the difference between writing a telnet client and writing a program
that emulates a telnet client for the purposes of communicating via
telnet protocol is . . . ?

KC Wong wrote:
> @marcus:
>
> The OP didn't ask to rewrite a telnet client in Java... look at the code the
> OP provided. Runtime is created and PrintWriter is used to pump input into
> it.
>
> @Wilson:
>
> I haven't tried that, but telnet isn't secure - you don't want to use it.
> Use SSH instead... it also talks telnet, and a free client is available for
> Win32, Unix and many other platforms.
>
> It's called PuTTY: http://www.chiark.greenend.org.uk/~sgtatham/putty/
>
> It supports commandline mode, where you can tell it to use a script file.
>
>



Steve Horsley 07-31-2004 02:44 PM

Re: telnet from Java
 
Wilson Chew wrote:
> hi,
>
> Say I am using a Windows machine, can I use Java to programmatically telnet
> to a unix machine?
> Here's what I did:
>
> String[] cmd = new String[3];
>
> cmd[0] = "cmd.exe";
> cmd[1] = "/c";
> cmd[2] = "telnet unixMachine";
>
> process = Runtime.getRuntime().exec(cmd);
>
> PrintWriter pr = new PrintWriter(process.getOutputStream());
> pr.print("username"); //print username to process
> pr.print("password"); //print password to process
>
> This doesn't seems to work. Anybody has experience with this?
>
> Thanks in advance !!
>
> Wilson Chew
>
>


It's a start, but you have a way to go. You will also need to use the stdout and
stderr streams from the client and read the messages returned from the server.
Failing to do so may result in a hang because the Process's output buffers
are full.

The immediate reaon this doesn't log in is that you forgot to send the enter
key after the name and password. Try:
pr.print(username + "\r");
or:
pr.println(username);

Steve

marcus 08-01-2004 03:04 AM

Re: telnet from Java
 
incorrect -- the reason it doesn't work is because he failed to complete
the protocol handshake.

Steve Horsley wrote:
> Wilson Chew wrote:
>
>> hi,
>>
>> Say I am using a Windows machine, can I use Java to programmatically
>> telnet
>> to a unix machine?
>> Here's what I did:
>>
>> String[] cmd = new String[3];
>>
>> cmd[0] = "cmd.exe";
>> cmd[1] = "/c";
>> cmd[2] = "telnet unixMachine";
>>
>> process = Runtime.getRuntime().exec(cmd);
>>
>> PrintWriter pr = new
>> PrintWriter(process.getOutputStream());
>> pr.print("username"); //print username to process
>> pr.print("password"); //print password to process
>>
>> This doesn't seems to work. Anybody has experience with this?
>>
>> Thanks in advance !!
>>
>> Wilson Chew
>>
>>

>
> It's a start, but you have a way to go. You will also need to use the
> stdout and
> stderr streams from the client and read the messages returned from the
> server.
> Failing to do so may result in a hang because the Process's output buffers
> are full.
>
> The immediate reaon this doesn't log in is that you forgot to send the
> enter
> key after the name and password. Try:
> pr.print(username + "\r");
> or:
> pr.println(username);
>
> Steve



Chris Uppal 08-01-2004 07:09 AM

Re: telnet from Java
 
marcus wrote:

> incorrect -- the reason it doesn't work is because he failed to complete
> the protocol handshake.


Please read the post again. For your convenience, here is the code from the
original post:

===========
String[] cmd = new String[3];

cmd[0] = "cmd.exe";
cmd[1] = "/c";
cmd[2] = "telnet unixMachine";

process = Runtime.getRuntime().exec(cmd);

PrintWriter pr = new PrintWriter(process.getOutputStream());
pr.print("username"); //print username to process
pr.print("password"); //print password to process
===========

You will note that he is /not/ writing anything (directly) to the network, he
is /not/ dealing with the telnet protocol, that is done (or would be done if it
worked) by the already existing telnet /program/.

-- chris




Steve Horsley 08-01-2004 12:03 PM

Re: telnet from Java
 
Wilson Chew wrote:
> hi,
>
> Say I am using a Windows machine, can I use Java to programmatically telnet
> to a unix machine?
> Here's what I did:
>
> String[] cmd = new String[3];
>
> cmd[0] = "cmd.exe";
> cmd[1] = "/c";
> cmd[2] = "telnet unixMachine";
>
> process = Runtime.getRuntime().exec(cmd);
>
> PrintWriter pr = new PrintWriter(process.getOutputStream());
> pr.print("username"); //print username to process
> pr.print("password"); //print password to process
>
> This doesn't seems to work. Anybody has experience with this?
>
> Thanks in advance !!
>
> Wilson Chew
>
>


You may also want to look up a package called jexpect,
http://www.joot.com/software/jexpect/

This may save you a lot of re-inventing wheels.

Steve


All times are GMT. The time now is 02:30 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.