2016-11-20 85 views
0

我使用Apache camel FTP和AWS模塊(v2.18)在SFTP和AWS S3之間創建路由。通過ssh jump-host建立到SFTP位置的連接。無法使用Apache駱駝連接到SFTP(通過jumhost)

能夠通過Unix命令連接:

sftp -o UserKnownHostsFile=/dev/null 
     -o StrictHostKeyChecking=no 
     -i /path/to/host/private-key-file 
     -o 'ProxyCommand=ssh 
      -o UserKnownHostsFile=/dev/null 
      -o StrictHostKeyChecking=no 
      -i /path/to/jumphost/private-key-file 
      -l jumphostuser jump.host.com nc sftp.host.com 22' [email protected] 

同時使用Apache駱駝連接但是我得到錯誤:

 Cannot connect/login to: sftp://[email protected]:22    

對於我嘗試連接使用Spring - 整合,我對SFTP測試目的能夠使用下面提到的相同代理實現(JumpHostProxyCommand)成功完成它。

下面是春季開機+ Apache的駱駝的代碼,我一直在使用:

Jsch代理:

 import com.jcraft.jsch.*; 

     class JumpHostProxyCommand implements Proxy { 

       String command; 
       Process p = null; 
       InputStream in = null; 
       OutputStream out = null; 

       public JumpHostProxyCommand(String command) { 
        this.command = command; 
       } 

       public void connect(SocketFactory socket_factory, String host, int port, int timeout) throws Exception { 


        String cmd = command.replace("%h", host); 
        cmd = cmd.replace("%p", new Integer(port).toString()); 

        p = Runtime.getRuntime().exec(cmd); 
        log.debug("Process returned by proxy command {} , {}", command, p); 
        in = p.getInputStream(); 
        log.debug("Input stream returned by proxy {}", in); 
        out = p.getOutputStream(); 
        log.debug("Output stream returned by proxy {}", out); 
       } 

       public Socket getSocket() { 
        return null; 
       } 

       public InputStream getInputStream() { 
        return in; 
       } 

       public OutputStream getOutputStream() { 
        return out; 
       } 

       public void close() { 
        try { 
         if (p != null) { 
          p.getErrorStream().close(); 
          p.getOutputStream().close(); 
          p.getInputStream().close(); 
          p.destroy(); 
          p = null; 
         } 
        } catch (IOException e) { 
         e.printStackTrace(); 
        } 
       } 
      }   

春季啓動駱駝配置:

@Configuration 
    public class CamelConfig { 

     @Autowired 
     DataSource dataSource; 


     @Bean(name = "jdbcMsgIdRepo") 
     public JdbcMessageIdRepository JdbcMessageIdRepository() { 
      return new JdbcMessageIdRepository(dataSource,"jdbc-repo"); 
     } 

     @Bean(name = "s3Client") 
     public AmazonS3 s3Client() { 
      return new AmazonS3Client(); 
     } 

     @Bean(name="jumpHostProxyCommand") 
     JumpHostProxyCommand jumpHostProxyCommand() 
     { 
      String proxykeyFilePath = "/path/to/jumphost/private-key-file"; 

      String command = "ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i /proxy/host/key/path -l jumphostuser jump.host.com nc %h %p"; 
      log.debug("JumpHostProxyCommand : " + command); 
      return new JumpHostProxyCommand(command); 
     } 

     } 

駱駝路線建設者:

  @Component 
      public class FtpRouteInitializer extends RouteBuilder { 

      @Value("${s3.bucket.name}") 
      private String s3Bucket; 

      @Autowired 
      private JdbcMessageIdRepository repo; 


      @Override 
      public void configure() throws Exception { 

       String ftpRoute = "sftp://[email protected]:22/?" 
        + "delay=300s" 
        + "&noop=true" 
        + "&idempotentRepository=#jdbcMsgIdRepo" 
        + "&idempotentKey=${file:name}-${file:modified}" 
        + "&proxy=#jumpHostProxyCommand" 
        + "&privateKeyUri=file:/path/to/host/private-key-file" 
        + "&jschLoggingLevel=DEBUG" 
        + "&knownHostsFile=/dev/null" 
        + "&initialDelay=60s" 
        + "&autoCreate=false" 
        + "&preferredAuthentications=publickey"; 

       from(ftpRoute) 
       .routeId("FTP-S3") 
       .setHeader(S3Constants.KEY, simple("${file:name}")) 
       .to("aws-s3://" + s3ucket + "?amazonS3Client=#s3Client") 
       .log("Uploaded ${file:name} complete."); 
      } 

      }   

