When I am trying to get connected with the ftp server for file uploading, I am getting exception com.jcraft.jsch.JSchException: session is down
Code is in groovy:
String SFTPHOST = "########"
int SFTPPORT = 22
String SFTPUSER = "########"
String SFTPPASS = "########"
String SFTPWORKINGDIR = "/QA/"
ChannelSftp sftp = null
Session session = null
try {
JSch jsch = new JSch()
session = jsch.getSession(SFTPUSER, SFTPHOST, SFTPPORT)
session.setPassword(SFTPPASS)
session.setConfig("StrictHostKeyChecking", "no")
session.setConfig("PreferredAuthentications",
"publickey,keyboard-interactive,password")
session.connect()
Channel channel = session.openChannel "sftp"
channel.connect()
sftp = channel as ChannelSftp
sftp.cd SFTPWORKINGDIR
File f = new File("Demo.csv")
sftp.put(new FileInputStream(f), f.getName())
//def fileList = sftp.ls("*")
println fileList.size()
} catch (Exception ex) {
ex.printStackTrace()
}
I got the issue...Actually JSch is not an FTP client it's an SSH client (with an included SFTP implementation). And the ftp server which i am connecting with is not a ssh server. That's why jsch is unable to connect with that ftp server. I have used apache commons ftp client and its working fine
Related
I need to setup a socks proxy (tunnel) over ssh in android with java (android studio). I searched a lot but I couldn't find any solutions. This is my code:
int assigned_port;
int local_port=8588;
int remote_port=22;
String remote_host = "server";
String login = "root";
String password = "password";
try {
JSch jsch = new JSch();
// Create SSH session. Port 22 is your SSH port which
// is open in your firewall setup.
Session session = jsch.getSession(login, remote_host, 22);
session.setPassword(password);
// Additional SSH options. See your ssh_config manual for
// more options. Set options according to your requirements.
java.util.Properties config = new java.util.Properties();
config.put("StrictHostKeyChecking", "no");
// config.put("Compression", "yes");
config.put("ConnectionAttempts","2");
session.setConfig(config);
// Connect
session.connect();
// Create the tunnel through port forwarding.
// This is basically instructing jsch session to send
// data received from local_port in the local machine to
// remote_port of the remote_host
// assigned_port is the port assigned by jsch for use,
// it may not always be the same as
// local_port.
assigned_port = session.setPortForwardingL(local_port,
remote_host, remote_port);
} catch (JSchException e) {
System.out.println("JSch:" + e.getMessage());
return;
}
if (assigned_port == 0) {
System.out.println("Port forwarding failed!");
return;
}
There is no error but it didn't work. I need a simple code of socks tunneling.
User upload the file, i need to password protect the file and then zip it put in a storage server which is different from the server where my code is running. So i use AESEncrypter to encrypt the file and jcraft.jsch.ChannelSftp to transfer the file to the server.
public ResponseEntity<ResponseWrapper> uploadFile(#RequestParam("uploads") MultipartFile file) throws Exception {
FileOutputStream fos = new FileOutputStream("outputfile.zip");
AESEncrypter aesEncrypter = new AESEncrypterBC();
aze=new AesZipFileEncrypter(fos, aesEncrypter);
aze.add(file.getOriginalFilename(), file.getInputStream(), "test123");
JSch ssh = new JSch();
Session session = ssh.getSession("username", "Servername", 22);
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.setPassword("*****");
session.connect();
Channel channel = session.openChannel("sftp");
channel.connect();
sftp = (ChannelSftp) channel;
sftpChannel.put(file.getInputStream(), "/storedfiles/outputfile.zip");
}
File is getting transferred to the server, but when i download that transferred file and try to open it says "Errors were found opening ".." you cannot extract file.. do you want to fix the problems". Not sure why i am getting this issue, also it creates a file in local server, which line is causing that?
I tried replacing this line
aze=new AesZipFileEncrypter(fos, aesEncrypter);
with
aze=new AesZipFileEncrypter("outputfile.zip", aesEncrypter);
but dint work.
I placed the file in remote server, read that in output stream and then password protected, solved my issue.
public ResponseEntity<ResponseWrapper> uploadFile(#RequestParam("uploads") MultipartFile file) throws Exception {
JSch ssh = new JSch();
Session session = ssh.getSession("username", "Servername", 22);
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.setPassword("*****");
session.connect();
Channel channel = session.openChannel("sftp");
channel.connect();
sftp = (ChannelSftp) channel;
OutputStream os = sftp.put("/storedfiles/outputfile.zip");
AESEncrypter aesEncrypter = new AESEncrypterBC();
aze=new AesZipFileEncrypter(os, aesEncrypter);
aze.add(file.getOriginalFilename(), file.getInputStream(), "test123");
if(aze != null) {
aze.close();
}
}
I am trying to execute a remote query over SSH using pubic key/private key based authentication. Following command works fine and gives me the required output as a string on a bash shell after sharing the public keys between local host and the remote server.
echo 123456 12#13:14 ABCD abc1234 | ssh -T user#abc.xyz.com
How do I achieve the same with JAVA using JSCH or SSHJ or any other similar library
This is what I have tried so far using SSHJ but it did not work for me (Connection was successful but no results)
public static void main(String... args)throws IOException {
final SSHClient ssh = new SSHClient();
ssh.loadKnownHosts();
ssh.connect("abc.xyz.com");
try {
ssh.authPublickey("user");
final Session session = ssh.startSession();
try {
final Command cmd = session.exec("123456 12#13:14 ABCD abc1234");
System.out.println(IOUtils.readFully(cmd.getInputStream()).toString());
cmd.join(5, TimeUnit.SECONDS);
System.out.println("\n** exit status: " + cmd.getExitStatus());
} finally {
session.close();
}
} finally {
ssh.disconnect();
ssh.close();
}
}
Below code uses JSCH Library and is working on my end :-
JSch jsch = new JSch();
String path = "PATH TO PRIVATE KEY";
jsch.addIdentity(path);
jsch.setConfig("StrictHostKeyChecking", "no");
Session session = jsch.getSession(userName, ipToConnect, portToConnect);
session.connect();
Channel channel = session.openChannel("exec");
((ChannelExec)channel).setCommand("COMMAND TO FIRE");
channel.setInputStream(null);
((ChannelExec)channel).setErrStream(System.err);
InputStream in = channel.getInputStream();
channel.connect();
//Read Response Here
channel.disconnect();
session.disconnect();
Note : You can use password based authentication also instead of key bases with JSCH
I'm getting a java.net.ConnectException: failed to connect to 192.168.2.100 (port 22): connect failed: ECONNREFUSED (Connection refused) when connecting to a server via SSH with JSCH-0.1.54 on Android.
The firewall is configured to let the IP adress through.
SSH connection from Windows to the server is possible via Putty.
The server's IP adress is alright.
What else can it be?
The code I'm using is below:
protected Long doInBackground(String... params)
{
try
{
System.setProperty("http.keepAlive", "false");
JSch jsch = new JSch();
Session session = jsch.getSession("root", "192.168.2.100", 22);
session.setPassword("Password");
session.setTimeout(10000);
Properties props = new Properties();
props.put("StrictHostKeyChecking", "no");
session.setConfig(props);
session.connect();
ChannelExec channel = (ChannelExec) session.openChannel("exec");
if(params[0]=="poweroff")
{
LogPublic("Server wird heruntergefahren");
}
else if(params[0]=="reboot")
{
LogPublic("Server wird neugestartet");
}
channel.setCommand(params[0]);
channel.connect();
channel.disconnect();
session.disconnect();
jsch.removeAllIdentity();
jsch = null;
return new Long(1);
}
catch (Exception ex)
{
LogPublic(ex.getMessage());
return new Long(0);
}
}
The solution was to set the server's static IP address out of my router's DHCP range as this caused an IP conflict.
I'm trying to connect to my SFTP server from a Java script.
I'm using JSch lib for my purpose. Username, password and hostname are correct but I obtain an: Auth fail error.
I've also tried to add the following lines before session.connect(), but the problem still remains.
java.util.Properties config = new java.util.Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
What do I have to put inside knownhosts.txt? The fingerprint of my server key?
public static void upload(ArrayList<File> a) {
try{
JSch jsch = new JSch();
jsch.setKnownHosts("knownhosts.txt");
Session session = jsch.getSession("username", "hostname", 22);
session.setPassword("mypassword");
session.connect();
Channel channel = session.openChannel("sftp");
channel.connect();
ChannelSftp channelSftp = (ChannelSftp) channel;
channelSftp.cd("/var/www/");
for(File object: a){
channelSftp.put(new FileInputStream(object), object.getName(), channelSftp.OVERWRITE);
}
channelSftp.exit();
session.disconnect();
} catch (Exception ex) {
ex.printStackTrace();
}
}
Do you have some advices? Thanks in advance!
Does your network/SMTP server support IP6? If your client has IP6 support, later versions of Java default to IP6, but many SMTP servers are configured on IP4. See this article here for Sending email using JSP for directions on configuring your JVM to force IP4. This needs to be set on the JVM as it is instantiated.