Junos PyEZ を使用したファイル転送
概要 Junos PyEZを使用して、ローカルホストとJunosデバイス間の安全なコピー(SCP)ファイルを使用します。
Junos PyEZは、Junosデバイス上でファイル管理タスクを実行するためのユーティリティを提供します。Junos PyEZ jnpr.junos.utils.scp.SCP
クラスを使用して、ローカルホストとJunosデバイス間の安全なコピー(SCP)ファイルを作成できます。
メソッドとclose()
メソッドはSCP
open()
、デバイスとの接続を確立して終了します。その結果、クライアント アプリケーションがファイル コピー操作のみを実行する場合は、メソッドDevice
open()
とclose()
メソッドの呼び出しを省略できます。クラスのインスタンスはSCP
、と のメソッドを自動的に呼び出すopen()
close()
コンテキストマネージャーとして使用できます。例えば:
from jnpr.junos import Device from jnpr.junos.utils.scp import SCP dev = Device('router1.example.com') with SCP(dev) as scp: scp.put('local-file', remote_path='path') scp.get('remote-file', local_path='path')
SCP
では、 パラメーターを使用して progress
転送の進行状況を追跡できます。既定では、 SCP
進行状況メッセージは出力されません。転送完了間隔が 10% 以上の場合に既定の進行状況メッセージを出力するように設定 progress=True
します。
with SCP(dev, progress=True) as scp:
または、進行状況メッセージを出力するカスタム関数を定義し、関数の名前と同じパラメーターを設定する progress
こともできます。関数定義には、デバイス インスタンスと進行状況メッセージに対応する 2 つのパラメーターを含める必要があります。例えば:
def log(dev, report): print (dev.hostname + ': ' + report) def main(): ... with SCP(dev, progress=log) as scp:
以下のサンプル・プログラムは、 scp-test1.txt ファイルと scp-test2.txt ・ファイルをローカル・ホストからターゲット・デバイス上の /var/tmp ディレクトリーに転送してから、メッセージ・ログ・ファイルをターゲット・デバイスからローカル・ホスト上の logs ディレクトリーに転送します。メッセージログの名前が変更され、デバイスのホスト名がファイル名に追加されます。この例では、ローカルホストとデバイスですでに設定されているSSHキーを認証に使用します。
比較のために、プログラムはデフォルトの進行状況メッセージと、 という名前の log
関数で定義されているカスタム メッセージの両方を使用して、転送の進行状況を追跡します。
from jnpr.junos import Device from jnpr.junos.utils.scp import SCP def log(dev, report): print (dev.hostname + ': ' + report) def main(): dev = Device('router1.example.com') msgfile = 'logs/'+dev.hostname+'-messages' try: #Default progress messages with SCP(dev, progress=True) as scp1: scp1.put('scp-test1.txt', remote_path='/var/tmp/') scp1.get('/var/log/messages', local_path=msgfile) #Custom progress messages with SCP(dev, progress=log) as scp2: scp2.put('scp-test2.txt', remote_path='/var/tmp/') scp2.get('/var/log/messages', local_path=msgfile) except Exception as err: print (err) return if __name__ == "__main__": main()
転送の進行状況は標準出力に送られます。デフォルトの出力(progress=True
)には、デバイス名、転送中のファイル、および転送の進行状況がバイト単位とパーセンテージで含まれています。
router1.example.com: scp-test1.txt: 8 / 8 (100%) router1.example.com: logs/router1.example.com-messages: 0 / 229513 (0%) router1.example.com: logs/router1.example.com-messages: 24576 / 229513 (10%) router1.example.com: logs/router1.example.com-messages: 139264 / 229513 (60%) router1.example.com: logs/router1.example.com-messages: 229513 / 229513 (100%)
この場合、カスタム関数は同様の出力を生成します。
router1.example.com : scp-test2.txt: 1 / 1 (100%) router1.example.com : logs/router1.example.com-messages: 0 / 526493 (0%) router1.example.com : logs/router1.example.com-messages: 57344 / 526493 (10%) router1.example.com : logs/router1.example.com-messages: 106496 / 526493 (20%) router1.example.com : logs/router1.example.com-messages: 212992 / 526493 (40%) router1.example.com : logs/router1.example.com-messages: 319488 / 526493 (60%) router1.example.com : logs/router1.example.com-messages: 368640 / 526493 (70%) router1.example.com : logs/router1.example.com-messages: 475136 / 526493 (90%) router1.example.com : logs/router1.example.com-messages: 526493 / 526493 (100%)
プログラム実行後、ターゲットデバイスで コマンドを発行 file list
して、 scp-test1.txt ファイルと scp-test2.txt ファイルが正しいディレクトリにコピーされたことを確認します。
user1@router1> file list /var/tmp/scp-test* /var/tmp/scp-test1.txt /var/tmp/scp-test2.txt
ローカルホストでは、デバイスホスト名を含むように名前が変更された メッセージ ログファイルが logs ディレクトリに存在する必要があります。
user1@server:~$ ls logs router1.example.com-messages
デフォルトでは、Junos PyEZ はデフォルトの SSH 設定ファイル (~/.ssh/config) があればそれを照会します。ただし、デバイス インスタンスを作成するときに、引数リストに パラメーターDevice
を含めるssh_config
ことで、別の SSH 構成ファイルを指定できます。例えば:
ssh_config_file = '~/.ssh/config_dc' dev = Device('198.51.100.1', ssh_config=ssh_config_file)
さらに、引数リストに パラメーターDevice
を含めてssh_private_key_file
認証用の特定の SSH プライベートキーファイルを定義すると、インスタンスはSCP
ファイル転送時に認証に同じキーファイルを使用します。
key_file='/home/user1/.ssh/id_rsa_dc' dev = Device('198.51.100.1', ssh_private_key_file=key_file) with SCP(dev) as scp: scp.put('scp-test.txt', remote_path='/var/tmp/')
このクラスは SCP
、netcat をサポートする中間ホストを介してローカル ホストからターゲット デバイスにファイルを転送できるようにする ProxyCommand のサポートも提供します。これは、中間ホストを介してのみターゲットデバイスにログインできる場合に便利です。プロキシ コマンドを構成するには、SSH 構成ファイルに適切な情報を追加します。例えば:
user1@server:~$ cat ~/.ssh/config Host 198.51.100.1 User user1 ProxyCommand ssh -l user1 198.51.100.2 nc %h 22 2>/dev/null