的build.gradle文件:

 task wrapper(type: Wrapper) { 
      gradleVersion = '2.5' 
     } 

     ext { 
       springBootVersion = "1.4.1.RELEASE" 
       awsJavaSdkVersion = "1.10.36" 
       postgresVersion = "11.2.0.3.0" 
       jacksonVersion = "2.8.4" 
       sl4jVersion = "1.7.21" 
       junitVersion = "4.12" 
       camelVersion ="2.18.0" 
     } 

     buildscript { 
      repositories { 
       mavenCentral() 
      } 

      dependencies { 
       classpath("org.springframework.boot:spring-boot-gradle-plugin:1.4.1.RELEASE") 
      } 
     } 

     repositories { 
      mavenCentral() 
     } 

     apply plugin: 'java' 
     apply plugin: 'eclipse' 
     apply plugin: 'spring-boot' 

     sourceCompatibility = 1.8 
     targetCompatibility = 1.8 

     springBoot { 
      executable = true 
     } 

     dependencies { 

      //logging 
      compile("ch.qos.logback:logback-classic:1.1.3") 
      compile("ch.qos.logback:logback-core:1.1.3") 
      compile("org.slf4j:slf4j-api:$sl4jVersion") 

      //Spring boot 
      compile("org.springframework.boot:spring-boot-starter-web:$springBootVersion") 
      compile("org.springframework.boot:spring-boot-starter-jdbc:$springBootVersion") 
      compile("org.apache.camel:camel-spring-boot-starter:$camelVersion") 

      //Jdbc 
      compile("postgresql:postgresql:9.0-801.jdbc4") 

      //Camel 
      compile("org.apache.camel:camel-ftp:$camelVersion") 
      compile("org.apache.camel:camel-aws:$camelVersion") 
      compile("org.apache.camel:camel-core:$camelVersion") 
      compile("org.apache.camel:camel-spring-boot:$camelVersion") 
      compile("org.apache.camel:camel-sql:$camelVersion") 


      //Aws sdk 
      compile("com.amazonaws:aws-java-sdk:$awsJavaSdkVersion") 

      //Json 
      compile("com.fasterxml.jackson.core:jackson-core:$jacksonVersion") 
      compile("com.fasterxml.jackson.core:jackson-annotations:$jacksonVersion") 
      compile("com.fasterxml.jackson.core:jackson-databind:$jacksonVersion") 
      compile("com.fasterxml.jackson.datatype:jackson-datatype-jsr310:$jacksonVersion") 

      //Swagger 
      compile("io.springfox:springfox-swagger2:2.0.2") 
      compile("io.springfox:springfox-swagger-ui:2.0.2") 

      //utilities 
      compile('org.projectlombok:lombok:1.16.6') 
      compile("org.apache.commons:commons-collections4:4.1") 
      compile("org.apache.commons:commons-lang3:3.4") 



      //Junit 
      testCompile("junit:junit:$junitVersion") 
      testCompile("org.springframework.boot:spring-boot-starter-test:$springBootVersion") 
      testCompile("org.mockito:mockito-all:1.10.19") 

     } 

我一直在掙扎了最後2天,找出錯誤的根源,在這個問題上的任何幫助真的讚賞。謝謝!

+0

我的答案解決了您的問題嗎? – Tushu

回答

0

嘗試在運行此代碼的機器上的ssh配置文件中添加跳轉主機配置。您應該能夠使用配置文件中指定的主機的跳轉主機透明地連接到服務器,而無需在sftp命令中指定任何代理或跳轉主機。

比如說配置設置一個動態跳躍主機如下:

Host sftp.host.com 
user sftp-user 
IdentityFile /home/sftp-user/.ssh/id_rsa 
ProxyCommand ssh [email protected] nc %h %p 2> /dev/null 
ForwardAgent yes 

可以在主機行添加多個主機或正則表達式。這個條目需要在〜/ .ssh/config文件中完成(如果這個文件尚不存在,就創建這個文件)。