Created
October 23, 2018 22:53
-
-
Save mageddo/b6f51b27e572a01b7fd657aa8581360b to your computer and use it in GitHub Desktop.
This file contains 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
dependencies { | |
compile group: 'com.hierynomus', name: 'sshj', version: '0.23.0' | |
compile group: 'org.bouncycastle', name: 'bcpkix-jdk15on', version: '1.57' | |
compile group: 'org.bouncycastle', name: 'bcprov-jdk15on', version: '1.57' | |
} |
This file contains 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 net.schmizz.sshj.SSHClient; | |
import net.schmizz.sshj.connection.channel.direct.Session; | |
import net.schmizz.sshj.sftp.SFTPClient; | |
import net.schmizz.sshj.transport.verification.PromiscuousVerifier; | |
import net.schmizz.sshj.userauth.keyprovider.PKCS8KeyFile; | |
import net.schmizz.sshj.userauth.method.AuthPublickey; | |
import org.apache.commons.io.IOUtils; | |
import org.slf4j.Logger; | |
import org.slf4j.LoggerFactory; | |
import org.springframework.stereotype.Component; | |
import java.nio.file.Files; | |
import java.nio.file.Paths; | |
@Component | |
public class SSHJmx { | |
private final Logger logger = LoggerFactory.getLogger(getClass()); | |
public static void main(String[] args) throws Exception { | |
new SSHJmx().doTransfer(); | |
} | |
void doTransfer() { | |
try { | |
testTransfer( | |
"/tmp/conc/testfile.t1", "/tmp/testfile.t1.transfered", "root", "13.0.0.5", | |
new String(Files.readAllBytes(Paths.get("/tmp/conc/conc"))) | |
); | |
} catch (Exception e){ | |
logger.error("status=fatal, msg={}", e.getMessage(), e); | |
} | |
} | |
public static void testTransfer(String source, String target, String user, String hostname, String privateKeyContent) throws Exception { | |
final SSHClient ssh = new SSHClient(); | |
ssh.loadKnownHosts(); | |
ssh.addHostKeyVerifier(new PromiscuousVerifier()); | |
ssh.connect(hostname); | |
try { | |
final PKCS8KeyFile keyFile = new PKCS8KeyFile(); | |
// keyFile.init(new File("/tmp/conc/conc")); | |
keyFile.init(privateKeyContent, privateKeyContent); | |
ssh.auth(user, new AuthPublickey(keyFile)); | |
try (SFTPClient sftp = ssh.newSFTPClient()) { | |
sftp.put(source, target); | |
} | |
try (Session.Command command = ssh.startSession().exec("ls -lha " + target)) { | |
System.out.println("-------------------------\nOUT"); | |
System.out.println(IOUtils.toString(command.getErrorStream())); | |
System.out.println(IOUtils.toString(command.getInputStream())); | |
} | |
} finally { | |
ssh.disconnect(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment