Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedInputStream;
- import java.io.BufferedOutputStream;
- import java.io.BufferedReader;
- import java.io.BufferedWriter;
- import java.io.ByteArrayInputStream;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.InputStreamReader;
- import java.io.OutputStream;
- import java.io.OutputStreamWriter;
- import java.io.UnsupportedEncodingException;
- import java.io.Writer;
- import com.jcraft.jsch.ChannelSftp;
- import com.jcraft.jsch.Channel;
- import com.jcraft.jsch.JSch;
- import com.jcraft.jsch.JSchException;
- import com.jcraft.jsch.Session;
- import com.jcraft.jsch.SftpException;
- /** Demonstrates a connection to a remote host via SSH. **/
- public class SshConnection
- {
- private String user = "arikbidny"; // Username of ssh account on remote machine
- private String host = "192.168.160.129"; // Hostname of the remote machine (eg: inst.eecs.berkeley.edu)
- private String password = "trhe422264"; // Password associated with your ssh account
- private String command; // Remote command you want to invoke
- private int port = 22;
- private ChannelSftp channelSftp = null;
- private String SFTPWORKINGDIR = "/home/arikbidny/"; // home directory on remote machine
- public SshConnection(String user, String host, String password) {
- super();
- this.user = user;
- this.host = host;
- this.password = password;
- this.port = port;
- }
- // default constructor
- public SshConnection() {
- super();
- this.user = user;
- this.host = host;
- this.password = password;
- this.port = port;
- }
- public String getUser() {
- return user;
- }
- public void setUser(String user) {
- this.user = user;
- }
- public String getHost() {
- return host;
- }
- public void setHost(String host) {
- this.host = host;
- }
- public String getPassword() {
- return password;
- }
- public void setPassword(String password) {
- this.password = password;
- }
- public String getCommand() {
- return command;
- }
- public void setCommand(String command) {
- this.command = command;
- }
- public void makeFolder(String foldername) throws JSchException, InterruptedException{
- String folderName = foldername;
- //String command = "mkdir " + folderName + "\n";
- String command = "mkdir " + foldername + "\n";
- JSch jsch = new JSch();
- Session session = jsch.getSession(this.user, this.host, this.port);
- session.setPassword(password);
- session.setConfig("StrictHostKeyChecking", "no");
- session.connect();
- Channel channel = session.openChannel("shell");
- InputStream is = new ByteArrayInputStream(command.getBytes());
- channel.setInputStream(is);
- channel.setOutputStream(System.out);
- channel.connect();
- Thread.sleep(3*1000);
- // Disconnect (close connection, clean up system resources)
- channel.disconnect();
- session.disconnect();
- }
- public void uploadToServer() {
- try{
- JSch jsch = new JSch();
- Session session = jsch.getSession(this.user, this.host, this.port);
- session.setPassword(password);
- java.util.Properties config = new java.util.Properties();
- config.put("StrictHostKeyChecking", "no");
- session.setConfig(config);
- session.connect();
- Channel channel = session.openChannel("sftp");
- channel.connect();
- channelSftp = (ChannelSftp)channel;
- channelSftp.cd(SFTPWORKINGDIR);
- File f = new File("F:/text.txt");
- channelSftp.put(new FileInputStream(f), f.getName());
- // Disconnect (close connection, clean up system resources)
- session.disconnect();
- channel.disconnect();
- }catch(Exception ex){
- ex.printStackTrace();
- }
- }
- public void downloadFromServer(){
- try{
- JSch jsch = new JSch();
- Session session = jsch.getSession(this.user, this.host, this.port);
- session.setPassword(password);
- java.util.Properties config = new java.util.Properties();
- config.put("StrictHostKeyChecking", "no");
- session.setConfig(config);
- session.connect();
- Channel channel = session.openChannel("sftp");
- channel.connect();
- channelSftp = (ChannelSftp)channel;
- channelSftp.cd(SFTPWORKINGDIR);
- byte[] buffer = new byte[1024];
- BufferedInputStream bis = new BufferedInputStream(channelSftp.get("text.txt"));
- File newFile = new File("F:/text.txt");
- OutputStream os = new FileOutputStream(newFile);
- BufferedOutputStream bos = new BufferedOutputStream(os);
- int readCount;
- //System.out.println("Getting: " + theLine);
- while( (readCount = bis.read(buffer)) > 0) {
- System.out.println("Writing: " );
- bos.write(buffer, 0, readCount);
- }
- bis.close();
- bos.close();
- // Disconnect (close connection, clean up system resources)
- session.disconnect();
- channel.disconnect();
- }catch(Exception ex){
- ex.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement