Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

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
7
Medium Priority
?
284 Views
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.
0
Comment
Question by:cappucino
6 Comments
 
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.

Mayank.
0
 
LVL 2

Expert Comment

by:Aleph
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.
0
 

Author Comment

by:cappucino
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.

Thanks.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 2

Accepted Solution

by:
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

cheers,
brian
0
 

Expert Comment

by:CleanupPing
ID: 9058571
cappucino:
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 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
0
 
LVL 15

Expert Comment

by:jimmack
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.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

jimmack
EE Cleanup Volunteer
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses
Course of the Month13 days, 11 hours left to enroll

580 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