DevOps自动化平台开发之 Shell脚本执行的封装
基础知识
基于如下技术栈开发DevOps平台
Spring Boot
Shell
Ansible
Git
Gitlab
Docker
K8S
Vue
1、spring boot starter的封装使用
2、Shell脚本的编写
3、Ansible 脚本的编写
4、Docker 的使用与封装设计
本篇介绍如何使用Java封装Linux命令和Shell脚本的使用
将其设计成spring boot starter
maven依赖pom文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.7.9</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>com.devops</groupId><artifactId>ssh-client-pool-spring-boot-starter</artifactId><version>0.0.1-SNAPSHOT</version><name>ssh-client-pool-spring-boot-starter</name><description>Demo project for Spring Boot</description><properties><java.version>1.8</java.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-autoconfigure</artifactId></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><optional>true</optional></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><dependency><groupId>com.hierynomus</groupId><artifactId>sshj</artifactId><version>0.26.0</version></dependency><dependency><groupId>org.bouncycastle</groupId><artifactId>bcprov-jdk15on</artifactId><version>1.60</version></dependency><dependency><groupId>com.fasterxml.uuid</groupId><artifactId>java-uuid-generator</artifactId><version>3.1.4</version></dependency><dependency><groupId>net.sf.expectit</groupId><artifactId>expectit-core</artifactId><version>0.9.0</version></dependency><dependency><groupId>org.apache.commons</groupId><artifactId>commons-pool2</artifactId><version>2.8.0</version></dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId><scope>test</scope></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><configuration><excludes><exclude><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></exclude></excludes></configuration></plugin></plugins></build></project>
具体的封装代码:
package com.devops.ssh.autoconfigure;import com.devops.ssh.pool.SshClientPoolConfig;
import com.devops.ssh.pool.SshClientsPool;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;/*** @author Gary*/
@Configuration
@EnableConfigurationProperties(SshClientPoolProperties.class)
public class SshClientPoolAutoConfiguration {private final SshClientPoolProperties properties;public SshClientPoolAutoConfiguration(SshClientPoolProperties properties) {this.properties = properties;}@Bean@ConditionalOnMissingBean(SshClientsPool.class)SshClientsPool sshClientsPool() {return new SshClientsPool(sshClientPoolConfig());}SshClientPoolConfig sshClientPoolConfig() {SshClientPoolConfig poolConfig = new SshClientPoolConfig(properties.getMaxActive(),properties.getMaxIdle(),properties.getIdleTime(),properties.getMaxWait());if(properties.getSshj()!=null) {poolConfig.setServerCommandPromotRegex(properties.getSshj().getServerCommandPromotRegex());}if (properties.getSshClientImplClass()!=null) {try {poolConfig.setSshClientImplClass(Class.forName(properties.getSshClientImplClass()));} catch (ClassNotFoundException e) {e.printStackTrace();}}return poolConfig;}
}
package com.devops.ssh.autoconfigure;import org.springframework.boot.context.properties.ConfigurationProperties;@ConfigurationProperties("devops.ssh-client-pool")
public class SshClientPoolProperties {/*** Max number of "idle" connections in the pool. Use a negative value to indicate* an unlimited number of idle connections.*/private int maxIdle = 20;/*** */private int idleTime = 120*1000;/*** Max number of connections that can be allocated by the pool at a given time.* Use a negative value for no limit.*/private int maxActive = 20;/*** Maximum amount of time (in milliseconds) a connection allocation should block* before throwing an exception when the pool is exhausted. Use a negative value* to block indefinitely.*/private int maxWait = 120*1000;private String sshClientImplClass = "com.devops.ssh.SshClientSSHJ";private SshClientProperites sshj;public int getMaxIdle() {return maxIdle;}public void setMaxIdle(int maxIdle) {this.maxIdle = maxIdle;}public int getIdleTime() {return idleTime;}public void setIdleTime(int idleTime) {this.idleTime = idleTime;}public int getMaxActive() {return maxActive;}public void setMaxActive(int maxActive) {this.maxActive = maxActive;}public int getMaxWait() {return maxWait;}public void setMaxWait(int maxWait) {this.maxWait = maxWait;}public String getSshClientImplClass() {return sshClientImplClass;}public void setSshClientImplClass(String sshClientImplClass) {this.sshClientImplClass = sshClientImplClass;}public SshClientProperites getSshj() {return sshj;}public void setSshj(SshClientProperites sshj) {this.sshj = sshj;}public static class SshClientProperites{private String serverCommandPromotRegex;public String getServerCommandPromotRegex() {return serverCommandPromotRegex;}public void setServerCommandPromotRegex(String serverCommandPromotRegex) {this.serverCommandPromotRegex = serverCommandPromotRegex;}}}
package com.devops.ssh.exception;/*** Ssh auth failed* @author Gary**/
public class AuthException extends SshException{public AuthException(String message) {this(message, null);}public AuthException(String message, Throwable error) {super(message, error);}/*** */private static final long serialVersionUID = -3961786667342327L;}
package com.devops.ssh.exception;/*** The ssh connection is disconnected* @author Gary**/
public class LostConnectionException extends SshException{private static final long serialVersionUID = -3961870786667342727L;public LostConnectionException(String message) {this(message, null);}public LostConnectionException(String message, Throwable error) {super(message, error);}
}
package com.devops.ssh.exception;public class SshException extends Exception{/*** */private static final long serialVersionUID = 2052615275027564490L;public SshException(String message, Throwable error) {super(message);if(error != null) {initCause(error);}}public SshException(String message) {this(message, null);}}
package com.devops.ssh.exception;/*** Timeout Exception* @author Gary**/
public class TimeoutException extends SshException {public TimeoutException(String message) {this(message, null);}public TimeoutException(String message, Throwable error) {super(message, error);}/****/private static final long serialVersionUID = -39618386667342727L;}
package com.devops.ssh.pool;import com.devops.ssh.SshClient;
import com.devops.ssh.SshClientSSHJ;
import org.apache.commons.pool2.impl.GenericKeyedObjectPoolConfig;/**** The configuration of SshClientPool library* <p>SshClientPoolConfig is a subclass of GenericKeyedObjectPoolConfig to control the pool behavior* <p>Also, you can replace the build-in {@link SshClient} implementation by {@link SshClientPoolConfig#setSshClientImplClass(Class)} if you want** @author Gary*/
public class SshClientPoolConfig extends GenericKeyedObjectPoolConfig<SshClientWrapper>{private Class<?> sshClientImplClass;private String serverCommandPromotRegex;public SshClientPoolConfig() {super();}/*** quick way to create SshClientPoolConfig* set TestOnBorrow to true* set TestOnReturn to true* set TestWhileIdle to true* set JmxEnabled to false* @param maxActive maxTotalPerKey* @param maxIdle maxIdlePerKey* @param idleTime idle time* @param maxWaitTime maxWaitMillis*/public SshClientPoolConfig(int maxActive, int maxIdle, long idleTime, long maxWaitTime){this.setMaxTotalPerKey(maxActive);this.setMaxIdlePerKey(maxIdle);this.setMaxWaitMillis(maxWaitTime);this.setBlockWhenExhausted(true);this.setMinEvictableIdleTimeMillis(idleTime);this.setTimeBetweenEvictionRunsMillis(idleTime);this.setTestOnBorrow(true);this.setTestOnReturn(true);this.setTestWhileIdle(true);this.setJmxEnabled(false);}public Class<?> getSshClientImplClass() {return sshClientImplClass;}/*** replace the build-in {@link SshClient} by {@link SshClientPoolConfig#setSshClientImplClass(Class)}* @param sshClientImplClass the implementation of {@link SshClient}*/public void setSshClientImplClass(Class<?> sshClientImplClass) {this.sshClientImplClass = sshClientImplClass;}/**** @return regex string used to match promot from server*/public String getServerCommandPromotRegex() {return serverCommandPromotRegex;}/*** see {@link SshClientSSHJ#setCommandPromotRegexStr(String)}* @param serverCommandPromotRegex regex string used to match promot from server*/public void setServerCommandPromotRegex(String serverCommandPromotRegex) {this.serverCommandPromotRegex = serverCommandPromotRegex;}}
package com.devops.ssh.pool;import com.devops.ssh.*;
import com.devops.ssh.exception.SshException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;import java.util.UUID;/*** A wrapper class of {@link SshClient} used by {@link SshClientsPool}** @author Gary**/
public class SshClientWrapper implements SshClientEventListener {private final static Logger logger = LoggerFactory.getLogger(SshClientWrapper.class);private String id;private SshClient client;SshClientEventListener listener;SshClientConfig config;public String getId() {return this.id;}public void setListener(SshClientEventListener listener) {this.listener = listener;}public SshClientConfig getConfig() {return this.config;}public SshClientWrapper(SshClientConfig config, SshClientPoolConfig poolConfig) {this.id = UUID.randomUUID().toString();this.config = config;this.client = SshClientFactory.newInstance(config, poolConfig);}public SshClientWrapper setEventListener(SshClientEventListener listener) {this.listener = listener;this.client.setEventListener(this);return this;}public SshClientWrapper connect(int timeoutInSeconds) throws SshException {client.connect(timeoutInSeconds);return this;}public SshClientWrapper auth() throws SshException{if(null!=this.config.getPassword() && this.config.getPassword().length()>0) {client.authPassword();}else if(null!=this.config.getPrivateKeyPath() && this.config.getPrivateKeyPath().length()>0) {client.authPublickey();}else {client.authPublickey();}return this;}public SshClientWrapper startSession() throws SshException{client.startSession(true);return this;}public SshResponse executeCommand(String command, int timeoutInSeconds){SshResponse response = client.executeCommand(command, timeoutInSeconds);return response;}public void disconnect() {client.disconnect();}@Overridepublic boolean equals(Object obj) {if(obj instanceof SshClientWrapper){return id.equals(((SshClientWrapper)obj).getId());}return false;}@Overridepublic int hashCode(){return id.hashCode();}public SshClientState getState() {return client.getState();}@Overridepublic String toString() {return "["+this.id+"|"+this.config.getHost()+"|"+this.config.getPort()+"|"+this.getState()+"]";}@Overridepublic void didExecuteCommand(Object client) {this.listener.didExecuteCommand(this);}@Overridepublic void didDisConnected(Object client) {this.listener.didDisConnected(this);}@Overridepublic void didConnected(Object client) {this.listener.didConnected(this);}}
package com.devops.ssh;import com.devops.ssh.exception.SshException;/*** Ssh Client used to connect to server instance and execute command. The build-in implementation is {@link SshClientSSHJ}<p>** Client can be used in chain mode, {@link SshClient}.{@link #init(SshClientConfig)}.{@link #connect(int)}.{@link #authPassword()}.{@link #startSession(boolean)}.{@link #executeCommand(String, int)}<p>** At last, close the client with {@link #disconnect()}** <p>Set an {@link SshClientEventListener} with {@link #setEventListener(SshClientEventListener)} to be notified when its event occurs* <p>* @author Gary**/
public interface SshClient {/*** pass the {@link SshClientConfig} to client* @param config the information used to connect to server* @return SshClient itself*/public SshClient init(SshClientConfig config);/*** connect to server, and timeout if longer than {@code timeoutInSeconds}* @param timeoutInSeconds timeout in seconds* @return SshClient itself* @throws SshException if server is unreachable, usually the host and port is incorrect*/public SshClient connect(int timeoutInSeconds) throws SshException;/*** auth with password* @return SshClient itself* @throws SshException if username or password is incorrect*/public SshClient authPassword() throws SshException;/*** auth with key* @return SshClient itself* @throws SshException if username or public key is incorrect*/public SshClient authPublickey() throws SshException;/*** start session* @param shellMode <tt>true</tt>: communicate with server interactively in session, just like command line* <p><tt>false</tt>: only execute command once in session* @return SshClient itself* @throws SshException when start session failed**/public SshClient startSession(boolean shellMode) throws SshException;/**** @param command execute the {@code command} on server instance, and timeout if longer than {@code timeoutInSeconds}.* @param timeoutInSeconds timeout in seconds* @return SshResponse**/public SshResponse executeCommand(String command, int timeoutInSeconds);/*** set the listener on SshClient* @param listener notify listener when events occur in SshClient* @return SshClient itself*/public SshClient setEventListener(SshClientEventListener listener);/*** disconnect from server*/public void disconnect();/*** state of SshClient** @return SshClientState the state of ssh client* <p><tt>inited</tt> before {@link #startSession(boolean)} success* <p><tt>connected</tt> after {@link #startSession(boolean)} success* <p><tt>disconnected</tt> after {@link #disconnect()}, or any connection problem occurs*/public SshClientState getState();}
package com.devops.ssh;/**** Configuration used by {@link SshClient} to connect to remote server instance** @author Gary**/
public class SshClientConfig {private String host;private int port;private String username;private String password;private String privateKeyPath;private String id;/**** @return host address*/public String getHost() {return host;}/*** @param host host address, usually the ip address of remote server*/public void setHost(String host) {this.host = host;}/**** @return ssh port of the remote server*/public int getPort() {return port;}/*** @param port ssh port of the remote server*/public void setPort(int port) {this.port = port;}/**** @return ssh username of the remote server*/public String getUsername() {return username;}/**** @param username ssh username of the remote server*/public void setUsername(String username) {this.username = username;}/**** @return ssh password of the remote server*/public String getPassword() {return password;}/**** @param password ssh password of the remote server*/public void setPassword(String password) {this.password = password;}/*** @return ssh local key file path of the remote server*/public String getPrivateKeyPath() {return privateKeyPath;}/*** @param privateKeyPath local key file path of the remote server*/public void setPrivateKeyPath(String privateKeyPath) {this.privateKeyPath = privateKeyPath;}/**** @return id of the config*/public String getId() {return id;}/**** @param host server host address* @param port server ssh port* @param username server ssh username* @param password server ssh password* @param privateKeyPath local security key used to connect to server*/public SshClientConfig(String host, int port, String username, String password, String privateKeyPath) {this.id = host + port + username;if (null != password && password.length() > 0) {this.id += password;}if (privateKeyPath != null) {this.id += privateKeyPath;}this.host = host;this.port = port;this.username = username;this.password = password;this.privateKeyPath = privateKeyPath;}public SshClientConfig() {}@Overridepublic boolean equals(Object obj) {if (obj instanceof SshClientConfig) {return id.equals(((SshClientConfig) obj).getId());}return false;}@Overridepublic int hashCode() {return id.hashCode();}@Overridepublic String toString() {return this.id;}
}
package com.devops.ssh;/**** Set listener to a SshClient by {@link SshClient#setEventListener(SshClientEventListener)}* @author Gary**/
public interface SshClientEventListener {/*** after SshClient finished executing command* @param client the ssh client*/public void didExecuteCommand(Object client);/*** after SshClient disconnnect from the remote server* @param client the ssh client*/public void didDisConnected(Object client);/*** after SshClient start the ssh session* @param client the ssh client*/public void didConnected(Object client);
}
package com.devops.ssh;import com.devops.ssh.pool.SshClientPoolConfig;/**** Factory of {@link SshClient} implementation* <p> Create a new instance of {@link SshClientSSHJ} with {@link #newInstance(SshClientConfig)}* <p> Create a custom implementation of {@link SshClient} with {@link #newInstance(SshClientConfig, SshClientPoolConfig)}** @author Gary**/
public class SshClientFactory {/*** Create a new instance of {@link SshClientSSHJ}* @param config ssh connection configuration of the remote server* @return SshClient in inited state*/public static SshClient newInstance(SshClientConfig config){return newInstance(config, null);}/*** Create a custom implementation of {@link SshClient}* @param config ssh connection configuration of the remote server* @param poolConfig customized configuration* @return SshClient in inited state* @throws RuntimeException if SshClientImplClass in {@code poolConfig} is invalid*/public static SshClient newInstance(SshClientConfig config, SshClientPoolConfig poolConfig){try {SshClient client = null;if (poolConfig==null || poolConfig.getSshClientImplClass()==null){client = new SshClientSSHJ();}else {client = (SshClient)poolConfig.getSshClientImplClass().newInstance();}client.init(config);if(client instanceof SshClientSSHJ && poolConfig!=null && poolConfig.getServerCommandPromotRegex()!=null) {((SshClientSSHJ)client).setCommandPromotRegexStr(poolConfig.getServerCommandPromotRegex());}return client;} catch (InstantiationException e) {throw new RuntimeException("new instance failed", e);} catch (IllegalAccessException e) {throw new RuntimeException("new instance failed", e);}}}
package com.devops.ssh;import com.devops.ssh.exception.AuthException;
import com.devops.ssh.exception.LostConnectionException;
import com.devops.ssh.exception.SshException;
import com.devops.ssh.exception.TimeoutException;
import com.devops.ssh.pool.SshClientPoolConfig;
import net.schmizz.sshj.DefaultConfig;
import net.schmizz.sshj.SSHClient;
import net.schmizz.sshj.connection.channel.direct.Session;
import net.schmizz.sshj.connection.channel.direct.Session.Command;
import net.schmizz.sshj.connection.channel.direct.Session.Shell;
import net.schmizz.sshj.transport.TransportException;
import net.schmizz.sshj.transport.verification.PromiscuousVerifier;
import net.schmizz.sshj.userauth.keyprovider.KeyProvider;
import net.sf.expectit.Expect;
import net.sf.expectit.ExpectBuilder;
import net.sf.expectit.ExpectIOException;
import net.sf.expectit.Result;
import net.sf.expectit.matcher.Matcher;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.SocketException;
import java.nio.channels.ClosedByInterruptException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;import static net.sf.expectit.filter.Filters.removeColors;
import static net.sf.expectit.filter.Filters.removeNonPrintable;
import static net.sf.expectit.matcher.Matchers.contains;
import static net.sf.expectit.matcher.Matchers.regexp;/**** build-in {@link SshClient} implementation with <a href="https://github.com/hierynomus/sshj">hierynomus/SshJ</a>** <p>Trouble and shooting:* <p>Problem: {@link #authPublickey()} throw exceptions contains "net.schmizz.sshj.common.Buffer$BufferException:Bad item length"* <p>Solution: may caused by key file format issue,use ssh-keygen on a remote Linux server to generate the key*** @author Gary**/
public class SshClientSSHJ implements SshClient {private final static Logger logger = LoggerFactory.getLogger(SshClientSSHJ.class);private SshClientConfig clientConfig;private SSHClient client;private Expect expect = null;private Session session = null;private Shell shell = null;private boolean shellMode = false;private SshClientState state = SshClientState.inited;private SshClientEventListener eventListener;public String commandPromotRegexStr = "[\\[]?.+@.+~[\\]]?[#\\$] *";public Matcher<Result> commandPromotRegex = regexp(commandPromotRegexStr);// initialize DefaultConfig will consume resources, so we should cache itprivate static DefaultConfig defaultConfig = null;public static DefaultConfig getDefaultConfig() {if(defaultConfig==null) {defaultConfig = new DefaultConfig();}return defaultConfig;}/*** used in shell mode, once it start session with server, the server will return promot to client* <p>the promot looks like [centos@ip-172-31-31-82 ~]$* <p>if the build-in one does not fit, you can change it by {@link SshClientPoolConfig#setServerCommandPromotRegex(String)}* @param promot used to match promot from server*/public void setCommandPromotRegexStr(String promot) {this.commandPromotRegexStr = promot;this.commandPromotRegex = regexp(this.commandPromotRegexStr);}@Overridepublic SshClient init(SshClientConfig config) {this.clientConfig = config;return this;}private void validate() throws SshException {if(this.clientConfig == null) {throw new SshException("missing client config");}}@Overridepublic SshClient connect(int timeoutInSeconds) throws SshException{this.validate();if (timeoutInSeconds <= 0) {timeoutInSeconds = Integer.MAX_VALUE;} else {timeoutInSeconds = timeoutInSeconds * 1000;}return this.connect(timeoutInSeconds, false);}private SshClient connect(int timeoutInSeconds, boolean retry) throws SshException{logger.debug("connecting to " + this.clientConfig.getHost() + " port:" + this.clientConfig.getPort() + " timeout in:"+ (timeoutInSeconds / 1000) + " s");client = new SSHClient(getDefaultConfig());try {client.setConnectTimeout(timeoutInSeconds);client.addHostKeyVerifier(new PromiscuousVerifier());// client.loadKnownHosts();client.connect(this.clientConfig.getHost().trim(), this.clientConfig.getPort());logger.debug("connected to " + this.clientConfig.getHost().trim() + " port:" + this.clientConfig.getPort());} catch (TransportException e) {if(!retry) {logger.error("sshj get exception when connect and will retry one more time ", e);try {Thread.sleep(1000);} catch (InterruptedException e1) {}return this.connect(timeoutInSeconds, true);}else {String errorMessage ="connect to " + this.clientConfig.getHost().trim() + " failed";logger.error(errorMessage, e);throw new SshException(errorMessage, e);}} catch (Exception e) {String errorMessage ="connect to " + this.clientConfig.getHost().trim() + " failed";logger.error(errorMessage, e);throw new SshException(errorMessage, e);}return this;}@Overridepublic SshClient setEventListener(SshClientEventListener listener) {this.eventListener = listener;return this;}@Overridepublic SshClient authPassword() throws SshException {try {logger.debug("auth with password");client.authPassword(this.clientConfig.getUsername(), this.clientConfig.getPassword());} catch (Exception e) {String errorMessage = "ssh auth " + this.clientConfig.getHost() + " fail";logger.error(errorMessage, e);throw new AuthException(errorMessage, e);}return this;}@Overridepublic SshClient authPublickey() throws SshException {try {logger.debug("auth with key:"+this.clientConfig.getUsername()+","+this.clientConfig.getPrivateKeyPath());if (this.clientConfig.getPrivateKeyPath() != null) {KeyProvider keys = client.loadKeys(this.clientConfig.getPrivateKeyPath());client.authPublickey(this.clientConfig.getUsername(), keys);} else {client.authPublickey(this.clientConfig.getUsername());}} catch (Exception e) {String errorMessage = "ssh auth " + this.clientConfig.getHost() + " fail";logger.error(errorMessage, e);throw new AuthException(errorMessage, e);}return this;}@Overridepublic SshClient startSession(boolean shellMode) {logger.info("start session " + (shellMode ? " in shellMode" : ""));try {session = client.startSession();this.shellMode = shellMode;if (shellMode) {session.allocateDefaultPTY();shell = session.startShell();shell.changeWindowDimensions(1024, 1024, 20, 20);this.renewExpect(60);expect.expect(commandPromotRegex);}this.state = SshClientState.connected;try {if(this.eventListener!=null) {this.eventListener.didConnected(this);}} catch (Exception e) {}} catch (Exception e) {if(e instanceof ExpectIOException) {ExpectIOException ioException = (ExpectIOException)e;logger.error("start session fail with server input:"+ioException.getInputBuffer().replaceAll("[\\\n\\\r]", ""), e);}else {logger.error("start session fail", e);}this.disconnect();throw new RuntimeException("start session fail." + e.getMessage());} finally {// close expecttry {if (expect != null) {expect.close();}} catch (IOException e) {logger.error("close IO error", e);}expect = null;}return this;}@Overridepublic SshResponse executeCommand(String command, int timeoutInSeconds) {if (this.shellMode) {return this.sendCommand(command, timeoutInSeconds);} else {return this.executeCommand_(command, timeoutInSeconds);}}private SshResponse executeCommand_(String command, int timeoutInSeconds) {logger.info("execute command: " + command);SshResponse response = new SshResponse();try {Command cmd = session.exec(command);if (timeoutInSeconds < 0) {cmd.join(Long.MAX_VALUE, TimeUnit.SECONDS);} else {cmd.join(timeoutInSeconds, TimeUnit.SECONDS);}BufferedReader reader = new BufferedReader(new InputStreamReader(cmd.getInputStream(), "UTF-8"));BufferedReader error_reader = new BufferedReader(new InputStreamReader(cmd.getErrorStream(), "UTF-8"));List<String> outputLines = new ArrayList<>();logger.debug("finish executing command on " + this.clientConfig.getHost() + ", console:");String outputLine;while ((outputLine = error_reader.readLine()) != null) {logger.debug(outputLine);outputLines.add(outputLine);}while ((outputLine = reader.readLine()) != null) {logger.debug(outputLine);outputLines.add(outputLine);}response.setStdout(outputLines);logger.info("execute ssh command on " + this.clientConfig.getHost() + " completed, with exit status:" + cmd.getExitStatus());response.setCode(cmd.getExitStatus());} catch (Exception e) {if (e.getCause() instanceof InterruptedException || e.getCause() instanceof java.util.concurrent.TimeoutException) {logger.error("execute ssh on " + this.clientConfig.getHost() + " timeout");response.setException(new TimeoutException("execute ssh command timeout"));} else {logger.error("execute ssh on " + this.clientConfig.getHost() + ", command error", e);response.setException(new SshException("execute ssh command error "+e.getMessage()));}}finally {try {if(this.eventListener!=null) {this.eventListener.didExecuteCommand(this);}} catch (Exception e) {}}return response;}private SshResponse sendCommand(String command, int timeoutInSeconds) {SshResponse response = new SshResponse();if (this.state != SshClientState.connected) {response.setException(new LostConnectionException("client not connected"));response.setCode(0);return response;}try {this.renewExpect(timeoutInSeconds);// start expectlogger.info(this + " execute command : " + command);expect.send(command);logger.debug(this + " command sent ");if (!command.endsWith("\n")) {expect.send("\n");logger.debug(this + " command \\n sent ");}Result result2 = expect.expect(contains(command));Result result = expect.expect(commandPromotRegex);logger.debug("command execute success with raw output");logger.debug("------------------------------------------");String[] inputArray = result.getInput().split("\\r\\n");List<String> stout = new ArrayList<String>();if(inputArray.length>0) {for(int i=0;i<inputArray.length;i++) {logger.debug(inputArray[i]);if(i==inputArray.length-1 && inputArray[i].matches(commandPromotRegexStr)) {break;}stout.add(inputArray[i]);}}logger.debug("------------------------------------------");response.setStdout(stout);response.setCode(0);logger.info("execute ssh command on " + this.clientConfig.getHost() + " completed, with code:" + 0);} catch (Exception e) {response.setCode(1);response.setException(new SshException(e.getMessage()));logger.error("execute command fail", e);if(e instanceof ArrayIndexOutOfBoundsException) {// server may be shutdownresponse.setException(new LostConnectionException("lost connection"));this.disconnect();} else if (e instanceof ClosedByInterruptException) {response.setException(new TimeoutException("execute command timeout"));this.sendCtrlCCommand();}else if (e.getCause() instanceof SocketException) {// the socket may be closedresponse.setException(new LostConnectionException("lost connection"));this.disconnect();} else if (e.getMessage().contains("timeout")) {response.setException(new TimeoutException("execute command timeout"));this.sendCtrlCCommand();}else {this.sendCtrlCCommand();}} finally {// close expecttry {if (expect != null) {expect.close();}} catch (IOException e) {logger.error("close IO error", e);}expect = null;try {if(this.eventListener!=null) {this.eventListener.didExecuteCommand(this);}} catch (Exception e) {}}return response;}private void renewExpect(int timeoutInSeconds) throws IOException {if (expect!=null) {try {expect.close();}catch(Exception e) {e.printStackTrace();}}expect = new ExpectBuilder().withOutput(shell.getOutputStream()).withInputs(shell.getInputStream(), shell.getErrorStream()).withInputFilters(removeColors(), removeNonPrintable()).withExceptionOnFailure().withTimeout(timeoutInSeconds, TimeUnit.SECONDS).build();}private void sendCtrlCCommand() {try {logger.debug("send ctr-c command ... ");expect.send("\03");expect.expect(commandPromotRegex);logger.debug("send ctr-c command success ");} catch (IOException e1) {logger.error("send ctrl+c command fail", e1);}}@Overridepublic void disconnect() {if(this.state== SshClientState.disconnected) {return;}this.state = SshClientState.disconnected;try {if (shell != null) {shell.close();}} catch (IOException e) {logger.error("close ssh shell error", e);}try {if (session != null) {session.close();}} catch (IOException e) {logger.error("close sesion error", e);}try {if (client != null) {client.disconnect();client.close();}} catch (IOException e) {logger.error("close ssh conenction error", e);}logger.debug("ssh disconnect");try {if(this.eventListener!=null) {this.eventListener.didDisConnected(this);}} catch (Exception e) {}}@Overridepublic SshClientState getState() {return this.state;}
}
package com.devops.ssh;/**** state of SshClient, See {@link SshClient#getState()} for more information** @author Gary**/
public enum SshClientState {inited,connected,disconnected
}
package com.devops.ssh;import java.util.ArrayList;
import java.util.List;/**** Response return from {@link SshClient#executeCommand(String, int)}** @author Gary**/
public class SshResponse {private int code;private Exception exception;private List<String> stdout = new ArrayList<String>();/*** @return 0*/public int getCode() {return code;}public void setCode(int code) {this.code = code;}/**** @return the exception in {@link SshClient#executeCommand(String, int)}*/public Exception getException() {return exception;}public void setException(Exception exception) {this.exception = exception;}/**** @return the output from remote server after send command*/public List<String> getStdout() {return stdout;}public void setStdout(List<String> stdout) {this.stdout = stdout;}}
运行测试Linux命令
echo 'yes'
运行测试 shell 脚本
相关文章:

