SSH の使用
%Net.SSH パッケージは、SSH (Secure ShellOpens in a new tab) 通信のサポートを提供します。このトピックでは、このパッケージに含まれているクラスについて簡単に説明します。
OpenSSL 3.0 では、プロバイダの新しい構想として、アルゴリズム実装のグループをパッケージ化する手法が導入されています。プロバイダの 1 つにレガシー・プロバイダがあります。OpenSSL でも InterSystems IRIS® データ・プラットフォームでも、既定ではレガシー・プロバイダはロードされません。
レガシー・プロバイダのアルゴリズムを使用しないことをお勧めします。このようなすべてのアルゴリズムは、OpenSSL のドキュメントOpens in a new tabを参照してください。インターシステムズでは、InterSystems IRIS とレガシー・アルゴリズムとの互換性を保証できません。
SSH セッションの作成
%Net.SSH.SessionOpens in a new tab は SSH セッションを表します。このクラスを使用するには、以下の手順を実行します。
-
クラスのインスタンスを作成します。
-
Connect() インスタンスのメソッドを使用して、サーバに接続します。
-
AuthenticateWithKeyPair()、AuthenticateWithUsername()、または AuthenticateWithKeyboardInteractive() を使用して自身をサーバに対して認証します。詳細は、%Net.SSH.SessionOpens in a new tab のクラス・リファレンスを参照してください。
-
%Net.SSH.SessionOpens in a new tab のその他のメソッドを使用して、リモート・システムとの間での単一ファイルの SCP (Secure Copy) 操作の実行、リモート・コマンドの実行、TCP トラフィックのトンネル、または SFTP 操作の実行を行います。クラス・リファレンスの %Net.SSH.SessionOpens in a new tab を参照してください。
例えば、OpenSFTP を使用して、SFTP 操作のセッションを使用します。このメソッドは、SFTP 操作に使用できる %Net.SSH.SFTPOpens in a new tab のインスタンスを参照で返します。次のセクションに示した用例を参照してください。
-
これらのクラスを使用できるサポート・プラットフォームの詳細は、%Net.SSH.SessionOpens in a new tab および %Net.SSH.SFTPOpens in a new tab のクラス・リファレンスを参照してください。
-
OpenSSL 3.0 を使用する SSH 接続では、暗号化アルゴリズムとして Blowfish も CAST もサポートしていません。そのような接続を確立しようとすると接続に失敗します。
サポートされる鍵タイプとホストキー・アルゴリズム
クライアントとサーバ間で SSH 接続を確立するには、両者が互いの身元を証明する必要があります。このために、それぞれが秘密鍵と公開鍵という 2 つの鍵を持ち、これらは公開鍵/秘密鍵ペアと呼ばれます。鍵ペアは以下のタイプと形式によって定義されます。
-
タイプ : 鍵ペアの生成に使用された暗号化アルゴリズム。
鍵ペアのタイプは、公開鍵 (.pub) ファイルに記述されます。例えば、ED25519 鍵ペアの場合、公開鍵に以下のように ssh-ed25519 という鍵タイプが表示されます。
ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIK+fbizguJPey1+7q2uNMvn8ku9Vd4MEt43jWfng8DAs user@example -
形式 : 鍵のエンコード方法。これは通常公開鍵と秘密鍵で異なります。公開鍵は常に OpenSSH 形式であり、これはサーバ上の .ssh/authorized_keys およびクライアント上の .ssh/known_hosts のエントリで使用される形式です。秘密鍵の形式はさまざまですが、よく使用されるものは OpenSSH や PEM です。
各鍵タイプには関連付けられたホストキー・アルゴリズムがあり、このアルゴリズムによって SSH 接続において鍵ペアがどのように使用されるかが決まります。InterSystems IRIS で鍵ペアを使用するには、そのタイプに関連付けられたホストキー・アルゴリズムが InterSystems IRIS とターゲット・サーバの両方でサポートされている必要があります。
RSA タイプの鍵ペアを除き、鍵タイプと関連付けられたホストキー・アルゴリズムは同じ名前を共有します。例えば、ssh-ed25519 タイプの鍵は、ssh-ed25519 のホストキー・アルゴリズムと関連付けられています。そのため、この鍵タイプを使用するには、InterSystems IRIS とターゲット・サーバの両方で ssh-ed25519 ホストキー・アルゴリズムをサポートしている必要があります。
この命名規則の例外は RSA (タイプ ssh-rsa) 鍵で、これはハッシュ関数の選択に依存しませんOpens in a new tab。つまり、ssh-rsa タイプの鍵は、以下のアルゴリズムのいずれでも使用できます。
-
ssh-rsa (SHA1 を使用するため非推奨)
-
rsa-sha2-256
-
rsa-sha2-512
お使いのバージョンの InterSystems IRIS でサポートされているホストキー・アルゴリズム (など) を確認するには、GetSupportedAlgorithms() を使用できます。以下に例を示します。
set ses = ##class(%Net.SSH.Session).%New()
set sc = ses.GetSupportedAlgorithms(.algos)
例 : SFTP 経由のファイルのリスト作成
次のメソッドは、SFTP を経由してサーバ上のファイルのリストを作成する方法を示しています。
Method SFTPDir(ftpserver, username, password) As %Status
{
set ssh = ##class(%Net.SSH.Session).%New()
set status = ssh.Connect(ftpserver)
set status = ssh.AuthenticateWithUsername(username,password)
//open an SFTP session and get that returned by reference
set status = ssh.OpenSFTP(.sftp)
//get a list of files
set status = sftp.Dir(".",.files)
set i=$ORDER(files(""))
while i'="" {
write $listget(files(i),1),!
set i=$ORDER(files(i))
}
quit $$$OK
}
例 : キーボード・インタラクティブによる認証
以下のターミナル・セッションは AuthenticateWithKeyboardInteractive() の使用方法を示しています。
set host="192.168.2.100"
set lambda="(u,i,p,f,c) quit $listbuild(c(""password""))"
set context("password")="fountain"
set sess=##class(%Net.SSH.Session).%New()
s status=sess.Connect(host)
set status=sess.AuthenticateWithKeyboardInteractive("root",lambda,.context)
例 : 鍵ペアによる認証
以下の例は AuthenticateWithKeyPair() の使用方法を示しています。
set host="example.com"
set username="Bob"
set publickeyfile="/Users/Bob/.ssh/id_ecdsa.pub"
set privatekeyfile="/Users/Bob/.ssh/id_ecdsa"
Set sess = ##class(%Net.SSH.Session).%New()
Set status = sess.Connect(host, 22)
Set status = sess.AuthenticateWithKeyPair(username, publickeyfile, privatekeyfile)
例 : リモート・コマンドの実行
以下の例は Execute() の使用方法を示しています。
// %Net.SSH.Session.Execute
// Specify the initial SSH connection information
Set host="192.168.2.37"
Set username = "SSHUser"
Set password = "SSHPassword"
// Set up the initial SSH connection
// localhost <--SSH--> 192.168.2.37
Set sshSession = ##class(%Net.SSH.Session).%New()
Set statusConnection = sshSession.Connect(host)
Set statusConnection = sshSession.AuthenticateWithUsername(username,password)
// Specify the command to execute on the remote host
Set command = "uname -a"
// Execute the command over the SSH connection
// tDevice is a pass-by-reference value to store the raw data passing through the connection
Set statusConnection = sshSession.Execute(command,.tDevice)
例 : ポートの転送
以下の例は ForwardPort() の使用方法を示しています。
// %Net.SSH.Session.ForwardPort
// Specify the initial SSH connection information
Set host="192.168.2.37"
Set username = "SSHUser"
Set password = "SSHPassword"
// Set up the initial SSH connection
// localhost <--SSH--> 192.168.2.37
Set sshSession = ##class(%Net.SSH.Session).%New()
Set statusConnection = sshSession.Connect(host)
Set statusConnection = sshSession.AuthenticateWithUsername(username,password)
// Specify the remote port forward information
Set remotehost = "192.168.2.100"
Set remoteport = 80
// Forward traffic via the SSH connection to a remote host:port
// localhost <--SSH--> 192.168.2.37 --SSHPortFwd--> 192.168.2.100:80
// tDevice is a pass-by-reference value to store the raw data passing through the connection
Set statusConnection = sshSession.ForwardPort(remotehost,remoteport,.tDevice)
関連項目
-
Using and debugging %Net.SSH.Session for SSH connectionsOpens in a new tab (Developer Community)