Ssh connect using SSHJ from a Java 6 app

Thing you want to do

It is to make an SSH connection from a self-made application compiled with Java 6 to another machine using the SSHJ library. I used to try to do this using the JSch library, but this article As I wrote in 86c9e9efb5358601b25b), it doesn't work, so I'm trying to change it from the JSch library to the SSHJ library.

environment

access point

client

Sample code

I think that you can find many samples that perform password authentication when connecting with SSH, but it seems that there were few samples that perform key authentication (with passphrase), so I will write the sample code that succeeded in connecting as a memorandum. I will.

Simply connecting to SSH is not enough, so I tried to make the code download the file after connecting to SSH.

All-capital alphabets are the parts that need to be specified externally or defined in the code. Also, exception handling is a lot skipped.

Sample.java


        SSHClient ssh = new SSHClient();
        ssh.addHostKeyVerifier(new PromiscuousVerifier());
        try {
            ssh.connect(HOST, PORT);
            ssh.authPublickey(USER, ssh.loadKeys(PRIVATEKEYFILEPATH, PASSPHRASE));
            ssh.newSCPFileTransfer().download(SRCDIR, new FileSystemFile(DSTDIR));
            ssh.disconnect();
            ssh.close();
        } catch (UserAuthException e) {
            e.printStackTrace();
        } catch (TransportException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

Precautions for key authentication

The private key file that can be used with SSHJ seems to be a PEM format file.

I think that the private key file and public key file are created using the ssh-keygen command, but depending on the version of the ssh-keygen command used, the private key file in the following format is generated by default. .. If you use this private key file with the SSHJ library, authentication will fail.

-----BEGIN RSA PRIVATE KEY-----
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
:
:
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
-----END RSA PRIVATE KEY-----

To authenticate with the SSHJ library, use a private key file in the following format.

-----BEGIN RSA PRIVATE KEY-----
Proc-Type: 4,ENCRYPTED
DEK-Info: AES-128-CBC,xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
:
:
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
-----END RSA PRIVATE KEY-----

To generate a PEM-formatted private key file like the one above, specify -m PEM as an option for the ssh-keygen command.

-- that's all --

Recommended Posts

Ssh connect using SSHJ from a Java 6 app
Connect from Java to MySQL using Eclipse
Connect to Aurora (MySQL) from a Java application
Create a portfolio app using Java and Spring Boot
Using the database (SQL Server 2014) from a Java program 2018/01/04
Connect from Java to PostgreSQL
Using Docker from Java Gradle
Make a rhombus using Java
[Unresolved] An exception occurs when an SSH connection is executed using JSch from a Java 6 application.
I made a shopify app @java
Upload a file using Java HttpURLConnection
Access Teradata from a Java application
Create a Java project using Eclipse
Sample code using Minio from Java
Try passing values from Java Servlet to iPhone app using JSON
Data processing using stream API from Java 8
Using JavaScript from Java in Rhino 2021 version
Try running a Kubernetes Job from Java
Create a TODO app in Java 7 Create Header
Try making a calculator app in Java
Call Java methods from Nim using jnim
Deploy a Java web app on Heroku
[Java] Generate a narrowed list from multiple lists using the Stream API
If a person from Java learns PHP
[Java] I tried to connect using a connection pool with Servlet (tomcat) & MySQL & Java
Access Forec.com from Java using Axis2 Enterprise WSDL
GetInstance () from a @Singleton class in Groovy from Java
To become a VB.net programmer from a Java shop
Build a Kotlin app using OpenJDK's Docker container
Programming beginners learn PHP from a Java perspective-variables-
Create Scala Seq from Java, make Scala Seq a Java List
Try accessing the dataset from Java using JZOS
[Java] Get a random value from an array
Create a Java development environment using jenv on Mac
[Java] [SQL Server] Connect to local SQL Server 2017 using JDBC for SQL Server
Create a docker image that runs a simple Java app
Output the maximum value from the array using Java standard output
Studying java8 (such as reading a file using Stream)
How to jump from Eclipse Java to a SQL file
Call a method with a Kotlin callback block from Java
[Java] Text extraction from PowerPoint (ppt) using Apache POI
I tried using Log4j2 on a Java EE server
[Note] Create a java environment from scratch with docker
How to convert A to a and a to A using AND and OR in Java
I made a Wrapper that calls KNP from Java
Try to build a Java development environment using Docker
I tried scraping a stock chart using Java (Jsoup)
Ssh connect from container to git with VSCode Remote Container
I want to connect to Heroku MySQL from a client