0

I need to send the following ftp commands

%   ftp test.cs.xxxx.edu  
Connected to test.cs.xxxx.edu.  
220 test FTP server (Version 5.53 ........) ready.  
Name (test.cs.xxxx.edu:yourlogin): yourlogin  
331 Password required for yourlogin.  
Password:  
230 User yourlogin logged in.  
ftp> cd HPSC/exercises  
ftp> get JHFLKDHLFKD.zip 

I tried sending these commands as follows

JSch jsch = new JSch();       

Session session_1 = jsch.getSession("user1", "host1", 22); 
session_1.setConfig("StrictHostKeyChecking", "no");
session_1.setPassword("pass1"); 
session_1.connect();
System.out.println("The session 1  has been established");

ChannelExec exec = (ChannelExec)session_1.openChannel("exec");

exec.connect(); 
exec.setCommand("ftp test.cs.xxxx.edu"); 
exec.setCommand("user2"); 
exec.setCommand("pass2"); 
exec.setCommand("cd \test\test\test"); 
exec.setCommand("get JHFLKDHLFKD.zip");  

But that did not work. I also tried "shell".

What am I doing wrong?

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
Pasha
  • 181
  • 1
  • 1
  • 13
  • Hint: what do you think interprets the FTP commands when you launch your FTP client? The shell or the FTP client? – fge Sep 23 '15 at 19:08

2 Answers2

0

The commands you are sending are commands of the ftp process. These are not commands the remote shell can understand.

The only real command is the ftp. The rest must be fed to the started process to its standard input. Use the setInputStream method to provide the input.


Anyway, you should better use a native FTP client rather that starting a command-line client on the server side, as I've suggested you previously:
How to connect to an FTP server from an existing connection to Unix server?

Community
  • 1
  • 1
Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
0

You are trying to use an SFTP client library to communicate with an FTP server.

Despite similar names and purpose, FTP and SFTP are completely different protocols. See Differences between SFTP and "FTP over SSH"

You need to change either the server or the client, so they match.

Community
  • 1
  • 1
slim
  • 40,215
  • 13
  • 94
  • 127