Advertisement
Guest User

Untitled

a guest
Dec 1st, 2015
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.02 KB | None | 0 0
  1. import java.io.BufferedInputStream;
  2. import java.io.BufferedOutputStream;
  3. import java.io.BufferedReader;
  4. import java.io.BufferedWriter;
  5. import java.io.ByteArrayInputStream;
  6. import java.io.File;
  7. import java.io.FileInputStream;
  8. import java.io.FileNotFoundException;
  9. import java.io.FileOutputStream;
  10. import java.io.IOException;
  11. import java.io.InputStream;
  12. import java.io.InputStreamReader;
  13. import java.io.OutputStream;
  14. import java.io.OutputStreamWriter;
  15. import java.io.UnsupportedEncodingException;
  16. import java.io.Writer;
  17.  
  18. import com.jcraft.jsch.ChannelSftp;
  19. import com.jcraft.jsch.Channel;
  20. import com.jcraft.jsch.JSch;
  21. import com.jcraft.jsch.JSchException;
  22. import com.jcraft.jsch.Session;
  23. import com.jcraft.jsch.SftpException;
  24.  
  25. /** Demonstrates a connection to a remote host via SSH. **/
  26. public class SshConnection
  27. {
  28. private String user = "arikbidny"; // Username of ssh account on remote machine
  29. private String host = "192.168.160.129"; // Hostname of the remote machine (eg: inst.eecs.berkeley.edu)
  30. private String password = "trhe422264"; // Password associated with your ssh account
  31. private String command; // Remote command you want to invoke
  32. private int port = 22;
  33. private ChannelSftp channelSftp = null;
  34. private String SFTPWORKINGDIR = "/home/arikbidny/"; // home directory on remote machine
  35.  
  36. public SshConnection(String user, String host, String password) {
  37. super();
  38. this.user = user;
  39. this.host = host;
  40. this.password = password;
  41. this.port = port;
  42. }
  43.  
  44.  
  45. // default constructor
  46. public SshConnection() {
  47. super();
  48. this.user = user;
  49. this.host = host;
  50. this.password = password;
  51. this.port = port;
  52. }
  53.  
  54. public String getUser() {
  55. return user;
  56. }
  57.  
  58. public void setUser(String user) {
  59. this.user = user;
  60. }
  61.  
  62. public String getHost() {
  63. return host;
  64. }
  65.  
  66. public void setHost(String host) {
  67. this.host = host;
  68. }
  69.  
  70. public String getPassword() {
  71. return password;
  72. }
  73.  
  74. public void setPassword(String password) {
  75. this.password = password;
  76. }
  77.  
  78. public String getCommand() {
  79. return command;
  80. }
  81.  
  82. public void setCommand(String command) {
  83. this.command = command;
  84. }
  85.  
  86. public void makeFolder(String foldername) throws JSchException, InterruptedException{
  87. String folderName = foldername;
  88. //String command = "mkdir " + folderName + "\n";
  89. String command = "mkdir " + foldername + "\n";
  90. JSch jsch = new JSch();
  91. Session session = jsch.getSession(this.user, this.host, this.port);
  92. session.setPassword(password);
  93. session.setConfig("StrictHostKeyChecking", "no");
  94. session.connect();
  95. Channel channel = session.openChannel("shell");
  96. InputStream is = new ByteArrayInputStream(command.getBytes());
  97. channel.setInputStream(is);
  98. channel.setOutputStream(System.out);
  99. channel.connect();
  100. Thread.sleep(3*1000);
  101.  
  102. // Disconnect (close connection, clean up system resources)
  103. channel.disconnect();
  104. session.disconnect();
  105. }
  106.  
  107. public void uploadToServer() {
  108. try{
  109. JSch jsch = new JSch();
  110. Session session = jsch.getSession(this.user, this.host, this.port);
  111. session.setPassword(password);
  112. java.util.Properties config = new java.util.Properties();
  113. config.put("StrictHostKeyChecking", "no");
  114. session.setConfig(config);
  115. session.connect();
  116. Channel channel = session.openChannel("sftp");
  117. channel.connect();
  118. channelSftp = (ChannelSftp)channel;
  119. channelSftp.cd(SFTPWORKINGDIR);
  120. File f = new File("F:/text.txt");
  121. channelSftp.put(new FileInputStream(f), f.getName());
  122.  
  123. // Disconnect (close connection, clean up system resources)
  124. session.disconnect();
  125. channel.disconnect();
  126. }catch(Exception ex){
  127. ex.printStackTrace();
  128. }
  129. }
  130.  
  131. public void downloadFromServer(){
  132. try{
  133. JSch jsch = new JSch();
  134. Session session = jsch.getSession(this.user, this.host, this.port);
  135. session.setPassword(password);
  136. java.util.Properties config = new java.util.Properties();
  137. config.put("StrictHostKeyChecking", "no");
  138. session.setConfig(config);
  139. session.connect();
  140. Channel channel = session.openChannel("sftp");
  141. channel.connect();
  142. channelSftp = (ChannelSftp)channel;
  143. channelSftp.cd(SFTPWORKINGDIR);
  144. byte[] buffer = new byte[1024];
  145. BufferedInputStream bis = new BufferedInputStream(channelSftp.get("text.txt"));
  146. File newFile = new File("F:/text.txt");
  147. OutputStream os = new FileOutputStream(newFile);
  148. BufferedOutputStream bos = new BufferedOutputStream(os);
  149. int readCount;
  150. //System.out.println("Getting: " + theLine);
  151. while( (readCount = bis.read(buffer)) > 0) {
  152. System.out.println("Writing: " );
  153. bos.write(buffer, 0, readCount);
  154. }
  155. bis.close();
  156. bos.close();
  157.  
  158. // Disconnect (close connection, clean up system resources)
  159. session.disconnect();
  160. channel.disconnect();
  161. }catch(Exception ex){
  162. ex.printStackTrace();
  163. }
  164. }
  165.  
  166. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement