Thread: [JSch-users] SFTP with keygen
Status: Alpha
Brought to you by:
ymnk
From: Benjamin L. M. <bl...@co...> - 2012-05-22 19:10:39
|
I am trying to use an SSH key with Jsch to copy a file via SFTP from one machine to another. On the remote machine I have done: $ssh-keygen -t rsa <hit enter twice for blank passphrase> I then end up with id_rsa and id_rsa.pub inside .ssh on the remote machine. I copy these files to the local machine, which is where the java program is running. This is my code: static public final String sshuser = "<redacted>"; static public final String sshhost = "<redacted>"; static public final String privatesshkey = "id_rsa"; public boolean transferZipFile(String filename){ boolean success = true; try{ JSch jsch = new JSch(); jsch.addIdentity(privatesshkey); Session session = jsch.getSession(sshuser,sshhost,sshport); java.util.Properties config = new java.util.Properties(); config.put("StrictHostKeyChecking","no"); session.setConfig(config); session.connect(); Channel channel = session.openChannel("sftp"); channel.setInputStream(System.in); channel.setOutputStream(System.out); channel.connect(); ChannelSftp c = (ChannelSftp) channel; c.put(filename,"/data/"); c.exit(); session.disconnect(); }catch(Exception e){ System.out.println("File transfer failed"); e.printStackTrace(); success = false; } return success; } And I am getting an exception: com.jcraft.jsch.JSchException: Auth fail at com.jcraft.jsch.Session.connect(Session.java:484) at com.jcraft.jsch.Session.connect(Session.java:162) at Synchronizer.transferZipFile(Synchronizer.java:185) at Synchronizer.logoff(Synchronizer.java:137) at Synchronizer.main(Synchronizer.java:61) Any help appreciated, thanks! Sincerely, Ben May Senior Research Analyst Herbert Irving Comprehensive Cancer Center 212-851-4772 |
From: Shai A. <sh...@gm...> - 2012-05-22 20:35:21
|
You should use the public key on the client (local machine), and the private key on the server (remote machine). That's how ssh works Shai On Tue, May 22, 2012 at 10:10 PM, Benjamin L. May <bl...@co...>wrote: > > I am trying to use an SSH key with Jsch to copy a file via SFTP from one > machine to another. On the remote machine I have done: > > $ssh-keygen -t rsa > <hit enter twice for blank passphrase> > > I then end up with id_rsa and id_rsa.pub inside .ssh on the remote > machine. I copy these files to the local machine, which is where the java > program is running. This is my code: > > static public final String sshuser = "<redacted>"; > static public final String sshhost = "<redacted>"; > static public final String privatesshkey = "id_rsa"; > > > public boolean transferZipFile(String filename){ > boolean success = true; > try{ > JSch jsch = new JSch(); > jsch.addIdentity(privatesshkey); > > Session session = jsch.getSession(sshuser,sshhost,sshport); > > java.util.Properties config = new java.util.Properties(); > config.put("StrictHostKeyChecking","no"); > session.setConfig(config); > session.connect(); > > Channel channel = session.openChannel("sftp"); > channel.setInputStream(System.in); > channel.setOutputStream(System.out); > channel.connect(); > > ChannelSftp c = (ChannelSftp) channel; > > c.put(filename,"/data/"); > c.exit(); > > session.disconnect(); > }catch(Exception e){ > System.out.println("File transfer failed"); > e.printStackTrace(); > success = false; > } > return success; > } > > And I am getting an exception: > > com.jcraft.jsch.JSchException: Auth fail > at com.jcraft.jsch.Session.connect(Session.java:484) > at com.jcraft.jsch.Session.connect(Session.java:162) > at Synchronizer.transferZipFile(Synchronizer.java:185) > at Synchronizer.logoff(Synchronizer.java:137) > at Synchronizer.main(Synchronizer.java:61) > > Any help appreciated, thanks! > > Sincerely, > Ben May > Senior Research Analyst > Herbert Irving Comprehensive Cancer Center > 212-851-4772 > > > ------------------------------------------------------------------------------ > Live Security Virtual Conference > Exclusive live event will cover all the ways today's security and > threat landscape has changed and how IT managers can respond. Discussions > will include endpoint security, mobile security and the latest in malware > threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/ > _______________________________________________ > JSch-users mailing list > JSc...@li... > https://lists.sourceforge.net/lists/listinfo/jsch-users > -- SPT <https://market.android.com/details?id=org.ayal.SPT> - persistent SSH tunnels for android. |
From: <ym...@jc...> - 2012-05-23 01:58:17
|
Hi, +-From: "Benjamin L. May" <bl...@co...> -- |_Date: Tue, 22 May 2012 15:10:33 -0400 (EDT) __ | |I am trying to use an SSH key with Jsch to copy a file via SFTP from one |machine to another. On the remote machine I have done: | |$ssh-keygen -t rsa |<hit enter twice for blank passphrase> | |I then end up with id_rsa and id_rsa.pub inside .ssh on the remote |machine. Have you appended 'id_rsa.pub' to '~/.ssh/authorized_keys' on the remote, and taken care for its file permission? $ cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys $ chmod go-rwx ~/.ssh/authorized_keys Try the following example on the local to check if the remote settings works well, http://www.jcraft.com/jsch/examples/UserAuthPubKey.java.html And then, you don't have to locate 'id_rsa' on the remote. It is the secrete. If there is not the strong requirements to do so, you should not. | I copy these files to the local machine, which is where the java |program is running. This is my code: | | static public final String sshuser = "<redacted>"; | static public final String sshhost = "<redacted>"; | static public final String privatesshkey = "id_rsa"; If your code does not work, how about setting the abolute path of 'id_rsa'? static public final String privatesshkey = "/home/name/.ssh/id_rsa"; Sincerely, -- Atsuhiko Yamanaka JCraft,Inc. 1-14-20 HONCHO AOBA-KU, SENDAI, MIYAGI 980-0014 Japan. Tel +81-22-723-2150 Skype callto://jcraft/ Twitter: http://twitter.com/ymnk Facebook: http://facebook.com/aymnk |