Created
January 27, 2025 07:43
-
-
Save gwenn/17018294a26b9fdbaf77023a1cc9bc1b to your computer and use it in GitHub Desktop.
Min Java Ssh client
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.google.common.io.ByteStreams; | |
import com.google.common.io.LineReader; | |
import org.apache.sshd.client.SshClient; | |
import org.apache.sshd.client.auth.password.PasswordIdentityProvider; | |
import org.apache.sshd.client.channel.ClientChannel; | |
import org.apache.sshd.client.channel.ClientChannelEvent; | |
import org.apache.sshd.client.session.ClientSession; | |
import org.apache.sshd.common.session.SessionContext; | |
import org.slf4j.Logger; | |
import org.slf4j.LoggerFactory; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import java.io.InputStreamReader; | |
import java.time.Duration; | |
import java.util.Arrays; | |
import java.util.Collections; | |
import java.util.EnumSet; | |
import java.util.List; | |
import java.util.concurrent.TimeUnit; | |
import static org.apache.sshd.common.SshConstants.DEFAULT_PORT; | |
public class MinSshClient { | |
public static void exec(String username, String password, String host, String... commands) throws Exception { | |
exec(username, password, host, Arrays.asList(commands), (cmd, is) -> ByteStreams.copy(is, System.out)); | |
} | |
public static void foreach_line(String username, String password, String host, String command, LineHandler handler) throws Exception { | |
foreach_line(username, password, host, Collections.singletonList(command), handler); | |
} | |
public static void foreach_line(String username, String password, String host, List<String> commands, LineHandler handler) throws Exception { | |
exec(username, password, host, commands, (command, is) -> { | |
handler.start(command); | |
LineReader lr = new LineReader(new InputStreamReader(is)); | |
String line; | |
while ((line = lr.readLine()) != null) { | |
handler.handle(line); | |
} | |
handler.end(command); | |
}); | |
} | |
private static void exec(String username, String password, String host, List<String> commands, OutputHandler handler) throws Exception { | |
try (SshClient client = SshClient.setUpDefaultClient()) { | |
//client.getServerKeyVerifier(); | |
//JSch.setConfig("StrictHostKeyChecking", "no"); | |
client.setPasswordIdentityProvider(new PasswordIdentityProvider() { | |
@Override | |
public Iterable<String> loadPasswords(SessionContext sessionContext) { | |
return Collections.singleton(password); | |
} | |
}); | |
client.start(); | |
log.info("{}@{}", username, host); | |
try (ClientSession session = client.connect(username, host, DEFAULT_PORT) | |
.verify(1, TimeUnit.MINUTES) | |
.getSession()) { | |
session.auth().verify(1, TimeUnit.MINUTES); | |
for (String command : commands) { | |
try (ClientChannel channel = session.createExecChannel(command)) { | |
channel.setErr(System.err); | |
channel.open().verify(5, TimeUnit.MINUTES); | |
handler.handle(command, channel.getInvertedOut()); | |
channel.waitFor(EnumSet.of(ClientChannelEvent.CLOSED), Duration.ofMinutes(5)); | |
} | |
} | |
} | |
} | |
} | |
public interface OutputHandler { | |
void handle(String command, InputStream is) throws IOException; | |
} | |
public interface LineHandler { | |
default void start(String cmd) { | |
} | |
void handle(String line); | |
default void end(String cmd) { | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment