Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import net.schmizz.sshj.SSHClient;
- import net.schmizz.sshj.common.IOUtils;
- import net.schmizz.sshj.connection.channel.direct.Session;
- import java.io.IOException;
- import java.util.concurrent.TimeUnit;
- /** This examples demonstrates how a remote command can be executed. */
- public class Exec {
- public static void main(String... args)
- throws IOException {
- String user = "pi";
- String password = "raspberry";
- String host = "192.168.1.10";
- int port = 22;
- final SSHClient ssh = new SSHClient();
- ssh.loadKnownHosts();
- //disable verification altogether:
- // sshClient.addHostKeyVerifier(new PromiscuousVerifier());
- ssh.connect(host);
- try {
- ssh.authPassword(user,password);
- final Session session = ssh.startSession();
- try {
- String execPing = "ping -c 1 google.com";
- final Session.Command cmd = session.exec(execPing);
- System.out.println(IOUtils.readFully(cmd.getInputStream()).toString());
- cmd.join(5, TimeUnit.SECONDS);
- System.out.println("\n** exit status: " + cmd.getExitStatus());
- } finally {
- session.close();
- }
- } finally {
- ssh.disconnect();
- }
- }
- }
Add Comment
Please, Sign In to add comment