Call command through sshj

1. create a maven project
mvn archetype:generate

2. copy sshj's dependency to pom.xml

<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>

3. create logback.xml
<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>

4. coding App.java
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();
	}
}

5. run: mvn compile package

6. file structure as below:

src

├─main

│  ├─java

│  │  └─dh

│  │          App.java

│  │

│  └─resources

│          logback.xml

└─test

    └─java

        └─dh

                AppTest.java

 7. run program in command line
run.bat

@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

你可能感兴趣的:(Call command through sshj)