我有一个套接字服务,当一个请求到达时,它正在侦听,来自同样在 java 中的客户端,当这个请求到达时,该服务通过 ssh 连接到服务器以检索信息。 ssh 连接是通过 JSch,当你有正确的用户名和密码时,一切正常,但当你有错误的密码时,它会导致异常 (
com.jcraft.jsch.JSchException: Auth fail
),当执行 session.connect() 时,我的客户端和正在侦听的套接字服务之间的连接发生了一些事情,导致 (SocketTimeoutException: java.net.SocketTimeoutException: Read timed out)
显示下一个异常
SocketTimeoutException:
java.net.SocketTimeoutException: Read timed out
at java.base/java.net.SocketInputStream.socketRead0(Native Method)
at java.base/java.net.SocketInputStream.socketRead(SocketInputStream.java:115)
at java.base/java.net.SocketInputStream.read(SocketInputStream.java:168)
at java.base/java.net.SocketInputStream.read(SocketInputStream.java:140)
at java.base/sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:284)
at java.base/sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:326)
at java.base/sun.nio.cs.StreamDecoder.read(StreamDecoder.java:178)
at java.base/java.io.InputStreamReader.read(InputStreamReader.java:181)
at java.base/java.io.BufferedReader.fill(BufferedReader.java:161)
at java.base/java.io.BufferedReader.readLine(BufferedReader.java:326)
at java.base/java.io.BufferedReader.readLine(BufferedReader.java:392)
at Gopher.sendCmd(Gopher.java:149)
at Gopher.main(Gopher.java:110)
然后显示另一个异常
com.jcraft.jsch.JSchException: Auth fail
at com.jcraft.jsch.Session.connect(Session.java:519)
at com.jcraft.jsch.Session.connect(Session.java:183)
at com.networkInterface.SshWorker5.doConnect3(SshWorker5.java:235)
at com.simpleSw.ossgw.connectionDaemon.command.CfiCmdV3.execute(CfiCmdV3.java:2663)
at com.simpleSw.ossgw.connectionDaemon.Connection.run(Connection.java:71)
at com.simpleSw.ossgw.connectionDaemon.DaemonThread$ConnectionThread.run(DaemonThread.java:90)
at java.base/java.lang.Thread.run(Thread.java:834)
过程如下
1 - 请求通过 shell 程序发送 2 - shell 将请求发送到 java 中的客户端 (Gopher.java) 3 - 客户端执行一个过程来选择将请求发送到哪个套接字(服务器)(4个套接字(服务器)侦听)(Thread.java) 4 - 服务器接收请求并尝试通过 ssh 连接到请求指示的服务器。 5 - ssh连接是通过JSch,当执行
session.connect()
命令时,出现SocketTimeoutException
并且进程在启动请求的shell中结束,我不知道session.connect命令内部发生了什么
6 - 几秒钟后出现第二个异常 (com.jcraft.jsch.JSchException: Auth fail
) 但客户端无法再收到任何东西,因为它给出了超时异常
这是发送请求的代码(客户端)
try {
SocketAddress sockaddr = new InetSocketAddress(getHostName(), getPort());
NetIntSocket = new Socket();
NetIntSocket.connect(sockaddr, 5000);
NetIntSocket.setSoTimeout(getTolerance());
socketOut = new PrintWriter(NetIntSocket.getOutputStream(), true);
socketIn = new BufferedReader(new InputStreamReader(NetIntSocket.getInputStream()));
socketOut.println(getCommand());
String line;
boolean isUDC = isHLRUDC(getCommand());
while ((line = socketIn.readLine()) != null) {
if (isUDC) {
Pattern pattern = Pattern.compile("EPDPIND EPDPADD| NO");
Matcher matcher = pattern.matcher(line);
if (!matcher.find())
System.out.println(line);
}else {
System.out.println(line);
}
}
return true;
这是ssh连接码
public boolean doConnect3(String sshusr, String sship, String sshpwd, String command, PrintWriter out) throws JSchException, IOException {
String result="";
String result2="";
try {
JSch jSch = new JSch();
session = jSch.getSession(sshusr, sship, port);
session.setPassword(sshpwd);
Properties config = new Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.setTimeout(timeout);
out.println("Antes session.conect timeout ["+timeout+"]");
session.connect(); ------------------------------------> here causes the exception
out.println("Despues session.conect timeout ["+timeout+"]");
channel = session.openChannel("shell");
channel.connect();
expect = new ExpectBuilder()
.withOutput(channel.getOutputStream())
.withInputs(channel.getInputStream(), channel.getExtInputStream())
//.withEchoOutput(System.out)
//.withEchoInput(System.err)
.build();
// out.println("Entro al try doConnect3.expect \["+channel.isConnected()+"\]");
return channel.isConnected();
} catch (JSchException e) {
e.printStackTrace();
out.println("JSchException out \[ "+e+" \]");
System.out.println("JSchException System.out \[ "+e+" \]");
Logger.getLogger(SshWorker5.class.getName()).log(Level.SEVERE, "JSchException:", e.getMessage());
out.println("JSchException getMessage() \[ "+e.getMessage()+" \]");
out.println("JSchException getCause() \[ "+e.getCause()+" \]");
out.println("JSchException getStackTrace() \[ "+e.getStackTrace().toString()+" \]");
return false;
} catch (Exception e) {
// out.println("Exception \[ "+e.getMessage()+" \]");
e.printStackTrace();
Logger.getLogger(SshWorker5.class.getName()).log(Level.SEVERE, "SshConnectionException:", e);
}
// out.println("Entro al try doConnect3.catch \["+channel.isConnected()+"\]");
return false;
}
忘了告诉你,当这个异常发生时,它不再打印代码中out.println的任何行。我无法找回
e.getMessage, e.getCause, e.getStackTrace
.
这是调用 ssh 连接的代码的一部分
if (sshSlave5.doConnect3(apgdata\[1\], apgdata\[0\], apgdata\[2\], MmlNe\[0\], out)) {
out.println("");
out.println("\<\<\<\<\< " + MmlNe\[1\] + " \>\>\>\>\>");
out.println("");
out.println("\<" + MmlNe\[0\]);
out.flush();
pod = sshSlave5.executeCommand(MmlNe\[0\], out);
// out.println("Saliendo de sshSlave5.executeCommand \["+pod+"\]");
sshSlave5.disconnect();;
out.println(pod);
doPMlog(MmlNe\[1\], MmlNe\[0\], initTime, "YES", Integer.toString(ActiveQueries), "Executed");
} else {
pod = ServantData.getACCESSERRMSG();
pod = pod + "\\nOSS connectionStatus \[" + qryRS + "\]";
out.println(pod);
out.flush();
doPMlog(MmlNe\[1\], MmlNe\[0\], initTime, "NOT", Integer.toString(ActiveQueries), "ACCESSERRMSG");
}
在这部分它返回 else 但不再打印 out.println
有谁知道 JSch 是否有错误,这可能是什么原因造成的?