DevOps自动化平台开发之 Shell脚本执行的封装
基础知识 基于如下技术栈开发DevOps平台 Spring Boot Shell Ansible Git Gitlab Docker K8S Vue 1、spring boot starter的封装使用 2、Shell脚本的编写 3、Ansible 脚本的编写 4、Docker 的使用与封装设计 本篇介绍如何使用Java封装Linux命令和Shell脚本的使用 将其设计成…...

STM32CubeIDE(I2C)
目录 一、IIC轮询模式 1.1 配置 1.2 编写AHT20驱动 1.2.1 aht20.h 1.2.2 aht20.c 二、I2C中断 2.1 打开中断 2.2 分离读取流程 2.3 在主函数中重新编写读取流程 2.4 在i2c.c中重新定义stm32f1xx_hal_i2c.h中的两个函数 三、I2CDMA 3.1 配置DMA通道 3.2 代码的修改 一…...

http 请求报文响应报文的格式以及Token cookie session 区别
面试必备 http 请求报文响应报文的格式Token cookie session 区别 http 请求报文响应报文的格式 HTTP 请求报文和响应报文的格式如下: HTTP 请求报文格式: <方法> <路径> <协议版本> <请求头部字段1>: <值1> <请求头…...

智能汽车的主动悬架工作原理详述
摘要: 本文将详细介绍主动悬架功能原理设计。 主动悬架是车辆上的一种汽车悬架。它使用车载系统来控制车轮相对于底盘或车身的垂直运动,而不是由大弹簧提供的被动悬架,后者的运动完全由路面决定。主动悬架分为两类:真正的主动悬架…...

