root / src / gZFS / libConnect.java @ 28
Historique | Voir | Annoter | Télécharger (3,09 ko)
1 |
package gZFS; |
---|---|
2 |
|
3 |
import java.io.BufferedReader; |
4 |
import java.io.IOException; |
5 |
import java.io.InputStream; |
6 |
import java.io.InputStreamReader; |
7 |
import java.io.OutputStream; |
8 |
import java.io.PipedInputStream; |
9 |
import java.io.PipedOutputStream; |
10 |
import java.io.PrintStream; |
11 |
import java.util.ArrayList; |
12 |
import java.util.Properties; |
13 |
|
14 |
import com.jcraft.jsch.Channel; |
15 |
import com.jcraft.jsch.ChannelExec; |
16 |
import com.jcraft.jsch.JSch; |
17 |
import com.jcraft.jsch.JSchException; |
18 |
import com.jcraft.jsch.Session; |
19 |
|
20 |
public class libConnect { |
21 |
|
22 |
private String userLogin; |
23 |
private String userPassword; |
24 |
private String sshHost; |
25 |
private int sshPort; |
26 |
private Session session;
|
27 |
private JSch jsch = new JSch(); |
28 |
|
29 |
public String connect(String cmd) { |
30 |
String outCmd = null ; |
31 |
try {
|
32 |
|
33 |
setSession(jsch.getSession(getUserLogin(), getSshHost(), getSshPort())); |
34 |
session.setPassword(this.getUserPassword());
|
35 |
Properties config = new Properties(); |
36 |
config.put("StrictHostKeyChecking", "no"); |
37 |
session.setConfig(config); |
38 |
session.connect(); |
39 |
if ( session.isConnected()){
|
40 |
System.out.println("Connected"); |
41 |
}else{
|
42 |
System.out.println("not Connected"); |
43 |
} |
44 |
ChannelExec channel = (ChannelExec) session.openChannel("exec");
|
45 |
//((ChannelExec) channel).setCommand("ls");
|
46 |
((ChannelExec) channel).setCommand(cmd); |
47 |
InputStream in = channel.getInputStream();
|
48 |
OutputStream out = channel.getOutputStream();
|
49 |
((ChannelExec) channel).setErrStream(System.err);
|
50 |
|
51 |
channel.connect(); |
52 |
byte[] tmp = new byte[1024]; |
53 |
|
54 |
while (true) { |
55 |
|
56 |
int ind = 0; |
57 |
while (in.available() > 0) { |
58 |
int i = in.read(tmp, 0, 1024); |
59 |
if (i < 0) |
60 |
break;
|
61 |
|
62 |
outCmd= new String(tmp, 0, i); |
63 |
//System.out.println(toto);
|
64 |
System.out.println(outCmd);
|
65 |
//System.out.print(new String(tmp, 0, i));
|
66 |
|
67 |
} |
68 |
if (channel.isClosed()) {
|
69 |
break;
|
70 |
} |
71 |
try {
|
72 |
Thread.sleep(1000); |
73 |
} catch (Exception ee) { |
74 |
System.out.println(ee);
|
75 |
} |
76 |
} |
77 |
channel.disconnect(); |
78 |
|
79 |
} catch (JSchException e) {
|
80 |
// TODO Auto-generated catch block
|
81 |
e.printStackTrace(); |
82 |
return null; |
83 |
} catch (IOException e) { |
84 |
// TODO Auto-generated catch block
|
85 |
e.printStackTrace(); |
86 |
return null; |
87 |
} |
88 |
/*for (int cpt=0;cpt < outCmd.length;cpt++ ) {
|
89 |
System.out.println(outCmd[cpt]);
|
90 |
}*/
|
91 |
System.out.println(outCmd);
|
92 |
return outCmd;
|
93 |
} |
94 |
|
95 |
|
96 |
|
97 |
public String zfsGetAllVolume(String volume){ |
98 |
return connect("zfs list"); |
99 |
|
100 |
// return this.executeCmd("/sbin/zfs get all "+volume);
|
101 |
} |
102 |
public String getSshHost() { |
103 |
return sshHost;
|
104 |
} |
105 |
|
106 |
public void setSshHost(String sshHost) { |
107 |
this.sshHost = sshHost;
|
108 |
} |
109 |
|
110 |
public String getUserLogin() { |
111 |
return userLogin;
|
112 |
} |
113 |
|
114 |
public void setUserLogin(String userLogin) { |
115 |
this.userLogin = userLogin;
|
116 |
} |
117 |
|
118 |
|
119 |
public int getSshPort() { |
120 |
return sshPort;
|
121 |
} |
122 |
|
123 |
|
124 |
public void setSshPort(int sshPort) { |
125 |
this.sshPort = sshPort;
|
126 |
} |
127 |
|
128 |
public Session getSession() {
|
129 |
return session;
|
130 |
} |
131 |
|
132 |
public void setSession(Session session) { |
133 |
this.session = session;
|
134 |
} |
135 |
|
136 |
public String getUserPassword() { |
137 |
return userPassword;
|
138 |
} |
139 |
|
140 |
public void setUserPassword(String userPassword) { |
141 |
this.userPassword = userPassword;
|
142 |
} |
143 |
|
144 |
} |