How to connect to an FTP server from an existing connection to Unix server?

不羁的心 提交于 2019-12-05 08:02:40

问题


Currently I am using Reflection for Unix and OpenVMS. I connect to a Unix server. Once connected, I establish a new FTP connection to the target server. Here is how the console window look like:

login: xxx 
Password : xxx 
// Now I'm connected to UNIX
// Next I am connecting to that FTP 
ftpLogin@hostName :/whatever/.../ $ ftp someftp.example.com 
Conencted 
Name: myLogin 
Password: 
XXX 
Login OK 

I tried to replicate the same steps programmatically, but got confused with this "double connection" thing. I was able to connect to Unix only, but then I don't know how to proceed.

JSch jsch = new JSch();
Session session = jsch.getSession(user, host, port);
    session.setPassword(password);
    session.setConfig("StrictHostKeyChecking", "no");
System.out.println("Establishing Connection...");
session.connect(); 

Now that I am connected to Unix, how do I proceed with this step ftpLogin@hostName :/whatever/.../ $ ftp someftp.example.com?

I think I might only need to send a command, something like

sftpChannel.setCommand("ftpLogin@hostName :/whatever/.../ $ ftp someftp.example.com ?") 

How can I connect to a separate FTP from an existing "session/connection"?


回答1:


You typically use the local port forwarding for this.

In JSch, after you connect, call the setPortForwardingL:

int ftpPort = session.setPortForwardingL(0, "someftp.example.com", 21);

And then connect a local FTP client (e.g. the FTPClient from Apache Commons) to the localhost:ftpPort:

FTPClient ftp = new FTPClient();
ftp.connect("localhost", ftpPort);
ftp.login("myLogin", password);


来源:https://stackoverflow.com/questions/32721530/how-to-connect-to-an-ftp-server-from-an-existing-connection-to-unix-server

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!