11package root .common .server .implement ;
22
3+ import java .io .IOException ;
34import java .io .InputStream ;
45import java .util .Properties ;
56
7+ import org .apache .commons .io .IOUtils ;
8+
69import com .jcraft .jsch .Channel ;
710import com .jcraft .jsch .ChannelExec ;
811import com .jcraft .jsch .JSch ;
912import com .jcraft .jsch .JSchException ;
1013import com .jcraft .jsch .Session ;
1114
15+ import lombok .extern .slf4j .Slf4j ;
1216import root .core .domain .JschConnectionInfo ;
1317
18+ @ Slf4j
1419public class JschServer {
1520 private JSch jsch ;
1621 private Session session ;
@@ -23,77 +28,128 @@ public JschServer(JschConnectionInfo jschConnectionInfo) {
2328 public void init () {
2429 jsch = new JSch ();
2530 session = null ;
26-
31+
2732 try {
2833 session = jsch .getSession (jschConnectionInfo .getUserName (), jschConnectionInfo .getHost (),
2934 Integer .valueOf (jschConnectionInfo .getPort ()));
3035 session .setPassword (jschConnectionInfo .getPassword ());
31-
36+
3237 Properties config = new Properties ();
3338 config .put ("StrictHostKeyChecking" , "no" ); // 호스트 정보를 검사하지 않는다.
39+ config .put ("PreferredAuthentications" , "password" );
3440 session .setConfig (config );
35-
41+
3642 } catch (JSchException e ) {
37- System .out .println ("JSch Session Creation Faild!" );
38- e .printStackTrace ();
43+ log .error (e .getMessage ());
3944 }
4045 }
41-
46+
4247 public Session getSession () {
43- if (session == null ) {
48+ if (session == null ) {
4449 return null ;
4550 }
4651 return session ;
4752 }
48-
49- public Session connect (Session session ) {
53+
54+ public Session connect (Session session ) throws JSchException {
55+ if (session == null ) {
56+ throw new NullPointerException ("Session is null" );
57+ }
58+
59+ if (session .isConnected ()) {
60+ return session ;
61+ }
62+
5063 try {
5164 session .connect ();
5265 } catch (JSchException e ) {
53- System . out . println ( "JSch Connection Faild!" );
54- e . printStackTrace () ;
66+ log . error ( e . getMessage () );
67+ throw e ;
5568 }
69+
5670 return session ;
5771 }
58-
72+
5973 public void disConnect (Session session ) {
74+ if (session == null ) {
75+ throw new NullPointerException ("Session is null" );
76+ }
77+
6078 session .disconnect ();
6179 }
62-
80+
6381 public Channel openExecChannel (Session session , String command ) {
82+ if (session == null ) {
83+ init ();
84+ try {
85+ session = this .connect (this .getSession ());
86+ } catch (JSchException e ) {
87+ log .error (e .getMessage ());
88+ }
89+ }
90+
6491 Channel channel = null ;
6592 try {
6693 channel = session .openChannel ("exec" );
67- //채널접속
68- ChannelExec channelExec = (ChannelExec ) channel ; //명령 전송 채널사용
69- channelExec .setPty (true );
70- channelExec .setCommand (command );
94+ // 채널접속
95+ ChannelExec channelExec = (ChannelExec ) channel ; // 명령 전송 채널사용
96+ // channelExec.setPty(true);
97+ channelExec .setCommand (command );
7198 } catch (JSchException e ) {
72- System .out .println ("Channel Open Faild!" );
73- // e.printStackTrace();
99+ log .error (e .getMessage ());
74100 }
75101 return channel ;
76102 }
77103
104+ private Channel openExecChannel (String command ) {
105+ return openExecChannel (session , command );
106+ }
107+
78108 public InputStream connectChannel (Channel channel ) {
79109 InputStream in = null ;
80110 try {
81111 // CallBack
82112 in = channel .getInputStream ();
83- ((ChannelExec ) channel ).setErrStream (System .err );
84-
113+ ((ChannelExec ) channel ).setErrStream (System .err );
114+
85115 channel .connect ();
86116 } catch (Exception e ) {
87- System . out . println ( "Channel Connect Failed!" );
117+ log . error ( e . getMessage () );
88118 }
89119 return in ;
90120 }
91-
121+
92122 public void disConnectChannel (Channel channel ) {
93- channel .disconnect ();
123+ channel .disconnect ();
94124 }
95-
125+
96126 public String getServerName () {
97127 return this .jschConnectionInfo .getServerName ();
98128 }
129+
130+ public String executeCommand (String command ) throws JSchException , IOException {
131+ log .debug (command );
132+ Channel channel = openExecChannel (command );
133+ InputStream in = connectChannel (channel );
134+ String result = IOUtils .toString (in , "UTF-8" );
135+ disConnectChannel (channel );
136+ disConnect (session );
137+ return result .trim ();
138+ }
139+
140+ public static boolean validateConn (Session session ) {
141+ if (session == null ) {
142+ log .error ("JSch session is null" );
143+ return false ;
144+ }
145+
146+ try {
147+ session .connect (3000 );
148+ } catch (JSchException e ) {
149+ log .error (e .getMessage ());
150+ return false ;
151+ }
152+
153+ return session .isConnected ();
154+ }
99155}
0 commit comments