I'm writing a Java program that connects to a linux machine via SSH.  however when i try to connect i get a Protocol Mismatch

Posted on 2003-03-20
Medium Priority
Last Modified: 2010-03-31
Hi.  I'm having a problem that i need resolved ASAP.  I'm writing a Java program that connects to a linux machine via SSH.  Here is my code.

Socket SSH_Socket = new Socket(_host_ADDRESS, _host_PORT);

WRITE = new DataOutputStream(SSH_Socket.getOutputStream());
System.out.println("Output Stream...CREATED");
READ = new BufferedReader(new InputStreamReader(SSH_Socket.getInputStream()));
System.out.println("Input Stream...CREATED");

System.out.println("Connecting to: " + _host_ADDRESS + "...SUCCESSFUL" );

current_Response = READ.readLine();

System.out.println("SSH RESPONSE :| " + current_Response);
WRITE.writeChars("ls\n"); // testing write to the server by sending a command
current_Response = READ.readLine();
System.out.println("SSH RESPONSE :| " + current_Response);

Here is the output ad response when i run this code:
Output Stream...CREATED
Input Stream...CREATED
Connecting to: hostname/hostIP...SUCCESSFUL
SSH RESPONSE :| SSH-2.0-OpenSSH_3.4p1 Debian 1:3.4p1-1
SSH RESPONSE :| Protocol mismatch.

I would really appreciate the help as I must get this code working.  Also, is there any other suggestions on the best way to connect to a linux machine and issue commands to the command line from within Java code on say a windows machine?

Thanks in advance.  Appreciate it.
Question by:cappucino
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 30

Expert Comment

by:Mayank S
ID: 8179197
I guess this is your client-side since it is trying to connect to some other host. Now, on that host (I assume it is the server), you should use a ServerSocket object which will listen to the port number that you've specified here. The moment it accepts a new connection, the accpet () method will return a Socket object which you can use to communicate with your client.

Something like:

// on the host machine to which you want to connect

ServerSocket ss = new ServerSocket ( portNo ) ; // same as the _host_port above in your code
Socket s = ss.accept () ;
BufferedReader br = new BufferedReader ( new InputStreamReader ( s.getInputStream () ) ) ;
PrintWriter op = new PrintWriter ( s.getOutputStream (), true ) ;

>> WRITE.writeChars("ls\n"); // testing write to the server by sending a command

I hope that WRITE is a PrintWriter object. If not, then please use PrintWriter objects for writing and if you're using readLine () to read, I would suggest that it is best to use PrintWriters (preferably with flush-on-newline set to true as I have done above) and send data using the println () method. Its best to use BufferedReader and PrintWriter objects (preferably with flush-on-newline to true) and the println () & readLine () methods for communicating. There should not be a protocol-error, then.

Please let us know if you still face any problems.


Expert Comment

ID: 8179910
The problem is that you sent "ls\n" to the SSH server the first thing you did. I don't know the SSH protocol in detail, but I assume it expects you to send some handshaking strings where you tell it what protocol version you want to use, some authorization information such as user name and password, and so on.

I suggest you take a look at the SSH-2 protocol specification. I don't have a web address right now but I could try to find one later.

Author Comment

ID: 8181560
Thanks for the comments.  In the code example that you provided above, you mentioned that this code should be on the Host machine to which i want to connect.  The application that i am writing is for the sole purpose of connecting to the linux machine with a standalone app running on my client machine and issuing commands to be run on the linux machine.  I will change my OutputStream object to a PrintWriter as you said.  However, since the connection was created ok and i got a response from the server i think what i need to do now is send it the Handshake string and authorization information as Aleph suggested.  This is what i could really use your help on.  I don't know what to send or how to send this information.  I have searched many site and i cannot find the information i need.  Heeeelp.

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users


Accepted Solution

bkrahmer earned 200 total points
ID: 8187956
This is outside of the scope of this java group.  You need to get information about the SSH protocol.  I googled an RFC in like .01 seconds.  http://www.free.lp.se/fish/rfc.txt


Expert Comment

ID: 9058571
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
Post your closing recommendations!  No comment means you don't care.
LVL 15

Expert Comment

ID: 9730535
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

Accept bkrahmer's comment as answer.

Please leave any comments here within the next seven days.


EE Cleanup Volunteer

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Suggested Courses
Course of the Month10 days, 21 hours left to enroll

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question