telnet from Java

W

Wilson Chew

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
 
J

John Fereira

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.
 
M

marcus

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
 
K

KC Wong

@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.
 
Z

zoopy

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=...2eb3.0407260907.4616b67d%40posting.google.com>
and
<http://groups.google.com/groups?hl=...2eb3.0407270615.5d365217%40posting.google.com>

HTH,
Z.
 
M

marcus

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 . . . ?
 
S

Steve Horsley

Wilson said:
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
 
M

marcus

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

Steve said:
Wilson said:
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
 
C

Chris Uppal

marcus said:
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
 
S

Steve Horsley

Wilson said:
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
 
Joined
Aug 15, 2011
Messages
1
Reaction score
0
Another Pet Peeve

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

A major pet peeve of mine is the fact that nearly every news group has a pedantic a-hole or two hanging around waiting for opportunities to toot their own horn at someone else's expense.

-rz
:adore:
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top