commons-net is compatible with ssh-2.0 protocol - java

i have tried create a project with library commons.net for send via ftp some files. But i created a connection with my server i have received this error.
org.apache.commons.net.MalformedServerReplyException: Could not parse response code.
Server Reply: SSH-2.0-OpenSSH_5.3
i have followed this article for create my connection, and with official examples i've controlled article.
my java code here:
private void connect(String host, String user, String pwd) {
try{
ftp = new FTPSClient(false);
//ftp.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out)));
int reply;
ftp.connect(host,22);//error is here
reply = ftp.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();
throw new Exception("Exception in connecting to FTP Server");
}
ftp.login(user, pwd);
ftp.setFileType(FTP.BINARY_FILE_TYPE);
ftp.enterLocalPassiveMode();
}catch(Exception ex){
ex.printStackTrace();
}
}
I do not understand where I went wrong.

The FTPS protocol does not run over SSH. What you need is SFTP. For this you could look at the Jsch library
JSch jsch = new JSch();
Session session = jsch.getSession( user, host, port );
session.setConfig( "PreferredAuthentications", "password" );
session.setPassword( pass );
session.connect( FTP_TIMEOUT );
Channel channel = session.openChannel( "sftp" );
ChannelSftp sftp = ( ChannelSftp ) channel;
sftp.connect( FTP_TIMEOUT );

SFTP (file transfer running as an SSH stream over an SSH connection) is not the same as FTPS (FTP using SSL/TLS).

