SSH connection from your client
machine to cloud server
Connect your Cloud server file system to your local client. This is the sample program of file uploading from your local machine to cloud server.
import java.io.File; import java.io.FileInputStream; import java.io.IOException; import com.jcraft.jsch.ChannelSftp; import com.jcraft.jsch.JSch; import com.jcraft.jsch.JSchException; import com.jcraft.jsch.Session; import com.jcraft.jsch.SftpException; public class ConnectSSH { public void connect(String dnsName, String privKey) throws IOException, SftpException { JSch jSch = new JSch(); Session session = null; ChannelSftp channel = null; try { //Authenticate through Private Key File jSch.addIdentity(privKey); //Give the user and dnsName session = jSch.getSession("huser", dnsName, 22); //Required if not a trusted host java.util.Properties config = new java.util.Properties(); config.put("StrictHostKeyChecking", "no"); session.setConfig(config); System.out.println("Connecting SSH to " + dnsName + " - Please wait for few minutes... "); session.connect(); System.out.println(session.getHost()); channel = (ChannelSftp)session.openChannel("sftp"); channel.connect(); channel.cd("/home/huser"); File localFile = new File("C:/Users/rsharma/Desktop/hivedata.txt"); channel.put(new FileInputStream(localFile), localFile.getName()); System.out.println("--------File has been Copied----------"); channel.disconnect(); session.disconnect(); } catch (JSchException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { channel = null; session = null; } } public static void main(String[] args) throws SftpException { ConnectSSH ssh = new ConnectSSH(); String privKey = "D:/Amazon Key/huser-key.pem"; try { ssh.connect("xxx-xx-xx-xxx-xx.compute-1.amazonaws.com", privKey); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
Great work.....keep it up Ravi.......
ReplyDelete