当尝试使用JSch连接到远程服务器时,我可以很好地连接到服务器并获得我需要的东西。但是每次第一次连接到服务器时,JSch都会拒绝连接并抛出异常。如果我再次失败的服务器它工作正常,它似乎只是在启动应用程序时第一次连接。我把会话从失败的时候到它的工作时进行了比较,它们都是相同的,只是无法弄清楚为什么它在第一次失败时会失败。
public class SSHConnector {
private static String password;
private String connectionRefused = "Connection refused to server. Server could be offline. \r\n";
/**
* Constructor for SSHConnector
*/
public SSHConnector() {
}
/**
* Get all versions for a server via SSH connection
*
* @param hostname
* The hostname of the server
* @param username
* The username of the server
* @param port
* The port of the server
* @param password
* The port of the server
* @param location
* The folder location of the server
* @return hashmap of all the versions
*/
public HashMap<String, String> getVersions(String hostname, String username, int port, String password,
String location) {
HashMap<String, String> result = new HashMap<String, String>();
String command = "cd " + location + "; ls";
String folders = "";
folders = getFolders(command, username, hostname, port, password);
if (!folders.equals(connectionRefused)) {
String[] foldersList = folders.split("\n");
for (String folder : foldersList) {
command = "cd " + location + "/" + folder + "; ls";
result.put(folder, getFolders(command, username, hostname, port, password));
}
} else {
result.put("", folders);
}
return result;
}
private String getFolders(String command, String username, String hostname, int port, String password) {
String result = "";
JSch jsch = new JSch();
Session session = null;
try {
session = jsch.getSession(username, hostname, port);
} catch (JSchException e) {
System.out.println(e.getMessage());
}
Properties config = new Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
UserInfo ui = new MyUserInfo();
session.setUserInfo(ui);
try {
session.connect(); // Fails first time here
} catch (JSchException e1) {
e1.printStackTrace();
}
SSHConnector.password = password;
Channel channel = null;
try {
channel = session.openChannel("exec");
} catch (JSchException e) {
return connectionRefused;
}
((ChannelExec) channel).setCommand(command);
channel.setInputStream(null);
((ChannelExec) channel).setErrStream(System.err);
InputStream in = null;
try {
in = channel.getInputStream();
} catch (IOException e1) {
e1.printStackTrace();
}
try {
channel.connect();
} catch (JSchException e) {
e.printStackTrace();
}
byte[] tmp = new byte[1024];
while (true) {
try {
while (in.available() > 0) {
int i = in.read(tmp, 0, 1024);
if (i < 0) {
break;
}
result = new String(tmp, 0, i);
}
} catch (IOException e) {
e.printStackTrace();
}
if (channel.isClosed()) {
try {
if (in.available() > 0) {
continue;
}
} catch (IOException e) {
e.printStackTrace();
}
break;
}
}
channel.disconnect();
session.disconnect();
return result;
}
/**
* MyUserInfo class
*/
public static class MyUserInfo implements UserInfo {
/**
* Get passphrase
*
* @return null
*/
public String getPassphrase() {
return null;
}
/**
* Get password
*
* @return password
*/
public String getPassword() {
return password;
}
/**
* Prompt Password
*
* @return true
*/
public boolean promptPassword(String message) {
return true;
}
/**
* Prompt Passphrase
*
* @return false
*/
public boolean promptPassphrase(String message) {
return false;
}
/**
* Prompt Yes No
*
* @return false
*/
public boolean promptYesNo(String message) {
return false;
}
/**
* Show Message
*/
public void showMessage(String message) {
}
}
}
每次都在同一部分失败:
try {
session.connect(); // Fails first time here
} catch (JSchException e1) {
e1.printStackTrace();
}
我收到的错误是:
com.jcraft.jsch.JSchException: Auth cancel
at com.jcraft.jsch.Session.connect(Session.java:511)
at com.jcraft.jsch.Session.connect(Session.java:183)
at uk.co.joshuawoolley.ssc.ssh.SSHConnector.getFolders(SSHConnector.java:86)
at uk.co.joshuawoolley.ssc.ssh.SSHConnector.getVersions(SSHConnector.java:51)
at uk.co.joshuawoolley.ssc.ssh.SSHManager.queryServers(SSHManager.java:41)
at uk.co.joshuawoolley.ssc.gui.GenerateReport$5.actionPerformed(GenerateReport.java:117)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6516)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
at java.awt.Component.processEvent(Component.java:6281)
at java.awt.Container.processEvent(Container.java:2229)
at java.awt.Component.dispatchEventImpl(Component.java:4872)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Component.dispatchEvent(Component.java:4698)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
at java.awt.Container.dispatchEventImpl(Container.java:2273)
at java.awt.Window.dispatchEventImpl(Window.java:2719)
at java.awt.Component.dispatchEvent(Component.java:4698)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:740)
at java.awt.EventQueue.access$300(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:699)
at java.awt.EventQueue$3.run(EventQueue.java:697)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:713)
at java.awt.EventQueue$4.run(EventQueue.java:711)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:710)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)
问题图片:
Picture after clicking button for first time
Picture after clicking button for second time
非常感谢任何帮助。
答案 0 :(得分:0)
//You should set the password before your first connect.
string SSH_PASSWORD = "your password";
session.password(SSH_PASSWORD);