vue2和vue3的一些技术点复习
二、vue2 1、vue2对已有组件二次封装,例如fes2 input 组件(文档链接) 子组件 <template><div class"keyboard-page"><wb-input:id"keyBoardId":placeholder"placeholder" :type"type&q…...

安装nvm 切换node版本
1. 安装 nvm curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.1/install.sh | bash 验证nvm安装完成 command -v nvm 如果安装完成,就会显示如下 nvm 2. 查看 nvm 可以安装的 node 版本 查看可以安装的版本 nvm ls-remote 查看所有可以安装的…...

【html中的BFC是什么】
BFC(块级格式化上下文)是 CSS 中的一种盒模型布局,是指一个独立的块级容器,容器内部的元素会按照一定规则进行布局。 BFC 具体的规则有以下几个: BFC 内部的元素在垂直方向上相互排列,不会出现浮动的情况。…...

苹果账号被禁用怎么办
转载:苹果账号被禁用怎么办 目录 禁用的原因 解除Apple ID禁用 [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-UKQ1ILhC-1689932607373)(data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw)]编辑 …...

跨境出海企业,如何防范恶意退货欺诈
很多出海企业遭遇到过恶意退货事件。 2021年,某跨境商家在海外电商平台运营超过13年。有一次,有个海外买家买了一台二手的数码摄像机。在买家收到货后,却声称商品备在使用了45分钟之后便自动关机,且不能继续充电。该商家很肯定产…...

数据出境要依法“过安检”!什么是数据出境?
为了规范数据出境活动,保护个人信息权益,维护国家安全和社会公共利益,促进数据跨境安全、自由流动。从2022年9月1日起,《数据出境安全评估办法》施行。什么是数据出境,什么情况下需要进行数据出境安全评估呢࿱…...

ARM——点灯实验
循环点灯 RCC寄存器使能GPIOE、GPIOF组寄存器 修改GPIOx组寄存器下的值 通过GPIOx_MODER寄存器设置为输出模式通过GPIOx_OTYOER寄存器设置为推挽输出类型通过GPIOx_OSPEEDR寄存器设置为低速输出通过GPIOx_PUPDR寄存器设置为禁止上下拉电阻点灯 通过GPIOx_ODR寄存器设置为高电…...

Kubernetes 使用 helm 部署 NFS Provisioner
文章目录 1. 介绍2. 预备条件3. 部署 nfs4. 部署 NFS subdir external provisioner4.1 集群配置 containerd 代理4.2 配置代理堡垒机通过 kubeconfig 部署 部署 MinIO添加仓库修改可配置项 访问nodepotingress 1. 介绍 NFS subdir external provisioner 使用现有且已配置的NFS…...

Istio Pilot源码学习(二):ServiceController服务发现
本文基于Istio 1.18.0版本进行源码学习 4、服务发现:ServiceController ServiceController是服务发现的核心模块,主要功能是监听底层平台的服务注册中心,将平台服务模型转换成Istio服务模型并缓存;同时根据服务的变化,…...

