Advertisement
Guest User

Untitled

a guest
Apr 13th, 2016
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.28 KB | None | 0 0
  1. package ru.smoltelecom.zapret.protocol;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.InputStreamReader;
  5.  
  6. import ru.smoltelecom.zapret.protocol.auth.UserPassInfo;
  7.  
  8. import com.jcraft.jsch.Channel;
  9. import com.jcraft.jsch.ChannelExec;
  10. import com.jcraft.jsch.ChannelSftp;
  11. import com.jcraft.jsch.ChannelShell;
  12. import com.jcraft.jsch.JSch;
  13. import com.jcraft.jsch.Session;
  14. import com.jcraft.jsch.UserInfo;
  15.  
  16. import expect4j.Expect4j;
  17.  
  18. public class SFTP {
  19.  
  20.     private String host;
  21.     private String userName;
  22.     private String userPassword;
  23.     private int port;
  24.  
  25.     private Session session;
  26.     private Channel channel;
  27.    
  28.     private ChannelShell shell;
  29.     private Expect4j     expect;
  30.     private ExpectHelper expectHelper;
  31.  
  32.     private JSch jsch;
  33.  
  34.     public SFTP(String host, String userName, String userPassword, int port) {
  35.         this.host = host;
  36.         this.userName = userName;
  37.         this.userPassword = userPassword;
  38.         this.port = port;
  39.     }
  40.  
  41.     public void open() throws Exception {
  42.         jsch = new JSch();
  43.         session = jsch.getSession(userName, host, port);
  44.         UserInfo ui = new UserPassInfo(userPassword);
  45.         session.setUserInfo(ui);
  46.         session.connect();
  47.     }
  48.    
  49.     public ChannelSftp openSFTPChannel() throws Exception {
  50.         channel = session.openChannel("sftp");
  51.         channel.connect();
  52.         return (ChannelSftp)channel;
  53.     }
  54.    
  55.     public ExpectHelper getExpect(String rootPassword) throws Exception {
  56.        
  57.         if(shell==null){
  58.             shell  = (ChannelShell)session.openChannel("shell");
  59.             expect = new Expect4j(shell.getInputStream(),shell.getOutputStream());
  60.             expect.setDefaultTimeout(1000L);
  61.             expectHelper = new ExpectHelper(expect,rootPassword);
  62.         }
  63.        
  64.         return expectHelper;
  65.     }
  66.    
  67.     public void closeExpect(){
  68.         shell.disconnect();
  69.         expect.close();
  70.     }
  71.    
  72.     public String execCommand(String command) throws Exception {
  73.        
  74.         Channel     channel = session.openChannel("exec");
  75.         ChannelExec exec    = (ChannelExec)channel;
  76.  
  77.         BufferedReader in = new BufferedReader(new InputStreamReader(exec.getInputStream()));
  78.         exec.setCommand(command);
  79.         exec.connect();
  80.        
  81.         String msg = null;
  82.         String result = "";
  83.        
  84.             while ((msg = in.readLine()) != null) {
  85.                 result+=msg+"\n";
  86.                 System.out.print(msg+"\n");
  87.             }
  88.  
  89.         exec.disconnect();
  90.        
  91.         return result;
  92.        
  93.     }
  94.    
  95.     public void close(){
  96.        
  97.         if (channel != null) {
  98.             channel.disconnect();
  99.         }
  100.         if (session != null) {
  101.             session.disconnect();
  102.         }
  103.        
  104.     }
  105.  
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement