SCP using ganymed / Java - no such file or directory? - java

I am trying to SCP a file from a Windows server (with openSSH / Cygwin installed) to a Mac using Java's Ganymed. I get the path to my current directory programmatically, but for some reason it's saying the local directory isn't found. It is absolutely the correct path, so I don't know what's going on.
Connection conn;
String hostname = "10.10.1.2";
String username = "myuser";
String password = "mypass";
String localDir = System.getProperty("user.dir");
String remoteFile = "/path/to/the/file.txt";
Connection conn = new Connection(hostname);
conn.connect();
conn.authenticateWithPassword(username, password);
SCPClient scp = conn.createSCPClient();
scp.put(remoteFile, 2815, localDir, "0644");
conn.close();
Because I am using the .getProperty method, I know the path is right. Also, I have separate methods which ssh to the windows machine and start/stop a service, so I know the credentials are right. So why does this not work? Thanks!

I gather this code runs on the Mac, and it's supposed to copy a file from the Windows system to the mac?
scp.put(remoteFile, 2815, localDir, "0644");
The put() operation transfers a file from the local system to the remote system. You want to call get(), which transfers a file from the remote system to the local system. Replace the put() line with something like this:
scp.get(remoteFile, localDir);

Related

jsch ssh connection can't get authorized_keys

i'm trying to make a connection via ssh from windows to a unix server
my goal to have it in my java app so i cann run command without inputting passwords on each connect
right now i'm trying to understand what i'm doing wrong with keys
I generated a key in Tectia and uploaded it to server;
I can see it in .ssh as 2798 Apr 17 10:56 authorized_keys
my connection setup looks like this
...
JSch jsch = new JSch();
jsch.setKnownHosts("~/.ssh/know_hosts");
jsch.addIdentity("~/.ssh/authorized_keys");
System.out.println("identity added ");
Session session=jsch.getSession(user, host, 22);
session.setConfig("PreferredAuthentications", "publickey");
System.out.println("session created.");
session.connect();
System.out.println("Connected");
....
and as a result of this i'm getting this error
com.jcraft.jsch.JSchException: java.io.FileNotFoundException:
C:\Users\User\ .ssh\authorized_keys (The system cannot find the path
specified)
it's looking for the key on my local computer and not connecting to the server
what am I going wrong with these keys ?
The argument to addIdentity is a local path to your private key.
Instead, you are giving it a path to a file that:
Would contain a public key;
Does not exit locally anyway.

Error while connecting to windows drive from linux machine [via java program]

I am trying to connect to windows network drive using Jcifs = 1.3.17 in java
code
String baseAddress = "smb://file-123/XYZ_Others/"
String DOMAIN = "XYZ"
smbFile = new SmbFile(baseAddress, new NtlmPasswordAuthentication(DOMAIN, userName, password))
//How i am using smbFile
final boolean isDirectory = smbFile.isDirectory(); //getting error here
If i run the code from mac it is working fine [it can able to connect] however if i try to run from linux [amazon cloud] i am getting following error
cifs.smb.SmbException: Failed to connect to server
at jcifs.smb.SmbFile.connect0(SmbFile.java:882) ~[jcifs-1.3.17.jar:?]
at jcifs.smb.SmbFile.queryPath(SmbFile.java:1335) ~[jcifs-1.3.17.jar:?]
at jcifs.smb.SmbFile.exists(SmbFile.java:1417) ~[jcifs-1.3.17.jar:?]
at jcifs.smb.SmbFile.isDirectory(SmbFile.java:1490) ~[jcifs-1.3.17.jar:?]
When i try to use smb client from command prompt
smbclient -L smb://test/ -U username -W ANT -R host -D DIR
Getting error as
Connection to smb: failed (Error NT_STATUS_BAD_NETWORK_NAME)

Folder does not exists when changing directory using jcraft.jsch

I'm running on a Linux machine a Java program that uses jcraft.jsch library to connect to an external sftp server. The code looks like:
JSch jsch = new JSch();
Session session = null;
Channel channel = null;
ChannelSftp c = null;
session = jsch.getSession(ftpUserName, ftpHost, ftpPort);
session.setPassword(ftpPassword);
channel = session.openChannel("sftp");
channel.connect();
c = (ChannelSftp)channel;
fn = c.ls("/Inbox");
c.cd("/Inbox"); //-- this line throws an error
For some reason when I run the change directory command "c.cd" I get:
4: Folder not found: /drwxr-x--- 2 ftpadmin ftpadmin 0 Jan 01 1970 /Inbox
It is weird because the listing (c.ls) of that folder does not throws an exception.
Furthermore, if I lftp from the command line from the same Linux server I can cd without any problems.
The stacktrace points to a _stat method inside the cd method.
at com.jcraft.jsch.ChannelSftp.throwStatusError(ChannelSftp.java:2108)
at com.jcraft.jsch.ChannelSftp._stat(ChannelSftp.java:1676)
at com.jcraft.jsch.ChannelSftp.cd(ChannelSftp.java:290)
at BW_Utilities.ftp.test.testFtpJsch(test.java:81)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
The folder structure of the remote site looks like the following when I connect using Filezilla from my desktop:
I just executed the same Java code on my windows desktop machine and the cd command worked. (Windows machine runs JDK 1.6.0_29 while the Linux server runs JRE 1.6.0.27)
Does jsch relies on some other library at the OS level at the client side?
Any idea how to proceed to troubleshoot this problem?
important UPDATE
I was able to reproduce the error on my dev machine. It got to do with jsch versions being used. The linux server is using jsch-0.1.31 while the dev machine uses jsch-0.1.52. It seems that whatever is causing the error is already solved in version 0.1.52. Wooot! Wooot! Finally!
Thanks
Upgrading to jsch version 0.1.52 fixes the issue.
Try doing the following.
fn = c.ls("Inbox");
c.cd("Inbox");

