Skip to content

Commit a0fc908

Browse files
committed
Add JschServer connection test method
1 parent 7034e22 commit a0fc908

File tree

1 file changed

+26
-10
lines changed

1 file changed

+26
-10
lines changed

src/main/java/root/common/server/implement/JschServer.java

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@
99
import com.jcraft.jsch.JSchException;
1010
import com.jcraft.jsch.Session;
1111

12+
import lombok.extern.slf4j.Slf4j;
1213
import root.core.domain.JschConnectionInfo;
1314

15+
@Slf4j
1416
public class JschServer {
1517
private JSch jsch;
1618
private Session session;
@@ -23,19 +25,19 @@ public JschServer(JschConnectionInfo jschConnectionInfo) {
2325
public void init() {
2426
jsch = new JSch();
2527
session = null;
26-
28+
2729
try {
2830
session = jsch.getSession(jschConnectionInfo.getUserName(), jschConnectionInfo.getHost(),
2931
Integer.valueOf(jschConnectionInfo.getPort()));
3032
session.setPassword(jschConnectionInfo.getPassword());
31-
33+
3234
Properties config = new Properties();
3335
config.put("StrictHostKeyChecking", "no"); // 호스트 정보를 검사하지 않는다.
36+
config.put("PreferredAuthentications", "password");
3437
session.setConfig(config);
35-
38+
3639
} catch (JSchException e) {
37-
System.out.println("JSch Session Creation Faild!");
38-
e.printStackTrace();
40+
log.error(e.getMessage());
3941
}
4042
}
4143

@@ -50,8 +52,7 @@ public Session connect(Session session) {
5052
try {
5153
session.connect();
5254
} catch (JSchException e) {
53-
System.out.println("JSch Connection Faild!");
54-
e.printStackTrace();
55+
log.error(e.getMessage());
5556
}
5657
return session;
5758
}
@@ -69,8 +70,7 @@ public Channel openExecChannel(Session session, String command) {
6970
channelExec.setPty(true);
7071
channelExec.setCommand(command);
7172
} catch (JSchException e) {
72-
System.out.println("Channel Open Faild!");
73-
// e.printStackTrace();
73+
log.error(e.getMessage());
7474
}
7575
return channel;
7676
}
@@ -84,7 +84,7 @@ public InputStream connectChannel(Channel channel) {
8484

8585
channel.connect();
8686
} catch (Exception e) {
87-
System.out.println("Channel Connect Failed!");
87+
log.error(e.getMessage());
8888
}
8989
return in;
9090
}
@@ -96,4 +96,20 @@ public void disConnectChannel(Channel channel) {
9696
public String getServerName() {
9797
return this.jschConnectionInfo.getServerName();
9898
}
99+
100+
public static boolean validateConn(Session session) {
101+
if (session == null) {
102+
log.error("JSch session is null");
103+
return false;
104+
}
105+
106+
try {
107+
session.connect(15);
108+
} catch (JSchException e) {
109+
log.error(e.getMessage());
110+
return false;
111+
}
112+
113+
return session.isConnected();
114+
}
99115
}

0 commit comments

Comments
 (0)