2021-08-05

This commit is contained in:
2021-08-05 11:46:27 +09:00
parent 2a56dff104
commit 438bf4bd5c
58 changed files with 2418 additions and 33 deletions

View File

@@ -1,7 +1,15 @@
/*
* Examples for Java
*
* Copyright (c) 2021. Elex. All Rights Reserved.
* https://www.elex-project.com/
*/
plugins {
id("elex-java")
}
dependencies {
// https://mvnrepository.com/artifact/com.jcraft/jsch
implementation("com.jcraft:jsch:0.1.55")
}

View File

@@ -0,0 +1,89 @@
/*
* Examples for Java
*
* Copyright (c) 2021. Elex. All Rights Reserved.
* https://www.elex-project.com/
*/
package kr.pe.elex.examples;
import com.jcraft.jsch.*;
import java.io.Closeable;
import java.io.InputStream;
import java.io.OutputStream;
public class SecureShell implements Closeable {
private final Session session;
public SecureShell(String host, int port, String user, String password) throws JSchException {
JSch jSch = new JSch();
session = jSch.getSession(user, host, port);
session.setPassword(password);
session.setUserInfo(new UserInfo() {
@Override
public String getPassphrase() {
return null;
}
@Override
public String getPassword() {
return null;
}
@Override
public boolean promptPassword(String message) {
return false;
}
@Override
public boolean promptPassphrase(String message) {
return false;
}
@Override
public boolean promptYesNo(String message) {
System.out.println(message);
// user type 'yes'
return true;
}
@Override
public void showMessage(String message) {
System.out.println(message);
}
});
session.connect();
}
@Override
public void close() {
session.disconnect();
}
public ChannelShell shell(InputStream is, OutputStream os) throws JSchException {
ChannelShell channel = (ChannelShell) session.openChannel("shell");
channel.setInputStream(is);
channel.setOutputStream(os);
channel.setAgentForwarding(true);
channel.setPtyType("vt100");
channel.connect();
return channel;
}
public ChannelExec exec(String command) throws JSchException {
ChannelExec channel = (ChannelExec) session.openChannel("exec");
channel.setCommand(command);
channel.setInputStream(null);
channel.setErrStream(System.err);
channel.connect();
return channel;
}
public void portForwardingL(int portL, String hostR, int portR) throws JSchException {
session.setPortForwardingL(portL, hostR, portR);
}
}

View File

@@ -0,0 +1,8 @@
/*
* Examples for Java
*
* Copyright (c) 2021. Elex. All Rights Reserved.
* https://www.elex-project.com/
*/
package kr.pe.elex.examples;

View File

@@ -0,0 +1,67 @@
/*
* Examples for Java
*
* Copyright (c) 2021. Elex. All Rights Reserved.
* https://www.elex-project.com/
*/
package kr.pe.elex.examples;
import com.elex_project.abraxas.IOz;
import com.jcraft.jsch.ChannelExec;
import com.jcraft.jsch.ChannelShell;
import com.jcraft.jsch.JSchException;
import jdk.nashorn.internal.ir.annotations.Ignore;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import static org.junit.jupiter.api.Assertions.*;
@Slf4j
class SecureShellTest {
private static final String host = "192.168.0.1";
private static final String user = "elex";
private static final String password = "test";
private static final int port = 22;
@Test
@Ignore
void shell() throws JSchException {
SecureShell ssh = new SecureShell(host, port, user, password);
ChannelShell shell = ssh.shell(System.in, System.out);
// ???
}
@Test
void exec() throws JSchException, IOException {
SecureShell ssh = new SecureShell(host, port, user, password);
ChannelExec channel = ssh.exec("ls -al");
String out = IOz.readStringFrom(channel.getInputStream(),"\n");
System.out.println(out);
if (channel.isClosed()){
channel.disconnect();
}
ssh.close();
}
@Test
void scpTo() throws JSchException, IOException {
String filename = "";
SecureShell ssh = new SecureShell(host, port, user, password);
ChannelExec channel = ssh.exec("scp -p -t "+ filename);
InputStream is = channel.getInputStream();
OutputStream os = channel.getOutputStream();
// ???
}
}