Call command through sshj

create a maven project mvn archetype:generate

copy sshj’s dependency to pom.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
<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>dh</groupId>
<artifactId>ssh-remote</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>

<name>ssh-remote</name>
<url>http://maven.apache.org</url>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>net.schmizz</groupId>
<artifactId>sshj</artifactId>
<version>0.8.0</version>
</dependency>

<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.6.1</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<version>0.9.29</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>0.9.29</version>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.1.1</version>
<executions>
<execution>
<phase>test</phase>
<goals>
<goal>java</goal>
</goals>
<configuration>
<mainClass>dh.App</mainClass>
<arguments>
<argument>arg0</argument>
<argument>arg1</argument>
</arguments>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

create logback.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<configuration>

<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<!-- encoders are assigned the type
ch.qos.logback.classic.encoder.PatternLayoutEncoder by default -->
<encoder>
<pattern>%d{HH:mm} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>

<!-- appender name="STDOUT"
class="ch.qos.logback.core.ConsoleAppender">
<layout class="ch.qos.logback.classic.PatternLayout">
<pattern>%-4relative [%thread] %-5level %class - %msg%n</pattern>
</layout>
</appender -->

<root level="off">
<appender-ref ref="STDOUT" />
</root>
</configuration>

coding App.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package dh;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

import net.schmizz.sshj.SSHClient;
import net.schmizz.sshj.common.IOUtils;
import net.schmizz.sshj.connection.channel.direct.Session;
import net.schmizz.sshj.connection.channel.direct.Session.Command;

public class App {
private static final String df_command = "df -g|head -3|tail -2";
private static final String recon_command = "/tmp/xx/pps-recon.sh";
private static final String user = "root";
private static final String passwd = "pass3822";
private static final String wap01fingerprint = "5a:34:f2:12:5f:2b:6c:8a:7d:8e:f0:1b:ca:1a:cb:f7";
private static final String wap02fingerprint = "2a:97:e4:7b:50:e2:8a:5c:38:ae:d4:bf:fa:8c:ee:01";

public void diskCheck() throws IOException {
run("10.13.135.71", wap01fingerprint, df_command);
run("10.13.135.72", wap02fingerprint, df_command);
}

private void restartRecon() throws IOException {
run("10.13.135.71", wap01fingerprint, recon_command);
run("10.13.135.72", wap02fingerprint, recon_command);
}

private void run(String host, String fingerprint, String command)
throws IOException {
final SSHClient ssh = new SSHClient();
//ssh.loadKnownHosts();

try {
ssh.addHostKeyVerifier(fingerprint);

ssh.connect(host);
ssh.authPassword(user, passwd);
final Session session = ssh.startSession();
try {
final Command cmd = session.exec(command);
System.out.println("<<" + host + ">> -- " + IOUtils.readFully(cmd.getInputStream())
.toString());
cmd.join(5, TimeUnit.SECONDS);
//System.out.println("\n** exit status: " + cmd.getExitStatus());
} finally {
session.close();
}
} finally {
ssh.disconnect();
}
}

public static void main(String... args) throws IOException {
App app = new App();
app.diskCheck();
app.restartRecon();
}
}

run: mvn compile package

file structure as below:

1
2
3
4
5
6
7
8
9
10
11
12
13
src
├─main
│ ├─java
│ │ └─dh
│ │ App.java
│ │
│ └─resources
│ logback.xml

└─test
└─java
└─dh
AppTest.java

run program in command line run.bat

1
2
3
4
@echo off
java -cp target/ssh-remote-1.0-SNAPSHOT.jar;C:/Users/HPUser/.m2/repository/ch/qos/logback/logback-core/0.9.29/logback-core-0.9.29.jar;C:/Users/HPUser/.m2/repository/ch/qos/logback/logback-classic/0.9.29/logback-classic-0.9.29.jar;C:/Users/HPUser/.m2/repository/net/schmizz/sshj/0.8.0/sshj-0.8.0.jar;C:/Users/HPUser/.m2/repository/org/slf4j/slf4j-api/1.6.1/slf4j-api-1.6.1.jar dh.App
rem mvn exec:java -Dexec.mainClass="dh.App"
rem mvn exec:java -Dexec.mainClass="dh.App" -Dexec.classpathScope=runtime