Compare commits

..

1 Commits

Author SHA1 Message Date
jshixiong da0a794c48 ssh导入环境变量 2023-10-16 18:06:39 +08:00
1 changed files with 16 additions and 4 deletions

View File

@ -10,6 +10,7 @@ import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import com.jcraft.jsch.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
@ -18,10 +19,6 @@ import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;
import com.alibaba.fastjson.JSONObject;
import com.jcraft.jsch.ChannelShell;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;
import com.jcraft.jsch.UserInfo;
import net.educoder.bridge.webssh.model.ConnectInfo;
import net.educoder.bridge.webssh.model.WebscoketObj;
@ -174,6 +171,10 @@ public class JchService {
session.setServerAliveInterval(30 * 1000); // 30秒心跳一次用于保活
session.connect();
//本次ssh导入环境变量
executeCommend(session, "export $(cat /proc/1/environ |tr '\0' '\n' |grep -v ' ' | xargs)");
ChannelShell channel = (ChannelShell) session.openChannel("shell");
channel.setPtyType("xterm");
@ -310,4 +311,15 @@ public class JchService {
}
return null;
}
private void executeCommend(Session session, String cmd){
try {
ChannelExec channelExec = (ChannelExec) session.openChannel("exec");
channelExec.setCommand(cmd);
channelExec.connect();
// 关闭ChannelExec
channelExec.disconnect();
}catch (Exception ignore){}
}
}