当前位置: 首页 > 知识库问答 >
问题:

无法使用Apache camel连接到SFTP(通过jumhost)

卫建义
2023-03-14
    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' sftp-user@sftp.host.com
      Cannot connect/login to: sftp://sftp-user@sftp.host.com:22               

出于测试目的,我尝试使用Spring-Integration连接到SFTP,并成功地使用了下面提到的相同的代理实现(JumpHostProxyCommand)。

下面是我一直使用的Spring boot+Apache Camel代码:

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://sftp-user@sftp.host.com: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")

        }

我已经挣扎了两天来找出错误的根本原因,任何关于这个问题的帮助都是非常感谢的。谢了!

共有1个答案

公良飞尘
2023-03-14

尝试在运行此代码的计算机上的ssh配置文件中添加跳转主机配置。您将能够使用配置文件中指定的主机的跳转主机透明地连接到服务器,而无需在sftp命令中指定任何代理或跳转主机。

设置动态跳转主机的示例配置如下:

Host sftp.host.com
user sftp-user
IdentityFile /home/sftp-user/.ssh/id_rsa
ProxyCommand ssh sftp-user@jump.host.com nc %h %p 2> /dev/null
ForwardAgent yes

可以在主机行中添加多个主机或regex模式。这个条目需要在~/.ssh/config文件中完成(如果还没有创建这个文件,请创建这个文件)。

 类似资料:
  • 问题内容: 我已经在服务器上安装了Kibana 5.4和Elastic search 5.4,我可以通过使用本地计算机上的curl来访问Kibana和Elastic search 我得到以下回应 var hashRoute =’/ app / kibana’; var defaultRoute =’/ app / kibana’; var hash = window.location.hash;

  • 我已经在服务器上安装了Kibana 5.4和Elastic search 5.4,我可以使用 我得到以下回应 var hashRoute='/app/kibana'; var defaultRoute='/app/kibana'; var hash=window.location.hash; if(hash.length){window.location=hashRoute hash;}其他{wi

  • 问题内容: 我最近安装了Elasticsearch,并且在开始的几天里一切正常,但是今天以某种方式停止了工作 当我启动该服务时,它声称很好… 但后来我明白了 查看elasticsearch日志: 看起来有关于Java VM的警告;那可能是问题吗?我还应该尝试/看看什么? 问题答案: 1) 使用linux中的命令 检查端口9200的状态 。 就我而言,以下是启动时的结果。 对我不是服务,否则找到正在

  • 这几天来,我无法在本地运行mac os ML的机器上连接到我的postgreSQL数据库。 我机器的nmap显示postgres在5432上运行,我可以通过pgadmin和psql进行本地连接。 收听地址设置为* 当我尝试连接JDBC时,我会遇到以下异常 组织。postgresql。util。PSQLException:连接被拒绝。检查主机名和端口是否正确,邮政局长是否接受TCP/IP连接。

  • 问题内容: 我在通过Ruby-Watir Webdriver连接到Tor时遇到问题。 我使用Tor浏览器套件。问题是,当我尝试通过Watir(Selenium)连接时,似乎无法打开Tor而不是常规的Firefox。 在研究类似问题时,我尝试了以下方法: 当我使用上述代码时,我仍然会打开普通的firefox浏览器,并且无法连接到洋葱站点。 有什么想法吗?是否需要将Tor浏览器连接到我的webdriv

  • 我正在尝试将sonarqube(版本5.1.2)与intellij(2016.2)集成。我已经添加了sonarLint插件。当我试图将一个sonar服务器添加到sonarLint设置中时,它会询问sonar服务器的用户名和密码以及其他细节,如URL。但它无法连接到声纳服务器,并出现错误:请求失败:https://example.com/api/system/status