Connexion SSH à l'aide de Java

Que j'essaie d'établir une connexion SSH à travers mon code Java, mais ci-dessous exception .. j'ai testé ma connexion via Putty/Winscp outils et il fonctionne très bien. Le problème est avec mon code Java...

SEVERE: The Transport Protocol thread failed
java.io.IOException: The socket is EOF
    at com.sshtools.j2ssh.transport.TransportProtocolInputStream.readBufferedData(Unknown Source)
    at com.sshtools.j2ssh.transport.TransportProtocolInputStream.readMessage(Unknown Source)
    at com.sshtools.j2ssh.transport.TransportProtocolCommon.readMessage(Unknown Source)
    at com.sshtools.j2ssh.transport.kex.DhGroup1Sha1.performClientExchange(Unknown Source)
    at com.sshtools.j2ssh.transport.TransportProtocolClient.performKeyExchange(Unknown Source)
    at com.sshtools.j2ssh.transport.TransportProtocolCommon.beginKeyExchange(Unknown Source)
    at com.sshtools.j2ssh.transport.TransportProtocolCommon.onMsgKexInit(Unknown Source)
    at com.sshtools.j2ssh.transport.TransportProtocolCommon.startBinaryPacketProtocol(Unknown Source)
    at com.sshtools.j2ssh.transport.TransportProtocolCommon.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)

Ci-dessous est mon morceau de code Java pour établir la connexion

public class MySSHClient {
static SshClient ssh = null;
static SshConnectionProperties properties = null;
SessionChannelClient session = null;
private static void MySSHClient(String hostName, String userName, String passwd )
{
try
{
//Make a client connection
ssh = new SshClient();
properties = new SshConnectionProperties();
properties.setHost("192.168.1.175");
//Connect to the host
ssh.connect(properties, new IgnoreHostKeyVerification());
//Create a password authentication instance
PasswordAuthenticationClient pwd = new PasswordAuthenticationClient();
pwd.setUsername("root");
pwd.setPassword("123456");
//Try the authentication
int result = ssh.authenticate(pwd);
//Evaluate the result
if (result==AuthenticationProtocolState.COMPLETE) {
System.out.println("Connection Authenticated");
}
}
catch(Exception e)
{
System.out.println("Exception : " + e.getMessage());
}
}//end of method.
public String execCmd(String cmd)
{
String theOutput = "";
try
{
//The connection is authenticated we can now do some real work!
session = ssh.openSessionChannel();
if ( session.executeCommand(cmd) )
{
IOStreamConnector output = new IOStreamConnector();
java.io.ByteArrayOutputStream bos =  new
java.io.ByteArrayOutputStream();
output.connect(session.getInputStream(), bos );
session.getState().waitForState(ChannelState.CHANNEL_CLOSED);
theOutput = bos.toString();
}
//else
//throw Exception("Failed to execute command : " + cmd);
//System.out.println("Failed to execute command : " + cmd);
}
catch(Exception e)
{
System.out.println("Exception : " + e.getMessage());
}
return theOutput;
}
public static void main(String[] args){
MySSHClient(null, null, null);
}
essayez d'utiliser jcraft.com/jsch
pour être honnête, l'exception et la posté code ne semblent pas correspondre, aussi execCmd n'est pas utilisé.
quelle est la version de ssh sur la machine linux?
[root@centos-test ssh]# ssh -v localhost OpenSSH_5.3p1, OpenSSL 1.0.1 e-fips-11 Feb 2013 debug1: la Lecture des données de configuration /etc/ssh/ssh_config debug1: Application des options pour l' * debug1: la Connexion à localhost [::1] le port 22. debug1: Connexion établie. debug1: permanently_set_uid: 0/0 debug1: identité fichier /root/.ssh/identity type -1 debug1: identité fichier /root/.ssh/identity-cert type -1 debug1: identité fichier /root/.ssh/id_rsa type -1 debug1: protocole à Distance de la version 2.0, la version du logiciel à distance

OriginalL'auteur Karthick Sambanghi | 2016-02-10