import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;
public class FTPConnectAndLoginDemo {
public static void main(String[] args) throws Exception {
String user = "username";
String host = "hostadress";
int port = 22;
String pass = "password";
JSch jsch = new JSch();
Session session = jsch.getSession(user, host, port);
session.setConfig("StrictHostKeyChecking", "no");
session.setPassword(pass);
session.connect();
System.out.println("Connection established.");
System.out.println("Creating SFTP Channel.");
}
try this instead hope this can help.The Session.setConfig ommits the hashcode check and allows user to connect to host.

Change the port to 21 to connect to FTP server instead of SFTP(port 22)

Related

SSH on to a remote server with Credentials from Java app for Request & Response [duplicate]

My aim is to connect to a server (host) which is behind a firewall. I am able to access this server by connecting to another server (tunnel) in the network and then SSH to this server. However I am not able to implement the same scenario via JSch.
I am not able to have the below code work, which I have written for this purpose. Please let me know if I am doing anything silly here.
public class JschExecutor {
public static void main(String[] args){
JschExecutor t=new JschExecutor();
try{
t.go();
} catch(Exception ex){
ex.printStackTrace();
}
}
public void go() throws Exception{
StringBuilder outputBuffer = new StringBuilder();
String host="xxx.xxx.xxx.xxx"; // The host to be connected finally
String user="user";
String password="passwrd";
int port=22;
String tunnelRemoteHost="xx.xx.xx.xx"; // The host from where the tunnel is created
JSch jsch=new JSch();
Session session=jsch.getSession(user, host, port);
session.setPassword(password);
localUserInfo lui=new localUserInfo();
session.setUserInfo(lui);
session.setConfig("StrictHostKeyChecking", "no");
ProxySOCKS5 proxyTunnel = new ProxySOCKS5(tunnelRemoteHost, 22);
proxyTunnel.setUserPasswd(user, password);
session.setProxy(proxyTunnel);
session.connect(30000);
Channel channel=session.openChannel("exec");
((ChannelExec)channel).setCommand("hostname");
channel.setInputStream(null);
((ChannelExec)channel).setErrStream(System.err);
InputStream in=channel.getInputStream();
BufferedReader ebr = new BufferedReader(new InputStreamReader(in));
channel.connect();
while (true) {
byte[] tmpArray=new byte[1024];
while(in.available()>0){
int i=in.read(tmpArray, 0, 1024);
if(i<0)break;
outputBuffer.append(new String(tmpArray, 0, i)).append("\n");
}
if(channel.isClosed()){
System.out.println("exit-status: "+channel.getExitStatus());
break;
}
}
ebr.close();
channel.disconnect();
session.disconnect();
System.out.println(outputBuffer.toString());
}
class localUserInfo implements UserInfo{
String passwd;
public String getPassword(){ return passwd; }
public boolean promptYesNo(String str){return true;}
public String getPassphrase(){ return null; }
public boolean promptPassphrase(String message){return true; }
public boolean promptPassword(String message){return true;}
public void showMessage(String message){}
}
}
The above code gives the below exception on the session.connect(30000); line.
com.jcraft.jsch.JSchException: ProxySOCKS5: com.jcraft.jsch.JSchException: fail in SOCKS5 proxy
at com.jcraft.jsch.ProxySOCKS5.connect(ProxySOCKS5.java:317)
at com.jcraft.jsch.Session.connect(Session.java:231)
at com.ukris.main.JschExecutor.go(JschExecutor.java:50)
at com.ukris.main.JschExecutor.main(JschExecutor.java:19)
Caused by: com.jcraft.jsch.JSchException: fail in SOCKS5 proxy
at com.jcraft.jsch.ProxySOCKS5.connect(ProxySOCKS5.java:200)
... 3 more
a SOCKS proxy setting on jsch allows you to connect to a running proxy server on the remote side. An sshd on the remote side would not be considered a SOCKS proxy. What you will have to do is establish a local port forward to the ssh port on the machine you're tunneling to, then establish a secondary ssh connection to this system using the api.
I've taken your example and slightly rewritten it to accomplish this:
import com.jcraft.jsch.*;
import java.io.*;
public class JschExecutor2 {
public static void main(String[] args){
JschExecutor2 t=new JschExecutor2();
try{
t.go();
} catch(Exception ex){
ex.printStackTrace();
}
}
public void go() throws Exception{
StringBuilder outputBuffer = new StringBuilder();
String host="firstsystem"; // First level target
String user="username";
String password="firstlevelpassword";
String tunnelRemoteHost="secondlevelhost"; // The host of the second target
String secondPassword="targetsystempassword";
int port=22;
JSch jsch=new JSch();
Session session=jsch.getSession(user, host, port);
session.setPassword(password);
localUserInfo lui=new localUserInfo();
session.setUserInfo(lui);
session.setConfig("StrictHostKeyChecking", "no");
// create port from 2233 on local system to port 22 on tunnelRemoteHost
session.setPortForwardingL(2233, tunnelRemoteHost, 22);
session.connect();
session.openChannel("direct-tcpip");
// create a session connected to port 2233 on the local host.
Session secondSession = jsch.getSession(user, "localhost", 2233);
secondSession.setPassword(secondPassword);
secondSession.setUserInfo(lui);
secondSession.setConfig("StrictHostKeyChecking", "no");
secondSession.connect(); // now we're connected to the secondary system
Channel channel=secondSession.openChannel("exec");
((ChannelExec)channel).setCommand("hostname");
channel.setInputStream(null);
InputStream stdout=channel.getInputStream();
channel.connect();
while (true) {
byte[] tmpArray=new byte[1024];
while(stdout.available() > 0){
int i=stdout.read(tmpArray, 0, 1024);
if(i<0)break;
outputBuffer.append(new String(tmpArray, 0, i));
}
if(channel.isClosed()){
System.out.println("exit-status: "+channel.getExitStatus());
break;
}
}
stdout.close();
channel.disconnect();
secondSession.disconnect();
session.disconnect();
System.out.print(outputBuffer.toString());
}
class localUserInfo implements UserInfo{
String passwd;
public String getPassword(){ return passwd; }
public boolean promptYesNo(String str){return true;}
public String getPassphrase(){ return null; }
public boolean promptPassphrase(String message){return true; }
public boolean promptPassword(String message){return true;}
public void showMessage(String message){}
}
}
What this code does is create a local port forwarding to the ssh port on the target system, then connects through it. The running of the hostname command illustrates that it is, indeed, running on the forwarded-to system.
This is tested and working fine. this works like secure pipes and best for tunneling
String strSshUser = "ssh_user_name"; // SSH loging username
String strSshPassword = "abcd1234"; // SSH login password
String strSshHost = "your.ssh.hostname.com"; // hostname or ip or
// SSH server
int nSshPort = 22; // remote SSH host port number
String strRemoteHost = "your.database.hostname.com"; // hostname or
// ip of
// your
// database
// server
int nLocalPort = 3366; // local port number use to bind SSH tunnel
int nRemotePort = 3306; // remote port number of your database
String strDbUser = "db_user_name"; // database loging username
String strDbPassword = "4321dcba"; // database login password
final JSch jsch = new JSch();
Session session = jsch.getSession(strSshUser, strSshHost, 22);
session.setPassword(strSshPassword);
final Properties config = new Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.connect();
session.setPortForwardingL(nLocalPort, strRemoteHost, nRemotePort);

Getting "connection is closed by foreign host" when I use JSch to transfer file from a remote machine

I'm trying to use JSch to transfer files from a remote machine, whenever I execute the code I get the exception "com.jcraft.jsch.JSchException: connection is closed by foreign host". My code is given below, it doesn't even print "session established", so I thing the problem is at seeion.connetct();
Please help.
String SFTPHOST ="hostname";
int SFTPPORT = 23;
String SFTPUSER = "user";
String SFTPPASS = "passwrd";
String source ="source file";
Stirng dest ="destination";
Session session = null;
Channel channel = null;
ChannelSftp channelSftp = null;
try{
JSch jsch = new JSch();
session = jsch.getSession(SFTPUSER,SFTPHOST,SFTPPORT);
session.setPassword(SFTPPASS);
session.connect();
System.out.println("session connected...");
channel = session.openChannel("sftp");
channel.connect();
System.out.println("channel connected...");
channelSftp = (ChannelSftp)channel;
channelSftp.get(source,dest);
System.out.println("Transfer completed");
}catch(Exception ex){
ex.printStackTrace();
}

can not connect to FTPS (explicit) server with java?

I use this code to connect to ftps with explicit encryption
String host = "10.21.13.66";
int port = 21;
String username = "user";
String password = "pass";
try {
FTPSClient ftpClient = new FTPSClient("ssl",false);
ftpClient.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out)));
ftpClient.connect(host, port);
int reply = ftpClient.getReplyCode();
if (FTPReply.isPositiveCompletion(reply)) {
// Login
(ftpClient.login(username, password))
}
} catch (IOException ioe) {
System.out.println("FTP client received network error");
}
when I try to connect to my own ftps it returns this error: 431 No security resource for TLS/SSL encryption, probably there is no selected SSL certificate
any help would be appreciated;
Try setting the auth on ftps client using below code after object is constructed:
ftpClient.setAuthValue("TLS");

