1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Scala+sshj:証明書認証をしないSCPアップロード/ダウンロード

Last updated at Posted at 2018-04-19

始めに

  • 意外とある証明書認証を行わないサイトに対して
  • SSHJを使用する
  • NullHostKeyVerifierを定義する

build.sbt

build.sbt
libraryDependencies ++= Seq(
  "com.hierynomus" % "sshj" % "0.21.1"
)

import

Sshj.scala
import java.io.IOException;
import net.schmizz.sshj.SSHClient;
import net.schmizz.sshj.xfer.FileSystemFile;
import net.schmizz.sshj.userauth.UserAuth;
import net.schmizz.sshj.userauth.UserAuthException;
import net.schmizz.sshj.userauth.UserAuthImpl;
import net.schmizz.sshj.userauth.keyprovider._;
import net.schmizz.sshj.userauth.method._;
import net.schmizz.sshj.userauth.password.PasswordFinder;
import net.schmizz.sshj.userauth.password.PasswordUpdateProvider;
import net.schmizz.sshj.userauth.password.PasswordUtils;
import net.schmizz.sshj.userauth.password.Resource;
import net.schmizz.sshj.SSHClient;
import net.schmizz.sshj.common.IOUtils;
import net.schmizz.sshj.connection.channel.direct.Session;
import net.schmizz.sshj.connection.channel.direct.Session.Command;
import net.schmizz.sshj.transport.TransportException;
import net.schmizz.sshj.transport.verification.PromiscuousVerifier;
import net.schmizz.sshj.userauth.UserAuthException;
import net.schmizz.sshj.userauth.keyprovider.FileKeyProvider;
import net.schmizz.sshj.userauth.keyprovider.OpenSSHKeyFile;
import net.schmizz.sshj.xfer.FileSystemFile;
import net.schmizz.sshj.transport.TransportImpl
import net.schmizz.sshj.xfer.scp.SCPFileTransfer  
import net.schmizz.sshj.transport.verification.HostKeyVerifier;
import java.security.PublicKey;

接続

証明書認証をしないための設定

  • NullHostKeyVerifierを定義
class NullHostKeyVerifier extends HostKeyVerifier {
    @Override
    def verify(arg0:String , arg1:Int, arg2:PublicKey ):Boolean={
        true
    }
}

インスタンス作成

val ssh:SSHClient  = new SSHClient()

証明書認証を行わない

ssh.addHostKeyVerifier(new NullHostKeyVerifier())

サーバー接続

ssh.connect(ホスト名,ポート番号)

ログイン認証

ssh.authPassword(ログインID,パスワード)

切断

ssh.disconnect()

SCPダウンロード

ssh.newSCPFileTransfer().download(ダウンロードファイル名, new FileSystemFile(フォルダパス))

SCPアップロード

ssh.newSCPFileTransfer().upload(new FileSystemFile(アップロードファイル名),フォルダパス)

簡単!

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?