org.yes.tools.build.utils.EXECChannel Maven / Gradle / Ivy
package org.yes.tools.build.utils;
import com.jcraft.jsch.ChannelExec;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;
import org.apache.maven.plugin.logging.Log;
import org.yes.tools.build.bo.DeployBo;
/**
* @author Co.
* @name EXECChannel
* @date 2023/9/14 11:33
*/
public class EXECChannel {
Session jschSession = null;
ChannelExec channelExec = null;
//登录sftp
public ChannelExec getChannel(DeployBo.Remote remote, Log log) {
try {
JSch jsch = new JSch();
jschSession = jsch.getSession(remote.getUsername(), remote.getRemoteHost(), remote.getRemotePort());
jschSession.setConfig("StrictHostKeyChecking", "no");
// 密码认证
jschSession.setPassword(remote.getPassword());
// 建立session
jschSession.connect(10000);
//建立可执行管道
channelExec = (ChannelExec) jschSession.openChannel("exec");
log.debug("Connected successfully to execHost = " + remote.getRemoteHost() + ",as execUserName = " + remote.getUsername()
+ ", returning: " + channelExec);
return channelExec;
} catch (Exception e) {
throw new RuntimeException("exec连接错误,host=" + remote.getRemoteHost() + ",userName=" + remote.getUsername() + ",port=" + remote.getRemoteHost() + ",pwd=" + remote.getPassword());
}
}
//退出EXEC
public void closeChannel() throws Exception {
if (channelExec != null) {
channelExec.disconnect();
}
if (jschSession != null) {
jschSession.disconnect();
}
}
}