CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Aug 2011
    Posts
    22

    [RESOLVED] Retrieve files from remote machine

    Hi,
    I have written the following code to establish connection to the remote unix machine which works fine. How do I retrieve a particular file from that system?

    Code:
    package my.userauthki;
    
    import com.jcraft.jsch.*;
    import java.awt.*;
    import javax.swing.*;
    
    public class UserAuthKI {
    
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) {
    		try{
    		      JSch jsch=new JSch();
    
    		      String host=null;
    		      if(args.length>0){
    		        host=args[0];
    		      }
    		      else{
    		        host=JOptionPane.showInputDialog("Enter username@hostname",
    		                                         System.getProperty("user.name")+
    		                                         "@localhost"); 
    		      }
    		      String user=host.substring(0, host.indexOf('@'));
    		      host=host.substring(host.indexOf('@')+1);
    
    		      Session session=jsch.getSession(user, host, 22);
    
    		      // username and passphrase will be given via UserInfo interface.
    		      UserInfo ui=new MyUserInfo();
    		      session.setUserInfo(ui);
    		      session.connect();
    
    		      Channel channel=session.openChannel("shell");
    
    		      channel.setInputStream(System.in);
    		      channel.setOutputStream(System.out);
    
    		      channel.connect();
    		    }
    		    catch(Exception e){
    		      System.out.println(e);
    		    }
    		  }
    
    		  public static class MyUserInfo implements UserInfo, UIKeyboardInteractive{
    		    public String getPassword(){ return passwd; }
    		    public boolean promptYesNo(String str){
    		      Object[] options={ "yes", "no" };
    		      int foo=JOptionPane.showOptionDialog(null, 
    		             str,
    		             "Warning", 
    		             JOptionPane.DEFAULT_OPTION, 
    		             JOptionPane.WARNING_MESSAGE,
    		             null, options, options[0]);
    		       return foo==0;
    		    }
    
    		    String passwd;
    		    JTextField passwordField=(JTextField)new JPasswordField(20);
    		  
    		    public String getPassphrase(){ return null; }
    		    public boolean promptPassphrase(String message){ return false; }
    		    public boolean promptPassword(String message){
    		      Object[] ob={passwordField}; 
    		      int result=
    		        JOptionPane.showConfirmDialog(null, ob, message,
    		                                      JOptionPane.OK_CANCEL_OPTION);
    		      if(result==JOptionPane.OK_OPTION){
    		        passwd=passwordField.getText();
    		        return true;
    		      }
    		      else{ 
    		        return false; 
    		      }
    		    }
    		    public void showMessage(String message){
    		      JOptionPane.showMessageDialog(null, message);
    		    }
    
    		    final GridBagConstraints gbc = 
    		      new GridBagConstraints(0,0,1,1,1,1,
    		                             GridBagConstraints.NORTHWEST,
    		                             GridBagConstraints.NONE,
    		                             new Insets(0,0,0,0),0,0);
    		    private Container panel;
    		    public String[] promptKeyboardInteractive(String destination,
    		                                              String name,
    		                                              String instruction,
    		                                              String[] prompt,
    		                                              boolean[] echo){
    		/*
    		//System.out.println("promptKeyboardInteractive");
    		System.out.println("destination: "+destination);
    		System.out.println("name: "+name);
    		System.out.println("instruction: "+instruction);
    		System.out.println("prompt.length: "+prompt.length);
    		System.out.println("prompt: "+prompt[0]);
    		*/
    		      panel = new JPanel();
    		      panel.setLayout(new GridBagLayout());
    
    		      gbc.weightx = 1.0;
    		      gbc.gridwidth = GridBagConstraints.REMAINDER;
    		      gbc.gridx = 0;
    		      panel.add(new JLabel(instruction), gbc);
    		      gbc.gridy++;
    
    		      gbc.gridwidth = GridBagConstraints.RELATIVE;
    
    		      JTextField[] texts=new JTextField[prompt.length];
    		      for(int i=0; i<prompt.length; i++){
    		        gbc.fill = GridBagConstraints.NONE;
    		        gbc.gridx = 0;
    		        gbc.weightx = 1;
    		        panel.add(new JLabel(prompt[i]),gbc);
    
    		        gbc.gridx = 1;
    		        gbc.fill = GridBagConstraints.HORIZONTAL;
    		        gbc.weighty = 1;
    		        if(echo[i]){
    		          texts[i]=new JTextField(20);
    		        }
    		        else{
    		          texts[i]=new JPasswordField(20);
    		        }
    		        panel.add(texts[i], gbc);
    		        gbc.gridy++;
    		      }
    
    		      if(JOptionPane.showConfirmDialog(null, panel, 
    		                                       destination+": "+name,
    		                                       JOptionPane.OK_CANCEL_OPTION,
    		                                       JOptionPane.QUESTION_MESSAGE)
    		         ==JOptionPane.OK_OPTION){
    		        String[] response=new String[prompt.length];
    		        for(int i=0; i<prompt.length; i++){
    		          response[i]=texts[i].getText();
    		        }
    			return response;
    		      }
    		      else{
    		        return null;  // cancel
    		      }
    		    }
    
    	}
    
    }

  2. #2
    Join Date
    Mar 2010
    Location
    Melbourne Australia
    Posts
    454

    Re: Retrieve files from remote machine

    and the protocol you are using to connect FTP SSH , ??? try not to put everything inside the main method ( makes bad programming practice)

  3. #3
    Join Date
    Aug 2011
    Posts
    22

    Re: Retrieve files from remote machine

    Yes @aamir121a it's ssh. Got to optimize the code.

  4. #4
    Join Date
    Mar 2010
    Location
    Melbourne Australia
    Posts
    454

    Re: Retrieve files from remote machine

    then that easy , SSH will give you remote shell , all you have to do then is SFTP ( part of SSH ) or use commands like cd and cp to navigate through files

  5. #5
    Join Date
    Aug 2011
    Posts
    22

    Re: Retrieve files from remote machine

    Yeah ok I will try with SFTP. But when I try to open any file in the shell with the vi command, some files appear to contain lot of other junk data apart from what is actually present.

    Moreover, I want to develop an application where the user can enter the filename in the GUI itself which will make a copy of that file from the remote system to my local system.

  6. #6
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Retrieve files from remote machine

    Quote Originally Posted by shik28
    Got to optimize the code.
    Premature optimization is the root of all evil - Donald Knuth.

    Never sacrifice good design principles for optimization until you know you definitely have a performance issue and you know exactly where that performance issue is. And the only way you can know where you have an issue is to have written the code and performed analysis on the code.

    BTW from years of experience I can tell you that, almost certainly, the bit of code you think you need to optimize won't in fact be the bit of code that is causing a problem. And more often than not, a timely feedback to the user is way more important to the user than having the application take a few less milliseconds.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  7. #7
    Join Date
    Mar 2010
    Location
    Melbourne Australia
    Posts
    454

    Re: Retrieve files from remote machine

    that's easy too , user input in GUI be validated Look at the end of the day I can't teach you everything Java , to try to put your questions in way that they can be answered short and simple , for the rest you dot to do your research cheers

  8. #8
    Join Date
    Aug 2011
    Posts
    22

    Re: Retrieve files from remote machine

    @keang: Agree with you.

    @aamir121a: thanks.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured