远程ssh连接执行shell脚本工具类
import com.jcraft.jsch.ChannelShell;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;
import com.sinosig.tianshu.ExecutionLxdasScript;
import org.apache.log4j.Logger;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
public class sshLxdasUtil {
private static final Logger logger = Logger.getLogger(ExecutionLxdasScript.class);
public static List<String> getShLxdas(String ip, String user, String password,String shellPath,String logPath) {
try {
JSch jSch = new JSch();
Session session = jSch.getSession(user, ip);
session.setPassword(password);
session.setConfig("StrictHostKeyChecking", "no");
session.connect();
ChannelShell shell = (ChannelShell) session.openChannel("shell");
String hdfsCommand1 = "sh " +shellPath+" >"+logPath +" 2>&1";
String hdfsCommand2 = "awk '{print}' " +logPath;
InputStream inputStream = shell.getInputStream();
OutputStream outputStream = shell.getOutputStream();
PrintWriter printWriter = new PrintWriter(outputStream);
shell.setPty(true);
shell.connect();
printWriter.println(hdfsCommand1);
printWriter.println(hdfsCommand2);
printWriter.println("exit");
printWriter.flush();
outputStream.flush();
BufferedReader in = new BufferedReader(new InputStreamReader(inputStream));
String msg = null;
List<String> lis = new ArrayList<>();
while ((msg = in.readLine()) != null) {
lis.add(msg.trim());
}
in.close();
session.disconnect();
return lis;
} catch (Exception e) {
e.printStackTrace();
logger.error("错误:", e);
return null;
}
}
}
jsch所需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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>executionLxdasScript</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.32</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
<version>4.4.9</version>
</dependency>
<dependency>
<groupId>com.jcraft</groupId>
<artifactId>jsch</artifactId>
<version>0.1.54</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.17.0</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.14</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.25</version>
<!-- <scope>compile</scope>-->
</dependency>
<dependency>
<groupId>com.typesafe.play</groupId>
<artifactId>play-mailer_2.12</artifactId>
<version>7.0.0</version>
</dependency>
</dependencies>
<build>
<!-- 增加编译xml文件配置-->
<!-- <resources>-->
<!-- <resource>-->
<!-- <directory>src/main/scala</directory>-->
<!-- <includes>-->
<!-- <include>**/*.xml</include>-->
<!-- <include>**/*.perperties</include>-->
<!-- </includes>-->
<!-- </resource>-->
<!-- <resource>-->
<!-- <directory>src/main/resources</directory>-->
<!-- <includes>-->
<!-- <include>**/*.xml</include>-->
<!-- <include>**/*.perperties</include>-->
<!-- </includes>-->
<!-- </resource>-->
<!-- </resources>-->
<plugins>
<!-- 该插件用于将Scala代码编译成class文件 -->
<plugin>
<groupId>net.alchim31.maven</groupId>
<artifactId>scala-maven-plugin</artifactId>
<version>4.4.0</version>
<executions>
<execution>
<!-- 声明绑定到maven的compile阶段 -->
<goals>
<goal>compile</goal>
</goals>
</execution>
<!-- 声明 -->
<execution>
<id>compile-scala</id>
<phase>process-resources</phase>
<goals>
<goal>add-source</goal>
<goal>compile</goal>
</goals>
</execution>
<execution>
<id>scala-compile-first</id>
<phase>compile</phase>
<goals>
<goal>add-source</goal>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.3.0</version>
<configuration>
<descriptorRefs>
<descriptorRef>
jar-with-dependencies
</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
因为本项目同时使用了java和Scala代码,所以依赖中添加了编译Scala的部分。