Spring框架中的ResourcePatternResolver只能指定jar包内文件,指定容器中文件路径报错:FileNotFoundException
原始代码: public static <T> T getFromFile(String specifiedFile, String defaultClasspathFile, Class<T> expectedClass) {try {ResourcePatternResolver resolver new PathMatchingResourcePatternResolver();Resource[] resources resolver.ge…...

pytorch工具——认识pytorch
目录 pytorch的基本元素操作创建一个没有初始化的矩阵创建一个有初始化的矩阵创建一个全0矩阵并可指定数据元素类型为long直接通过数据创建张量通过已有的一个张量创建相同尺寸的新张量利用randn_like方法得到相同尺寸张量,并且采用随机初始化的方法为其赋值采用.si…...

解决Jmeter响应内容显示乱码
一、问题描述 jmeter在执行接口请求后,返回的响应体里面出现乱码现象,尽管在调了对应请求的响应编码也无用,现找到解决办法。 二、解决办法 进入到jmeter的bin目录下,找到jmeter.properties,通过按ctrlF快速定位查找到…...

ChatGPT和搜索引擎哪个更好用
目录 ChatGPT和搜索引擎的概念 ChatGPT和搜索引擎的作用 ChatGPT的作用 搜索引擎的作用 ChatGPT和搜索引擎哪个更好用 总结 ChatGPT和搜索引擎的概念 ChatGPT是一种基于对话的人工智能技术,而搜索引擎则是一种用于在互联网上查找和检索信息的工具。它们各自具…...

Nginx 301 https跳转后出现跨域和混合内容问题 —— 筑梦之路
问题 在浏览器地址栏敲入url访问静态资源目录时,发现默认跳转到了http协议的地址 如上图所示,客户端https请求先到达API网关,然后网关将请求通过http协议转发到静态资源服务器。 调出浏览器发现客户端发送的https请求收到了一个301状态码的响…...

记录--关于前端的音频可视化-Web Audio
这里给大家分享我在网上总结出来的一些知识,希望对大家有所帮助 背景 最近听音乐的时候,看到各种动效,突然好奇这些音频数据是如何获取并展示出来的,于是花了几天功夫去研究相关的内容,这里只是给大家一些代码实例&…...

docker-compose yml配置、常用命令
下载完docker-compose后,如果想使用docker-compose命令开头,需要创建软连接 sudo ln -s /usr/local/lib/docker/cli-plugins/docker-compose /usr/bin/docker-compose 1.docker-compose.yml文件编排 一个 docker-compose.yml 文件的顶层元素有ÿ…...

【实战】 七、Hook,路由,与 URL 状态管理(下) —— React17+React Hook+TS4 最佳实践,仿 Jira 企业级项目(十三)
文章目录 一、项目起航:项目初始化与配置二、React 与 Hook 应用:实现项目列表三、TS 应用:JS神助攻 - 强类型四、JWT、用户认证与异步请求五、CSS 其实很简单 - 用 CSS-in-JS 添加样式六、用户体验优化 - 加载中和错误状态处理七、Hook&…...

【MySQL】_5.MySQL的联合查询
目录 1. 笛卡尔积 2. 内连接 2.1 示例1:查询许仙同学的成绩 2.2 示例2: 查询所有同学的总成绩,及同学的个人信息 2.3 示例3:查询所有同学的科目及各科成绩,及同学的个人信息 3. 外连接 3.1 情况一:两…...

【后端面经】微服务构架 (1-3) | 熔断:熔断-恢复-熔断-恢复,抖来抖去怎么办?
文章目录 一、前置知识1、什么是熔断?2、什么是限流?3、什么是降级?4、怎么判断微服务出现了问题?A、指标有哪些?B、阈值如何选择?C、超过阈值之后,要不要持续一段时间才触发熔断?5、服务恢复正常二、面试环节1、面试准备2、面试基本思路三、总结 在微服务构架中…...

对UITextField输入内容的各种限制-总结
使用代理方法来限制输入框中的字数,输入的符号,输入的数字大小等各种限制 限制输入字数 已经有小数点了,就不能继续输入小数点 不能输入以0为开头的内容 不能输入以.为开头的内容 小数点后只允许输入一位数 只能输入100以下的数值 **不能包括…...

【图论】二分图
二分图,即可以将图中的所有顶点分层两个点集,每个点集内部没有边 判定图为二分图的充要条件:有向连通图不含奇数环 1、染色法 可以解决二分图判断的问题 步骤与基本思路 遍历图中每一个点,若该点未被染色,则遍历该…...

数据结构——(一)绪论
👉数据元素整体思维导图 欢迎补充 一、基本概念❤️ 1.1基本术语⭐️ (1)数据 客观事务属性的数字、字符。 (2)数据元素 数据元素是数据的基本单位,一个数据元素可由若干数据项组成,数据项是…...

[ 华为云 ] 云计算中Region、VPC、AZ 是什么,他们又是什么关系,应该如何抉择
前几天看到一个问答帖,我回答完了才发现这个帖子居然是去年的也没人回复,其中他问了一些华为云的问题,对于其中的一些概念,这里来总结讲解一下,希望对学习华为云的小伙伴有所帮助。 文章目录 区域(Region&a…...

表单验证:输入的字符串以回车分隔并验证是否有
公司项目开发时,有一个需求,需要对输入的字符串按回车分隔并验证是否有重复项,效果如下: 表单代码: <el-form-item label"IP地址条目:" prop"ipAddressEntry"><el-inputtype&…...

智能财务分析-亿发财务报表管理系统,赋能中小企业财务数字化转型
对于许多中小企业来说,企业重要部门往往是财务和业务部门。业务负责创收,财务负责控制成本,降低税收风险。但因管理机制和公司运行制度的原因,中小企业往往面临着业务与财务割裂的问题,财务数据不清晰,无法…...

图为科技T501赋能工业机器人 革新传统工业流程
工业机器人已成为一个国家制造技术与科技水平的重要衡量标准,在2019年,中国工业机器人的组装量与产量均位居了全球首位。 当前,工业机器人被广泛用于电子、物流、化工等多个领域之中,是一种通过电子科技和机械关节制作出来的智能机…...