程式扎記: [ JScp 代碼範本 ] Scp local file to remote machine

標籤

2015年3月25日 星期三

[ JScp 代碼範本 ] Scp local file to remote machine

Preface
This program will demonstrate the file transfer from local to remote through JSch.

Sample Code: 
- ScpToDemo.groovy
  1. package examples  
  2.   
  3. import java.awt.Container  
  4. import java.awt.GridBagConstraints  
  5. import java.awt.GridBagLayout  
  6. import java.awt.Insets  
  7.   
  8. import javax.swing.JLabel  
  9. import javax.swing.JOptionPane  
  10. import javax.swing.JPanel  
  11. import javax.swing.JPasswordField  
  12. import javax.swing.JTextField  
  13.   
  14. import com.jcraft.jsch.Channel  
  15. import com.jcraft.jsch.ChannelExec  
  16. import com.jcraft.jsch.JSch  
  17. import com.jcraft.jsch.Session  
  18. import com.jcraft.jsch.UIKeyboardInteractive  
  19. import com.jcraft.jsch.UserInfo  
  20.   
  21.   
  22. def user="john"  
  23. def host="192.168.192.134"  
  24. def passwd="john7810"  
  25. def lfile="test.txt"  
  26. def rfile="~/test.txt"  
  27.   
  28. public static int checkAck(InputStream ins) throws IOException{  
  29.     int b=ins.read();  
  30.     // b may be 0 for success,  
  31.     //          1 for error,  
  32.     //          2 for fatal error,  
  33.     //          -1  
  34.     if(b==0return b;  
  35.     if(b==-1return b;  
  36.   
  37.     if(b==1 || b==2){  
  38.         StringBuffer sb=new StringBuffer();  
  39.         int c;  
  40.         c=ins.read();  
  41.         sb.append((char)c);  
  42.         while(c!='\n') {  
  43.             c=ins.read();  
  44.             sb.append((char)c);  
  45.         }  
  46.   
  47.         if(b==1){ // error  
  48.             System.out.print(sb.toString());  
  49.         }  
  50.         if(b==2){ // fatal error  
  51.             System.out.print(sb.toString());  
  52.         }  
  53.     }  
  54.     return b;  
  55. }  
  56.   
  57. public class ScpUserInfo implements UserInfo, UIKeyboardInteractive{  
  58.     String passwd;  
  59.     JTextField passwordField=(JTextField)new JPasswordField(20);  
  60.   
  61.     public String getPassword(){  
  62.         return passwd;  
  63.     }  
  64.     public boolean promptYesNo(String str){  
  65.         printf("\t[Info] With Prompt:\n%s\n", str)  
  66.         return true  
  67.     }  
  68.     public String getPassphrase(){  
  69.         return null;  
  70.     }  
  71.     public boolean promptPassphrase(String message){  
  72.         return true;  
  73.     }  
  74.     public boolean promptPassword(String message){  
  75.         printf("\t[Info] Prompt for password:\n%s\n", message)  
  76.         return true  
  77.     }  
  78.     public void showMessage(String message){  
  79.         JOptionPane.showMessageDialog(null, message);  
  80.     }  
  81.     final GridBagConstraints gbc =  
  82.     new GridBagConstraints(0,0,1,1,1,1,  
  83.     GridBagConstraints.NORTHWEST,  
  84.     GridBagConstraints.NONE,  
  85.     new Insets(0,0,0,0),0,0);  
  86.     private Container panel;  
  87.     public String[] promptKeyboardInteractive(String destination,  
  88.             String name,  
  89.             String instruction,  
  90.             String[] prompt,  
  91.             boolean[] echo){  
  92.         panel = new JPanel();  
  93.         panel.setLayout(new GridBagLayout());  
  94.   
  95.         gbc.weightx = 1.0;  
  96.         gbc.gridwidth = GridBagConstraints.REMAINDER;  
  97.         gbc.gridx = 0;  
  98.         panel.add(new JLabel(instruction), gbc);  
  99.         gbc.gridy++;  
  100.   
  101.         gbc.gridwidth = GridBagConstraints.RELATIVE;  
  102.   
  103.         JTextField[] texts=new JTextField[prompt.length];  
  104.         for(int i=0; i
  105.             gbc.fill = GridBagConstraints.NONE;  
  106.             gbc.gridx = 0;  
  107.             gbc.weightx = 1;  
  108.             panel.add(new JLabel(prompt[i]),gbc);  
  109.   
  110.             gbc.gridx = 1;  
  111.             gbc.fill = GridBagConstraints.HORIZONTAL;  
  112.             gbc.weighty = 1;  
  113.             if(echo[i]){  
  114.                 texts[i]=new JTextField(20);  
  115.             }  
  116.             else{  
  117.                 texts[i]=new JPasswordField(20);  
  118.             }  
  119.             panel.add(texts[i], gbc);  
  120.             gbc.gridy++;  
  121.         }  
  122.   
  123.         if(JOptionPane.showConfirmDialog(null, panel,  
  124.         destination+": "+name,  
  125.         JOptionPane.OK_CANCEL_OPTION,  
  126.         JOptionPane.QUESTION_MESSAGE)  
  127.         ==JOptionPane.OK_OPTION){  
  128.             String[] response=new String[prompt.length];  
  129.             for(int i=0; i
  130.                 response[i]=texts[i].getText();  
  131.             }  
  132.             return response;  
  133.         }  
  134.         else{  
  135.             return null;  // cancel  
  136.         }  
  137.     }  
  138. }  
  139.   
  140.   
  141.   
  142. JSch jsch=new JSch();  
  143. Session session=jsch.getSession(user, host, 22);  
  144.   
  145. // username and password will be given via UserInfo interface.  
  146. UserInfo ui=new ScpUserInfo(passwd:passwd);  
  147. session.setUserInfo(ui);  
  148. session.connect();  
  149.   
  150. boolean ptimestamp = true;  
  151.   
  152. // exec 'scp -t rfile' remotely  
  153. String command="scp " + (ptimestamp ? "-p" :"") +" -t "+rfile;  
  154. Channel channel=session.openChannel("exec");  
  155. printf("\t[Info] Execute command: '%s'...\n", command)  
  156. ((ChannelExec)channel).setCommand(command);  
  157.   
  158. // get I/O streams for remote scp  
  159. OutputStream out=channel.getOutputStream();  
  160. InputStream ins=channel.getInputStream();  
  161.   
  162. channel.connect();  
  163.   
  164. if(checkAck(ins)!=0){  
  165.     System.exit(0);  
  166. }  
  167.   
  168. File _lfile = new File(lfile);  
  169.   
  170. if(ptimestamp){  
  171.     command="T "+(_lfile.lastModified()/1000)+" 0";  
  172.     // The access time should be sent here,  
  173.     // but it is not accessible with JavaAPI ;-<  
  174.     command+=(" "+(_lfile.lastModified()/1000)+" 0\n");  
  175.     out.write(command.getBytes()); out.flush();  
  176.     if(checkAck(ins)!=0){  
  177.         printf("\t[Error] Fail in executing scp!\n")  
  178.         System.exit(0);  
  179.     }  
  180. }  
  181.   
  182. // send "C0644 filesize filename", where filename should not include '/'  
  183. long filesize=_lfile.length();  
  184. command="C0644 "+filesize+" ";  
  185. if(lfile.lastIndexOf('/')>0){  
  186.     command+=lfile.substring(lfile.lastIndexOf('/')+1);  
  187. }  
  188. else{  
  189.     command+=lfile;  
  190. }  
  191. command+="\n";  
  192. out.write(command.getBytes()); out.flush();  
  193. if(checkAck(ins)!=0){     
  194.     System.exit(0);  
  195. }  
  196.   
  197. // send a content of lfile  
  198. fis=new FileInputStream(lfile);  
  199. byte[] buf=new byte[1024];  
  200. while(true){  
  201.     int len=fis.read(buf, 0, buf.length);  
  202.     if(len<=0break;  
  203.     out.write(buf, 0, len); //out.flush();  
  204. }  
  205. fis.close();  
  206. fis=null;  
  207. // send '\0'  
  208. buf[0]=0; out.write(buf, 01); out.flush();  
  209. if(checkAck(ins)!=0){  
  210.     System.exit(0);  
  211. }  
  212. out.close();  
  213.   
  214. channel.disconnect();  
  215. session.disconnect();  
  216.   
  217. System.exit(0);  

沒有留言:

張貼留言

網誌存檔

關於我自己

我的相片
Where there is a will, there is a way!