Copy files from local window machine to remote windows machine using java

I have searched a lot but couldn't get a solution for this. I need to copy a file from local windows machine to remote windows machine using java program. I have tried with JSch,
JSch jsch = new JSch();
Session session = null;
session = jsch.getSession("username","hostname",22);
session.setPassword("password");
session.setConfig("StrictHostKeyChecking", "no");
session.connect();
ChannelSftp channel = null;
channel = (ChannelSftp)session.openChannel("sftp");
channel.connect();
File localFile = new File("filePath");
//If you want you can change the directory using the following line.
channel.cd("E:/xxx");
channel.put(new FileInputStream(localFile),localFile.getName());
channel.disconnect();
session.disconnect();
While executing the above code, i am facing the below error,
Exception in thread "main" 2: No such file
at com.jcraft.jsch.ChannelSftp.throwStatusError(ChannelSftp.java:2846)
at com.jcraft.jsch.ChannelSftp._realpath(ChannelSftp.java:2340)
at com.jcraft.jsch.ChannelSftp.cd(ChannelSftp.java:342)
I have cygwin installed in remote windows machine. It seems Jsch is not able to find the windows path. The same code works properly when copying files from windows machine to linux machine.
Please tel me a solution for the above problem or is there any other options for this to achieve in java ? Thanks
In order to resolve a Windows path with a drive letter you may need to use the /cygdrive prefix. In this case, your cd method call should be called with the parameter /cygdrive/e/xxx.

How to copy a file from Linux System to Windows system using Java program?

How to copy a file from Linux System to Windows system using Java program?
Thanks for your help.
I want to copy file from linux : /inet/apps/test.java to windows System1: C:\apps\test
We can use following program to copy
public static void copyFiles(String fromFile, String toFile ){
FileInputStream from = null;
FileOutputStream to = null;
try {
from = new FileInputStream(fromFile);
to = new FileOutputStream(toFile);
byte[] buffer = new byte[4096];
int bytesRead;
while ((bytesRead = from.read(buffer)) != -1)
to.write(buffer, 0, bytesRead); // write
}catch(Exception e){e.printStackTrace();}
finally {
if (from != null)
try { from.close();} catch (IOException e) {}
if (to != null)try {to.close();} catch (IOException e) {}
}
}
This program is running on linux.
so fromFile = /inet/apps/test. What will be the toFile path. If i use simply C:\apps\test then how applicaiton recognise the target as System1.
Java makes no diffeence between Windows and Linux files. So, as long as you have access to both filesystem in the computer your java program is running, you can just copy them.
I think you are asking about some
properties for the program.
In that case the properties, should
be configurable. You can keep the
properties file in the same
directory as your Java program or in
the class path.
The property file might look like :
windows.filepath = C:\user\somefile.txt
unix.filepath = /inet/apps/test.txt
So when you port environments. You
don't need to change the properties.
If you are asking about how to port
test.java to windows, then just copy
the file to JAVA_HOME directory on
windows and then you are good to go.
Or If you have a Dual boot system.
You can access your linux drive
from windows, but not the other
way around.
If the Unix system has the Window file system cross-mounted (e.g. via an SMB share), you should be able to find the Unix pathname that corresponds to the Windows destination and copy as you are currently doing.
Otherwise, you will need to use a file transfer protocol of some kind to copy the file.
There's no Java magic that allows you to magically write files to a different computer. The operating system has to be set up to allow this to happen.
FOLLOW UP - you asked:
I have no thought about the magic. So my question was how to copy a file from Windows to Linux. Normally we do FTP on unix Without mounting or we use FileZilla tool to transfer happens. Here if we want to do same thing though java then how to do that?
I don't know how I can say this differently to make you understand, but here goes:
Your choices in Java are basically the same:
You can use FTP. For example on the destination machine, turn pathname of the source file into a "ftp://..." URL, and use java.net.URL.connect() to pull it. There are probably 3rd-party Java libraries that you can use to "push" the file to a FTP server.
If your OS is setup with the file systems cross-mount, you can do a regular file copy, much as your code does.
You can use java.lang.System.exec(...) to run some Windows specific command line utility to do the copying.
In all cases, you will need to figure out how to map pathnames between the Windows and Linux worlds.
You can try the copy() method of the java.nio.file.Files class, of course we assume that your application can access both the path on Linux and Windows either by mapping the folders or something similar. For example
Files.copy(Paths.get(source), Paths.get(destination), StandardCopyOption.REPLACE_EXISTING, COPY_ATTRIBUTES);

Categories

Resources