帮助Jsch,在JumpHosts上执行命令:我需要在我无法直接访问的服务器中获得SSH访问,因为中间有一个SSH跳转服务器。我使用JSCH库,但是文档(JumpHosts.java示例)是关于如何获取服务器文件目录的。有什么建议吗
public static void main(String[] arg){
try{
JSch jsch=new JSch();
String host="host ip";
if(arg.length>0){
host=arg[0];
}
String user = "username";
session=jsch.getSession(user, host, 22);
String passwd = "password";
session.setPassword(passwd);
UserInfo ui = new MyUserInfo(){
public void showMessage(String message){
JOptionPane.showMessageDialog(null, message);
}
};
session.setUserInfo(ui);
session.connect(30000); // making a connection with timeout.
channel=session.openChannel("shell");
channel.setInputStream(System.in);
channel.setOutputStream(System.out);
channel.connect(3*1000);
}
catch(Exception e){
System.out.println(e);
}
答案 0 :(得分:0)
我只是按照我的要求去做。我与有需要的人分享:此后,您必须创建Channel(“ Shell / Exec”)并管理输入/输出流(对我的英语感到抱歉)
public static void sesionA(){
try {
Session sessionA = jSch.getSession(username, hostA);
Properties config = new Properties();
config.put("StrictHostKeyChecking", "no");
sessionA.setConfig(config);
sessionA.setPassword(passwordA);
sessionA.connect();
if(sessionA.isConnected()) {
System.out.println("Connected host A!");
forwardedPort = 2222;
sessionA.setPortForwardingL(forwardedPort, hostB, 22);
}
} catch (JSchException e) {
e.printStackTrace();
}
}
public static void sesionB(){
try {
Session sessionB = jSch.getSession(username, "localhost", forwardedPort);
Properties config = new Properties();
config.put("StrictHostKeyChecking", "no");
sessionB.setConfig(config);
sessionB.setPassword(passwordB);
sessionB.connect();
if(sessionB.isConnected()) {
System.out.println("Connected host B!");
}
}
}