|
a |
|
b/src/main/java/net/timbusproject/extractors/modules/tavernaextractor/utils/SSHManager.java |
|
|
1 |
package net.timbusproject.extractors.modules.tavernaextractor.utils;
|
|
|
2 |
|
|
|
3 |
import com.jcraft.jsch.*;
|
|
|
4 |
|
|
|
5 |
import java.io.File;
|
|
|
6 |
import java.io.IOException;
|
|
|
7 |
import java.io.InputStream;
|
|
|
8 |
import java.lang.Exception;import java.lang.String;import java.nio.file.Path;
|
|
|
9 |
import java.nio.file.Paths;
|
|
|
10 |
|
|
|
11 |
/**
|
|
|
12 |
* Created by marco unterberger on 28.08.2014.
|
|
|
13 |
*
|
|
|
14 |
* Copy input files from a remote location to use them for the extraction.
|
|
|
15 |
*
|
|
|
16 |
* * munterberger@sba-research.org
|
|
|
17 |
*/
|
|
|
18 |
public class SSHManager {
|
|
|
19 |
|
|
|
20 |
private final String username;
|
|
|
21 |
private final String password;
|
|
|
22 |
private final String fqdn;
|
|
|
23 |
private final int port;
|
|
|
24 |
|
|
|
25 |
public SSHManager(String fqdn, int port, String username, String password){
|
|
|
26 |
this.username = username;
|
|
|
27 |
this.password = password;
|
|
|
28 |
this.fqdn = fqdn;
|
|
|
29 |
this.port = port;
|
|
|
30 |
}
|
|
|
31 |
|
|
|
32 |
public Session createSession(int timeout) throws SSHManagerException{
|
|
|
33 |
|
|
|
34 |
JSch jsch = new JSch();
|
|
|
35 |
|
|
|
36 |
Session session = null;
|
|
|
37 |
try {
|
|
|
38 |
session = jsch.getSession(username, fqdn, port);
|
|
|
39 |
session.setPassword(password);
|
|
|
40 |
session.setConfig("StrictHostKeyChecking", "no");
|
|
|
41 |
session.connect(timeout);
|
|
|
42 |
|
|
|
43 |
} catch (JSchException e) {
|
|
|
44 |
e.printStackTrace();
|
|
|
45 |
}
|
|
|
46 |
return session;
|
|
|
47 |
}
|
|
|
48 |
|
|
|
49 |
public File readFile(Session session, String path) throws SSHManagerException{
|
|
|
50 |
|
|
|
51 |
if(!session.isConnected()){
|
|
|
52 |
try{
|
|
|
53 |
session.connect();
|
|
|
54 |
}
|
|
|
55 |
catch (JSchException e){
|
|
|
56 |
e.printStackTrace();
|
|
|
57 |
throw new SSHManagerException(e.getLocalizedMessage());
|
|
|
58 |
}
|
|
|
59 |
}
|
|
|
60 |
|
|
|
61 |
Channel channel = null;
|
|
|
62 |
try {
|
|
|
63 |
channel = session.openChannel("sftp");
|
|
|
64 |
channel.connect();
|
|
|
65 |
} catch (JSchException e) {
|
|
|
66 |
e.printStackTrace();
|
|
|
67 |
throw new SSHManagerException(e.getLocalizedMessage());
|
|
|
68 |
}
|
|
|
69 |
|
|
|
70 |
ChannelSftp sftpChannel = (ChannelSftp) channel;
|
|
|
71 |
|
|
|
72 |
File file = null;
|
|
|
73 |
try {
|
|
|
74 |
file = File.createTempFile("tavernaExtractor-", ".input");
|
|
|
75 |
//file.deleteOnExit();
|
|
|
76 |
} catch (IOException e) {
|
|
|
77 |
e.printStackTrace();
|
|
|
78 |
throw new SSHManagerException(e.getLocalizedMessage());
|
|
|
79 |
}
|
|
|
80 |
|
|
|
81 |
try {
|
|
|
82 |
sftpChannel.get(path, file.getAbsolutePath());
|
|
|
83 |
} catch (SftpException e) {
|
|
|
84 |
e.printStackTrace();
|
|
|
85 |
throw new SSHManagerException(e.getLocalizedMessage());
|
|
|
86 |
}
|
|
|
87 |
|
|
|
88 |
session.disconnect();
|
|
|
89 |
return file;
|
|
|
90 |
}
|
|
|
91 |
|
|
|
92 |
public void sendFile(Session session, String from, String to) throws SSHManagerException{
|
|
|
93 |
|
|
|
94 |
if(!session.isConnected()){
|
|
|
95 |
try{
|
|
|
96 |
session.connect();
|
|
|
97 |
}
|
|
|
98 |
catch (JSchException e){
|
|
|
99 |
e.printStackTrace();
|
|
|
100 |
throw new SSHManagerException(e.getLocalizedMessage());
|
|
|
101 |
}
|
|
|
102 |
}
|
|
|
103 |
|
|
|
104 |
Channel channel = null;
|
|
|
105 |
try {
|
|
|
106 |
channel = session.openChannel("sftp");
|
|
|
107 |
channel.connect();
|
|
|
108 |
} catch (JSchException e) {
|
|
|
109 |
e.printStackTrace();
|
|
|
110 |
throw new SSHManagerException(e.getLocalizedMessage());
|
|
|
111 |
}
|
|
|
112 |
|
|
|
113 |
ChannelSftp sftpChannel = (ChannelSftp) channel;
|
|
|
114 |
|
|
|
115 |
try {
|
|
|
116 |
sftpChannel.put(from, to);
|
|
|
117 |
} catch (SftpException e) {
|
|
|
118 |
e.printStackTrace();
|
|
|
119 |
throw new SSHManagerException(e.getLocalizedMessage());
|
|
|
120 |
}
|
|
|
121 |
|
|
|
122 |
session.disconnect();
|
|
|
123 |
}
|
|
|
124 |
|
|
|
125 |
public static void main(String[] arg) throws Exception{
|
|
|
126 |
|
|
|
127 |
SSHManager ssh = new SSHManager("172.16.1.3",22, "timbus", "timbus");
|
|
|
128 |
Session session = ssh.createSession(60000);
|
|
|
129 |
|
|
|
130 |
// downloading file
|
|
|
131 |
File file = ssh.readFile(session, "/home/timbus/WorkflowInput/+Z150709801_ground_truth.csv");
|
|
|
132 |
if(file!=null){
|
|
|
133 |
System.out.println("File successfully saved at "+file.getAbsolutePath() + " [size="+file.length()+"]");
|
|
|
134 |
}
|
|
|
135 |
|
|
|
136 |
Thread.sleep(3000);
|
|
|
137 |
|
|
|
138 |
// receiving file
|
|
|
139 |
ssh.sendFile(session, new File("C:\\Users\\munterberger\\AppData\\Local\\Temp\\tavernaExtractor-7820989094280082735.input").getAbsolutePath(), "/home/timbus/WorkflowInput/tmp.csv");
|
|
|
140 |
}
|
|
|
141 |
|
|
|
142 |
}
|