how to transfer a file through SFTP in java? [duplicate]

This question already has answers here:
How to retrieve a file from a server via SFTP?
(16 answers)
Closed 6 years ago.
How to transfer a file through SFTP in java? I want sample code for SFTP client.
I want to embed the SFTP server in my application and the client should able to send a file to my application.
PS: This was asked for SFTP client. And This question is not a duplicate of other two questions.
Find the below link to implement SFTP.
https://codetransient.wordpress.com/2019/06/22/sftp-secured-file-transfer-protocol/
Try this code.
public void send (String fileName) {
String SFTPHOST = "host:IP";
int SFTPPORT = 22;
String SFTPUSER = "username";
String SFTPPASS = "password";
String SFTPWORKINGDIR = "file/to/transfer";
Session session = null;
Channel channel = null;
ChannelSftp channelSftp = null;
System.out.println("preparing the host information for sftp.");
try {
JSch jsch = new JSch();
session = jsch.getSession(SFTPUSER, SFTPHOST, SFTPPORT);
session.setPassword(SFTPPASS);
java.util.Properties config = new java.util.Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.connect();
System.out.println("Host connected.");
channel = session.openChannel("sftp");
channel.connect();
System.out.println("sftp channel opened and connected.");
channelSftp = (ChannelSftp) channel;
channelSftp.cd(SFTPWORKINGDIR);
File f = new File(fileName);
channelSftp.put(new FileInputStream(f), f.getName());
log.info("File transfered successfully to host.");
} catch (Exception ex) {
System.out.println("Exception found while tranfer the response.");
} finally {
channelSftp.exit();
System.out.println("sftp Channel exited.");
channel.disconnect();
System.out.println("Channel disconnected.");
session.disconnect();
System.out.println("Host Session disconnected.");
}
}

