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();
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); } finally { session.close(); } } finally { ssh.disconnect(); } }
public static void main(String... args) throws IOException { App app = new App(); app.diskCheck(); app.restartRecon(); } }
|