001package ssh;
002
003import grails.plugin.remotessh.SshConfig;
004
005import java.io.BufferedOutputStream;
006import java.io.File;
007import java.io.FileOutputStream;
008import java.io.IOException;
009
010import ch.ethz.ssh2.Connection;
011import ch.ethz.ssh2.SFTPv3Client;
012import ch.ethz.ssh2.SFTPv3FileHandle;
013
014/**
015 * Used by AddFTP and AddServerRemote classes This uses
016 * SSHConfig Interface Gets files from remote hosts
017 */
018public class RemoteSCPGet  {
019        private static final int BUFSIZE = 1024;
020          
021        String host = "";
022        String user = "";
023        Integer port=0;
024        String userpass="";
025        String file = "";
026        String localdir = "";
027        String usercommand = "";
028        String output = "";
029        String characterSet="";
030        
031
032        public RemoteSCPGet(String host, String file, String localdir) {
033                this.host = host;
034                this.file = file;
035                this.localdir = localdir;
036        }
037        public RemoteSCPGet(String host, String user, String file, String localdir) {
038                this.host = host;
039                this.user = user;
040                this.file = file;
041                this.localdir = localdir;
042        }
043        public RemoteSCPGet(String host, String user, String userpass, String file, String localdir) {
044                this.host = host;
045                this.user = user;
046                this.userpass=userpass;
047                this.file = file;
048                this.localdir = localdir;
049        }
050        public RemoteSCPGet(String host,  String file, String localdir,Integer port) {
051                this.host = host;
052                this.file = file;
053                this.localdir = localdir;
054                this.port=port;
055        }
056        public RemoteSCPGet(String host, String user, String file, String localdir,Integer port) {
057                this.host = host;
058                this.user = user;
059                this.file = file;
060                this.localdir = localdir;
061                this.port=port;
062        }
063
064        public RemoteSCPGet(String host, String user,  String userpass, String file, String localdir,Integer port) {
065                this.host = host;
066                this.user = user;
067                this.userpass=userpass;
068                this.file = file;
069                this.localdir = localdir;
070                this.port=port;
071        }
072
073        public RemoteSCPGet(String host, String user,  String userpass, String file, String localdir,Integer port,String charSet) {
074                this.host = host;
075                this.user = user;
076                this.userpass=userpass;
077                this.file = file;
078                this.localdir = localdir;
079                this.port=port;
080                this.characterSet=charSet;
081        }
082
083        public String Result(SshConfig ac) throws IOException{
084                Object sshuser=ac.getConfig("USER");
085                Object sshpass=ac.getConfig("PASS");
086                Object sshkey=ac.getConfig("KEY");
087                Object sshkeypass=ac.getConfig("KEYPASS");
088                Object sshport=ac.getConfig("PORT");
089                String charSet=ac.getConfig("CHARACTERSET").toString();
090                
091                //System.out.println("----"+sshuser.toString());
092                if (user.equals("")) {
093                        user = sshuser.toString();
094                }
095                if (userpass.equals("")) {
096                        userpass = sshpass.toString();
097                }
098                if (port==0) {
099                        String sps=sshport.toString();
100                        if (sps.matches("[0-9]+")) {
101                                port=Integer.parseInt(sps);
102                        }
103                }
104                if (this.characterSet.length()==0) {
105                        if (charSet.length()>0) {
106                                this.characterSet=charSet;
107                        } else {
108                                this.characterSet="UTF-8";
109                        }
110                }
111
112                String hostname = host;
113                String username = user;
114                File keyfile = new File(sshkey.toString());
115                String keyfilePass = sshkeypass.toString();
116                        if (port==0){port=22; }
117                        Connection conn = new Connection(hostname,port);
118                        /* Now connect */
119                        conn.connect();
120                        /* Authenticate */
121                        boolean isAuthenticated=false;
122                        if (userpass.equals("")) {
123                                isAuthenticated = conn.authenticateWithPublicKey(username,
124                                                keyfile, keyfilePass);
125                        }else{
126                                isAuthenticated = conn.authenticateWithPassword(username,userpass);
127                        }
128
129                        if (isAuthenticated == false)
130                                throw new IOException("Authentication failed.");
131
132                        String fileName=null;
133                        String localFile=null;
134                        if (file!=null) {
135                                fileName=(new File(file)).getName();
136                                if (fileName!=null) {
137                                        localFile = localdir + File.separator + fileName;
138                                }
139                        }
140                        if (localFile!=null) {
141                                SFTPv3Client client = new SFTPv3Client(conn);
142                                File actualFile = new File(localFile);
143                                SFTPv3FileHandle handle = client.openFileRO(file);
144                                BufferedOutputStream bfout = new BufferedOutputStream(new FileOutputStream(actualFile));
145                                byte[] buf = new byte[BUFSIZE];
146                                int count = 0;
147                                int bufsiz = 0;
148                                while ((bufsiz = client.read(handle, count, buf, 0, BUFSIZE)) != -1) {
149                                        bfout.write(buf, 0, bufsiz);
150                                        count += bufsiz;
151                                }
152                                bfout.close();
153                                client.closeFile(handle);
154                                if (handle.isClosed()){
155                                        client.close();
156                                }
157                                
158                                output = "File " + file + " should now be copied from " + host
159                                                + " to localdir: " + localdir + "<br>";
160                        }
161                        conn.close();
162                return output;
163        }
164}