JSCH expection handling - process error result from Catch - java

i have written a program to connect to ssh server using JSCH lib, the program runs well.
in the catch block i have used
catch(JSchException ee)
{
....
}
when error occurs i get these messages,
com.jcraft.jsch.JSchException: java.net.NoRouteToHostException: No route to host
com.jcraft.jsch.JSchException: Auth fail
com.jcraft.jsch.JSchException: timeout: socket is not established
now i need to display the error message in a dialog box. for each type of error i have to display different output. plz guide me how to process these exceptions and to differentiate them..

Your server seems to be unreachable: "No route to host" so your client cannot connect to it "Auth fail" and the socket reaches its timeout.
Try to ping the server to see its response. Make sure you give the same ip address you use in the program. If you directly provide the server name, check your host file /etc/hosts on Linux or %SystemRoot%\system32\drivers\etc\hosts.

ADD below nuget packages to your solution
DiffieHellman
Org.Mentalis.Security

Related

java: getting Connection refused when trying to upload file to FTP

When trying to upload file to FTP with java program:
public void upload(String localFile,String remoteFile) throws Exception{
ftp = new FTPClient();
ftp.setControlKeepAliveTimeout(300);
ftp.connect(host,21);
ftp.enterLocalPassiveMode();
ftp.setUseEPSVwithIPv4(false);
ftp.login(user,password);
ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
FileInputStream in = null;
in = new FileInputStream(localFile);
ftp.storeFile(remoteFile,in);
in.close();
ftp.disconnect();
}
I'm getting:
java.net.ConnectException: Connection refused
at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:381)
at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:243)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:230)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:377)
at java.net.Socket.connect(Socket.java:539)
at org.apache.commons.net.SocketClient._connect(SocketClient.java:243)
at org.apache.commons.net.SocketClient.connect(SocketClient.java:202)
When I try to upload the same file with command line (from linux), I'm able to do it only when using EPSV:
llnx:~ ftp anonymous#9.20.1.116
Connected to 9.20.1.116.
220 Microsoft FTP Service
331 Anonymous access allowed, send identity (e-mail name) as password.
Password:
230 User logged in.
Remote system type is Windows_NT.
ftp> epsv
EPSV/EPRT on IPv4 off.
ftp> put /tmp/file1.xml /dir_1/file1.xml
local: /tmp/file1.xml remote: /dir_1/file1.xml
227 Entering Passive Mode (10,40,1,149,233,168).
125 Data connection already open; Transfer starting.
100% |*************************************| 117 KB 28.66 MB/s --:-- ETA
226 Transfer complete.
120032 bytes sent in 00:00 (7.96 MB/s)
So, Why does my java code getting Connection refused?
Maybe I'm not using the enterLocalPassiveMode() or setUseEPSVwithIPv4() method the right way?
*** I think the answer is how to run the EPSV command from Java program.
Thank you all.
Eithan.
This is purely a guess but java.net.ConnectException: Connection refused normally happens when there is nothing listening on the target host/port. You don't specify a port in the CLI example so maybe that is the problem. Try changing ftp.connect(host,21); to ftp.connect(host); to use the default. Also confirm the the hostnames are exactly the same.
This assumes that the error is on the call to connect(). You haven't provided a big enough stack trace to indicate either way.
Connection refused means that your TCP connection request has reached the remote server (or more correctly >>a<< remote server) but the server is not expecting / listening for an incoming connection. So it "refuses" it.
Here are the things to check:
Check that you have the correct remote hostname or IP address for the FTP server.
Check that you are using the correct port for the FTP server. Port 21 is the default, but it is possible that the server is on a non-standard port.
Check that the FTP server is actually running.
It is also possible that the problem is due to a firewall doing something deliberately confusing. But that is unlikely for a publicly routable FTP server.
Maybe I'm not using the enterLocalPassiveMode() or setUseEPSVwithIPv4() method the right way?
That can't be the problem. The stacktrace shows that your application failed while trying to establish the initial connection to the server. You haven't gotten to the point where the you can make those calls.

SFTP Connection error

I am using jsch-0.1.51.jar for sftp connection in my program and it was working fine for last 1 year but suddenly program started throwing error :
Algorithm negotiation fail. Below code:
=========================================
jsch.addIdentity(sftpIdentityFilePath);
logger.info("*****************Getting SFTP Connection******************");
session = jsch.getSession(sftpUser, sftpHost, 2222);
System.out.println("crossed seesion initialize");
session.setConfig("StrictHostKeyChecking", "no");
System.out.println("crossed seesion config");
session.connect();
System.out.println("crossed seesion connect");
channel= session.openChannel("sftp");
System.out.println("sftp server connected");
logger.info("SFTP server connected");
channel.connect();
logger.info("*****************SFTP Connected******************");
==============================================================
After finding the issue I have used a updated jar to jsch-0.1.54.jar. But it's throwing a different exception
2018-04-28 18:17:51 ERROR FileCopyMain:978 -
Session.connect: java.io.IOException: End of IO Stream Read
Also in both this cases when I am trying to run this program from Eclipse IDE then it's working fine. But when I am creating the jar file of this Java code then I am getting these issue.
Context of this SFTP connection code: I am connecting a server using private key to download files to my local
Can some body please help me out with this?
Algorithm negotiation fail.
This means that the client and server side could not agree on the encryption algorithm to be used to keep the SSH connection secure. When that happens, the server side will close the connection, leading to the IOException that you see.
The most likely explanation is that either the client side SSH implementation is out of date, or the server-side SSH implementation is out of date. There should be some clues in the jcsh "DEBUG" logging; see JSch logger - where can I configure the level. If that fails, look at the logs on the server side.
The solution will depend on what you find.

Check if ftp service is running

I want to check if a server or ip has ftp service running before connect to it. I have the next code, however if the ftp service is not active I get: "Error: failed to connect to localhost...NNREFUSED (connection refused)"
I want to capture this error to show message if service is offline or continue if is online.
ftpClient.connect(localhost, 21);
Boolean temp = FTPReply.isPositiveCompletion(ftpClient.getReplyCode());
Log.d(TAG,"respuesta server: " + Boolean.toString(temp));
The main problem is that in line 1, logcat shows the error and the lines after it are not executed.
I don't know if with FTPClient is possible, if not, something similar to make a "telnet localhost 21" manually for example.
Note: the error is not a exception.
I want to check if a server or IP has FTP service running before [trying to] connect to it
No you don't. The best way to tell whether it's running is to try to connect to it. If you get a connect exception, it isn't running. No other scheme you can devise will work as well.

JSch causes packet size exceeds maximum allowed error

I am attempting to use the JSch class (Java Secure Channel; jsch-0.1.50.jar) to connect to an SFTP server and send a file from within a ColdFusion (9.0.2) application (which runs atop Java 1.7.0_15). The basic code in question is:
jsch = classLoader.create("com.jcraft.jsch.JSch").init(); // ColdFusion-specific to load the jar
jschSession = jsch.getSession("myusername", "ftp.example.com", 22);
jschSession.setConfig("StrictHostKeyChecking", "no");
jschSession.setTimeout(60000);
jschSession.setPassword("mypassword");
jschSession.connect();
Upon connection to a Serv-U SFTP server it is giving me the following error on the Serv-U side immediately after the connection opens:
SSH Protocol Error: packet size exceeds maximum allowed.
Serv-U then closes the session, at which point JSch throws the exception:
Session.connect: java.io.IOException: End of IO Stream Read
I am new to the JSch class, and it's possible I'm missing something obvious, but I am at a loss as to where the error may lie. Connecting to the same SFTP server from the same origin with WinSCP gives no errors. Any tips on what the code is doing wrong or where to turn next for troubleshooting?
SSH Protocol Error: packet size exceeds maximum allowed
This means that the local client received some data from the remote server which wasn't properly formatted as an SFTP protocol message. The usual reason is that the server sent some kind of plain text message through the SSH connection. There are few things that might be going on:
Your .bashrc, .bash_profile, or similar shell configuration file on the server is set to print some message.
The server is poorly configured, and it's sending some kind of greeting.
The server is sending some kind of error message.
If you have access to the ssh command-line utility, you can use that to see what the server is sending. Run something like this:
$ ssh myusername#ftp.example.com -s sftp
This will open a plain SSH session to the remote server and request the SFTP subsystem, which is the same thing an SFTP client would do. If the server starts SFTP properly, you won't see any output from this command--it'll just wait until you kill it. If you see any text from the remote server, that is the problem. You'll need to figure out why the server is sending that text and prevent it.

Connection problem with sockets in java

i tried this code:
http://programming-guides.com/java/tcp-client-server-chat
(full example)
and it's working but when i switch the "host" int the client side to an external IP (insted of localhost) it give me this error:
"Cannot get IO for connection to.."
i treid to switch the port but it didnt work..
Is the server running on that remote machine? Is the port open in the firewall?
Try to netcat to the remote host. If you get an connection the example maybe wrong. If not, you cannot connect to that host: host not exists, not listen on that port, port is filtered, etc, etc.

Categories