001package ssh;
002
003import grails.plugin.remotessh.SshConfig;
004
005import java.io.File;
006import java.io.FileOutputStream;
007import java.io.IOException;
008
009import ch.ethz.ssh2.Connection;
010import ch.ethz.ssh2.SCPClient;
011import ch.ethz.ssh2.SFTPv3Client;
012import ch.ethz.ssh2.SFTPv3FileHandle;
013
014/**
015 * Copies a file to remote server.
016 */
017public class RemoteSCP {
018
019        String host = "";
020        String user = "";
021        Integer port=0;
022        String userpass="";
023        String file = "";
024        String remotedir = "";
025        String usercommand = "";
026        String output = "";
027        String characterSet="";
028        String permission = "";
029        
030        public RemoteSCP(String host, String file, String remotedir) {
031                this.host = host;
032                this.file = file;
033                this.remotedir = remotedir;
034        }
035        public RemoteSCP(String host, String user, String file, String remotedir) {
036                this.host = host;
037                this.user = user;
038                this.file = file;
039                this.remotedir = remotedir;
040        }
041        public RemoteSCP(String host, String user, String userpass, String file, String remotedir) {
042                this.host = host;
043                this.user = user;
044                this.userpass=userpass;
045                this.file = file;
046                this.remotedir = remotedir;
047        }
048        public RemoteSCP(String host, String file, String remotedir, Integer port) {
049                this.host = host;
050                this.file = file;
051                this.remotedir = remotedir;
052                this.port=port;
053        }
054        public RemoteSCP(String host, String user, String file, String remotedir, Integer port) {
055                this.host = host;
056                this.user = user;
057                this.file = file;
058                this.remotedir = remotedir;
059                this.port=port;
060        }
061        
062        public RemoteSCP(String host, String user, String userpass,  String file, String remotedir, Integer port) {
063                this.host = host;
064                this.user = user;
065                this.userpass=userpass;
066                this.file = file;
067                this.remotedir = remotedir;
068                this.port=port;
069        }
070        
071        public RemoteSCP(String host, String user, String userpass,  String file, String remotedir, Integer port,String charSet,String permission) {
072                this.host = host;
073                this.user = user;
074                this.userpass=userpass;
075                this.file = file;
076                this.remotedir = remotedir;
077                this.port=port;
078                this.characterSet=charSet;
079                this.permission=permission;
080        }
081
082        public RemoteSCP(String host, String user, String userpass,  String file, String remotedir, Integer port,String charSet) {
083                this.host = host;
084                this.user = user;
085                this.userpass=userpass;
086                this.file = file;
087                this.remotedir = remotedir;
088                this.port=port;
089                this.characterSet=charSet;
090        }
091        
092        
093        public String Result(SshConfig ac) throws IOException{
094                Object sshuser=ac.getConfig("USER");
095                Object sshpass=ac.getConfig("PASS");
096                Object sshkey=ac.getConfig("KEY");
097                Object sshkeypass=ac.getConfig("KEYPASS");
098                Object sshport=ac.getConfig("PORT");
099                String charSet=ac.getConfig("CHARACTERSET").toString();
100                String perm=ac.getConfig("PERMISSION").toString();
101                //System.out.println("----"+sshuser.toString());
102                if (user.equals("")) {
103                        user = sshuser.toString();
104                }
105                if (userpass.equals("")) {
106                        userpass = sshpass.toString();
107                }
108                if (port==0) {
109                        String sps=sshport.toString();
110                        if (sps.matches("[0-9]+")) {
111                                port=Integer.parseInt(sps);
112                        }
113                }
114                
115                if (this.characterSet.length()==0) {
116                        if (charSet.length()>0) {
117                                this.characterSet=charSet;
118                        } else {
119                                this.characterSet="UTF-8";
120                        }
121                }
122                if (this.permission.length()==0) {
123                        if (perm.length()>0) {
124                                this.permission=perm;
125                        } else {
126                                this.permission="0600";
127                        }
128                }
129                //String characterSet = (charsetName ?: (charSet ? charSet.toString() : null))
130                        //      String permission =(mode ?: (perm ?perm.toString() :"0600"))
131                                
132                String hostname = host;
133                String username = user;
134                File keyfile = new File(sshkey.toString());
135                String keyfilePass = sshkeypass.toString();
136                if (port==0){port=22; }
137                        Connection conn = new Connection(hostname,port);
138                        /* Now connect */
139                        conn.connect();
140                        /* Authenticate */
141                        boolean isAuthenticated=false;
142                        if (userpass.equals("")) {
143                                isAuthenticated = conn.authenticateWithPublicKey(username,
144                                                keyfile, keyfilePass);
145                        }else{
146                                isAuthenticated = conn.authenticateWithPassword(username,userpass);
147                        }
148                        if (isAuthenticated == false)
149                                throw new IOException("Authentication failed.");
150                        File actualFile = new File(file);
151                        FileOutputStream out =  null;
152                        try {
153                                out = new FileOutputStream(remotedir+File.separator+actualFile.getName());
154                                SFTPv3Client sFTPv3Client = new SFTPv3Client(conn);
155                                if (this.characterSet!=null) {
156                                        sFTPv3Client.setCharset(characterSet);
157                                }
158                                SFTPv3FileHandle handle = sFTPv3Client.openFileRO(file);
159                                byte[] cache = new byte[1024];
160                                int i = 0;
161                                int offset = 0;
162                                while((i = sFTPv3Client.read(handle, offset, cache, 0, 1024)) != -1){
163                                        out.write(cache, 0, i);
164                                        offset += i;
165                                }
166                                sFTPv3Client.closeFile(handle);
167                        if (handle.isClosed()){
168                                    sFTPv3Client.close();
169                        } 
170                        } catch (IOException e) {
171                                e.printStackTrace();
172                        } finally {
173                                try {
174                                        if (out != null) {
175                                                out.close();
176                                        }
177                                } catch (IOException e) {
178                                        e.printStackTrace();
179                                }
180                        }
181                        conn.close();
182
183                        output = "File " + file + " should now be copied to " + host + ":"
184                                        + remotedir + "<br>";
185
186                return output;
187        }
188}