Advertisement
Varun_Krishna

scp: command error: Ambiguous target file.

Feb 28th, 2014
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.92 KB | None | 0 0
  1. /* -*-mode:java; c-basic-offset:2; indent-tabs-mode:nil -*- */
  2. /**
  3.  * This program will demonstrate the file transfer from local to remote.
  4.  *   $ CLASSPATH=.:../build javac ScpTo.java
  5.  *   $ CLASSPATH=.:../build java ScpTo file1 user@remotehost:file2
  6.  * You will be asked passwd.
  7.  * If everything works fine, a local file 'file1' will copied to
  8.  * 'file2' on 'remotehost'.
  9.  *
  10.  */
  11. import com.jcraft.jsch.*;
  12. import java.awt.*;
  13. import javax.swing.*;
  14. import java.io.*;
  15.  
  16. public class ScpFileUpload{
  17.   public static void main(String[] arg){
  18.      
  19.  
  20.     FileInputStream fis=null;
  21.     try{
  22.  
  23.       /*String lfile=arg[0];
  24.       String user=arg[1].substring(0, arg[1].indexOf('@'));
  25.       arg[1]=arg[1].substring(arg[1].indexOf('@')+1);
  26.       String host=arg[1].substring(0, arg[1].indexOf(':'));
  27.       String rfile=arg[1].substring(arg[1].indexOf(':')+1);*/
  28.       String user = "Administrator";
  29.       String host = "localhost";
  30.       //String rfile = "Readme.txt";
  31.  
  32.       JSch jsch=new JSch();
  33.       Session session=jsch.getSession(user, host, 22);
  34.  
  35.       // username and password will be given via UserInfo interface.
  36.       UserInfo ui=new MyUserInfo();
  37.       session.setUserInfo(ui);
  38.       session.connect();
  39.  
  40.       //boolean ptimestamp = true;
  41.       boolean ptimestamp = false;
  42.  
  43.       // exec 'scp -t rfile' remotely
  44.       //String command="scp " + (ptimestamp ? "-p" :"") +" -t "+rfile;
  45.       String command = "scp -t 1.txt Administrator@localhost:/SCPDemo/1.txt";
  46.       Channel channel=session.openChannel("exec");
  47.       ((ChannelExec)channel).setCommand(command);
  48.  
  49.       // get I/O streams for remote scp
  50.       OutputStream out=channel.getOutputStream();
  51.       InputStream in=channel.getInputStream();
  52.  
  53.       channel.connect();
  54.  
  55.       if(checkAck(in)!=0){
  56.     System.exit(0);
  57.       }
  58.  
  59.       /*File _lfile = new File(lfile);
  60.  
  61.       if(ptimestamp){
  62.         command="T "+(_lfile.lastModified()/1000)+" 0";
  63.         // The access time should be sent here,
  64.         // but it is not accessible with JavaAPI ;-<
  65.         command+=(" "+(_lfile.lastModified()/1000)+" 0\n");
  66.         out.write(command.getBytes()); out.flush();
  67.         if(checkAck(in)!=0){
  68.       System.exit(0);
  69.         }
  70.       }*/
  71.  
  72.       // send "C0644 filesize filename", where filename should not include '/'
  73.       /*long filesize=_lfile.length();
  74.       command="C0644 "+filesize+" ";
  75.       if(lfile.lastIndexOf('/')>0){
  76.         command+=lfile.substring(lfile.lastIndexOf('/')+1);
  77.       }
  78.       else{
  79.         command+=lfile;
  80.       }
  81.       command+="\n";
  82.       out.write(command.getBytes()); out.flush();
  83.       if(checkAck(in)!=0){
  84.     System.exit(0);
  85.       }
  86.  
  87.       // send a content of lfile
  88.       fis=new FileInputStream(lfile);
  89.       byte[] buf=new byte[1024];
  90.       while(true){
  91.         int len=fis.read(buf, 0, buf.length);
  92.     if(len<=0) break;
  93.         out.write(buf, 0, len); //out.flush();
  94.       }
  95.       fis.close();
  96.       fis=null;
  97.       // send '\0'
  98.       buf[0]=0; out.write(buf, 0, 1); out.flush();
  99.       if(checkAck(in)!=0){
  100.     System.exit(0);
  101.       }
  102.       out.close();
  103.  
  104.       channel.disconnect();
  105.       session.disconnect();
  106.  
  107.       System.exit(0);*/
  108.     }
  109.     catch(Exception e){
  110.       System.out.println(e);
  111.       try{if(fis!=null)fis.close();}catch(Exception ee){}
  112.     }
  113.   }//end of main
  114.  
  115.   static int checkAck(InputStream in) throws IOException{
  116.     int b=in.read();
  117.     // b may be 0 for success,
  118.     //          1 for error,
  119.     //          2 for fatal error,
  120.     //          -1
  121.     if(b==0) return b;
  122.     if(b==-1) return b;
  123.  
  124.     if(b==1 || b==2){
  125.       StringBuffer sb=new StringBuffer();
  126.       int c;
  127.       do {
  128.     c=in.read();
  129.     sb.append((char)c);
  130.       }
  131.       while(c!='\n');
  132.       if(b==1){ // error
  133.     System.out.print(sb.toString());
  134.       }
  135.       if(b==2){ // fatal error
  136.     System.out.print(sb.toString());
  137.       }
  138.     }
  139.     return b;
  140.   }
  141.  
  142.   public static class MyUserInfo implements UserInfo, UIKeyboardInteractive{
  143.     public String getPassword(){ return passwd; }
  144.     public boolean promptYesNo(String str){
  145.       Object[] options={ "yes", "no" };
  146.       int foo=JOptionPane.showOptionDialog(null,
  147.              str,
  148.              "Warning",
  149.              JOptionPane.DEFAULT_OPTION,
  150.              JOptionPane.WARNING_MESSAGE,
  151.              null, options, options[0]);
  152.        return foo==0;
  153.     }
  154.  
  155.     String passwd;
  156.     JTextField passwordField=(JTextField)new JPasswordField(20);
  157.  
  158.     public String getPassphrase(){ return null; }
  159.     public boolean promptPassphrase(String message){ return true; }
  160.     public boolean promptPassword(String message){
  161.       Object[] ob={passwordField};
  162.       int result=
  163.       JOptionPane.showConfirmDialog(null, ob, message,
  164.                     JOptionPane.OK_CANCEL_OPTION);
  165.       if(result==JOptionPane.OK_OPTION){
  166.     passwd=passwordField.getText();
  167.     return true;
  168.       }
  169.       else{ return false; }
  170.     }
  171.     public void showMessage(String message){
  172.       JOptionPane.showMessageDialog(null, message);
  173.     }
  174.     final GridBagConstraints gbc =
  175.       new GridBagConstraints(0,0,1,1,1,1,
  176.                              GridBagConstraints.NORTHWEST,
  177.                              GridBagConstraints.NONE,
  178.                              new Insets(0,0,0,0),0,0);
  179.     private Container panel;
  180.     public String[] promptKeyboardInteractive(String destination,
  181.                                               String name,
  182.                                               String instruction,
  183.                                               String[] prompt,
  184.                                               boolean[] echo){
  185.       panel = new JPanel();
  186.       panel.setLayout(new GridBagLayout());
  187.  
  188.       gbc.weightx = 1.0;
  189.       gbc.gridwidth = GridBagConstraints.REMAINDER;
  190.       gbc.gridx = 0;
  191.       panel.add(new JLabel(instruction), gbc);
  192.       gbc.gridy++;
  193.  
  194.       gbc.gridwidth = GridBagConstraints.RELATIVE;
  195.  
  196.       JTextField[] texts=new JTextField[prompt.length];
  197.       for(int i=0; i<prompt.length; i++){
  198.         gbc.fill = GridBagConstraints.NONE;
  199.         gbc.gridx = 0;
  200.         gbc.weightx = 1;
  201.         panel.add(new JLabel(prompt[i]),gbc);
  202.  
  203.         gbc.gridx = 1;
  204.         gbc.fill = GridBagConstraints.HORIZONTAL;
  205.         gbc.weighty = 1;
  206.         if(echo[i]){
  207.           texts[i]=new JTextField(20);
  208.         }
  209.         else{
  210.           texts[i]=new JPasswordField(20);
  211.         }
  212.         panel.add(texts[i], gbc);
  213.         gbc.gridy++;
  214.       }
  215.  
  216.       if(JOptionPane.showConfirmDialog(null, panel,
  217.                                        destination+": "+name,
  218.                                        JOptionPane.OK_CANCEL_OPTION,
  219.                                        JOptionPane.QUESTION_MESSAGE)
  220.          ==JOptionPane.OK_OPTION){
  221.         String[] response=new String[prompt.length];
  222.         for(int i=0; i<prompt.length; i++){
  223.           response[i]=texts[i].getText();
  224.         }
  225.     return response;
  226.       }
  227.       else{
  228.         return null;  // cancel
  229.       }
  230.     }
  231.   }
  232. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement