【发布时间】:2016-05-20 08:15:12
【问题描述】:
我正在尝试通过我的 Java 代码建立 SSH 连接,但遇到异常。我通过 Putty/Winscp 工具测试了我的连接,它工作正常。问题出在我的 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)
下面是我建立连接的一段Java代码
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);
}
【问题讨论】:
-
尝试使用jcraft.com/jsch
-
老实说,异常和贴出的代码好像不匹配,也没有使用
execCmd。 -
linux主机上
ssh的版本是多少? -
[root@centos-test ssh]# ssh -v localhost OpenSSH_5.3p1, OpenSSL 1.0.1e-fips 2013 年 2 月 11 日 debug1:读取配置数据 /etc/ssh/ssh_config debug1:应用选项* debug1:连接到 localhost [::1] 端口 22。 debug1:已建立连接。 debug1:permanent_set_uid:0/0 debug1:身份文件/root/.ssh/身份类型-1 debug1:身份文件/root/.ssh/identity-cert类型-1 debug1:身份文件/root/.ssh/id_rsa类型- 1 debug1:远程协议版本2.0,远程软件版本
-
您的问题在这里有几个答案——请接受一个(如果它回答了您的问题),这样您的问题就不会在问题搜索中显示为未回答。