FTPS client behind a proxy server using apache commons net

Our network team only allow us to connect to our third party client thru proxy server.
Is there a way to add a proxy server to FTPS client of apache commons net?
If it is not possible, can you tell a way on how to do it.
By the way here's the code that is working outside of the company network
String server = "ftp.xxxx.com";
String username = "username";
String password = "password";
String remoteFile = "xmlSR.xml";
String localFile = "c:/downloadedfile.xml";
String protocol = "TLS"; // TLS / SSL
int timeoutInMillis = 3000;
boolean isImpicit = false;
FTPSClient client = new FTPSClient(protocol, isImpicit);
client.enterLocalActiveMode();
client.setRemoteVerificationEnabled(false);
client.setActivePortRange(50000, 50200);
client.setDataTimeout(timeoutInMillis);
client.addProtocolCommandListener(new PrintCommandListener(
new PrintWriter(System.out)));
client.setTrustManager(TrustManagerUtils.getAcceptAllTrustManager());
try {
int reply;
client.connect(server);
client.login(username, password);
client.setFileType(FTP.BINARY_FILE_TYPE);
client.execPBSZ(0);
client.execPROT("P");
System.out.println("Connected to " + server + ".");
reply = client.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
client.disconnect();
System.err.println("FTP server refused connection.");
System.exit(1);
}
client.listFiles();
boolean retrieved = client.retrieveFile(remoteFile,
new FileOutputStream(localFile));
} catch (Exception e) {
if (client.isConnected()) {
try {
client.disconnect();
} catch (IOException ex) {
ex.printStackTrace();
}
}
System.err.println("Could not connect to server.");
e.printStackTrace();
return;
} finally {
System.out.println("# client disconnected");
client.disconnect();
}
even we tried to add some system property for proxyHost and proxyPort
System.setProperty("http.proxyPort", "80");
System.setProperty("http.proxyHost", "yyyy.com");
System.setProperty("ftp.proxyPort", "80");
System.setProperty("ftp.proxyHost", "yyyy.com");
System.setProperty("socksProxyPort", "80");
System.setProperty("socksProxyHost", "yyyy.com");
error message
Could not connect to server.
java.net.UnknownHostException: ftp.xxxx.com
at java.net.Inet4AddressImpl.lookupAllHostAddr(Native Method)
at java.net.InetAddress$1.lookupAllHostAddr(InetAddress.java:849)
at java.net.InetAddress.getAddressFromNameService(InetAddress.java:1202)
at java.net.InetAddress.getAllByName0(InetAddress.java:1153)
at java.net.InetAddress.getAllByName(InetAddress.java:1083)
at java.net.InetAddress.getAllByName(InetAddress.java:1019)
at java.net.InetAddress.getByName(InetAddress.java:969)
at org.apache.commons.net.SocketClient.connect(SocketClient.java:192)# client disconnected
at org.apache.commons.net.SocketClient.connect(SocketClient.java:285)
at com.ti.itg.peit.bom.TestingApache.main(TestingApache.java:44)
Thank you very much.
Gerald
Its possible..see the link below....
http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b14/java/net/ProxySelector.java
Well using Apache's common lib to access FTP using proxy server, please set the RemoteVerificationEnabled to false after creation of FTPClient object.
eg:
FTPClient fc = new FTPClient();
fc.setRemoteVerificationEnabled(false);
You can use java.net.Proxy class which is for Java 1.5 and above, this
is used to set or unset the Proxy per connection basis
By using the java.net.ProxySelector, will determine a Proxy for each Connection.

Categories

Resources