コンテンツにスキップ

Fabric

リモートホストにSSHでログインしてコマンドを打つためのツール。

Fabric 1.xからの主な変更点は以下の通り

Install

pip install fabric

Note

Windowsの場合は bcrypt のビルドにVC++が要るので https://visualstudio.microsoft.com/ja/visual-cpp-build-tools/ を使って C++ Build Tools を入れる

機能メモ

Async

fabricの裏でタスクランナーとして使われているinvoke 1.4から asynchronous オプションが追加された。
http://www.pyinvoke.org/changelog.html

これを使うと、非同期なコマンド実行を簡単に実装できる。

>>> from fabric import Connection
>>> c = Connection("192.168.122.18")
>>> server = c.run("iperf3 -s -1", asynchronous=True)
>>> client_result = c.run("iperf3 -c 127.0.0.1 -t5 -i1", hide=True)
>>> server_result = server.join()
>>> print(client_result)
Command exited with status 0.
=== stdout ===
Connecting to host 127.0.0.1, port 5201
[  5] local 127.0.0.1 port 42652 connected to 127.0.0.1 port 5201
[ ID] Interval           Transfer     Bitrate         Retr  Cwnd
[  5]   0.00-1.00   sec  3.72 GBytes  32.0 Gbits/sec    0    959 KBytes       
[  5]   1.00-2.00   sec  3.73 GBytes  32.0 Gbits/sec    0   1.87 MBytes       
[  5]   2.00-3.00   sec  3.73 GBytes  32.0 Gbits/sec    0   1.87 MBytes       
[  5]   3.00-4.00   sec  3.70 GBytes  31.7 Gbits/sec    0   1.87 MBytes       
[  5]   4.00-5.00   sec  3.72 GBytes  31.9 Gbits/sec    0   1.87 MBytes       
- - - - - - - - - - - - - - - - - - - - - - - - -
[ ID] Interval           Transfer     Bitrate         Retr
[  5]   0.00-5.00   sec  18.6 GBytes  31.9 Gbits/sec    0             sender
[  5]   0.00-5.00   sec  18.6 GBytes  31.9 Gbits/sec                  receiver

iperf Done.

(no stderr)
>>> print(server_result)
Command exited with status 0.
=== stdout ===
-----------------------------------------------------------
Server listening on 5201
-----------------------------------------------------------
Accepted connection from 127.0.0.1, port 42650
[  5] local 127.0.0.1 port 5201 connected to 127.0.0.1 port 42652
[ ID] Interval           Transfer     Bitrate
[  5]   0.00-1.00   sec  3.72 GBytes  32.0 Gbits/sec                  
[  5]   1.00-2.00   sec  3.73 GBytes  32.0 Gbits/sec                  
[  5]   2.00-3.00   sec  3.73 GBytes  32.0 Gbits/sec                  
[  5]   3.00-4.00   sec  3.70 GBytes  31.7 Gbits/sec                  
[  5]   4.00-5.00   sec  3.72 GBytes  31.9 Gbits/sec                  
[  5]   5.00-5.00   sec  1.12 MBytes  30.2 Gbits/sec                  
- - - - - - - - - - - - - - - - - - - - - - - - -
[ ID] Interval           Transfer     Bitrate
[  5]   0.00-5.00   sec  18.6 GBytes  31.9 Gbits/sec                  receiver

(no stderr)

ProxyJump

Node-1を踏み台にNode-2にアクセスしたい場合、インスタンスをネストするだけで良い。

https://docs.fabfile.org/en/2.5/concepts/networking.html#proxyjump

from fabric import Connection
node_1 = Connection("node-1")
node_2 = Connection("node-2", gateway=node_1)
node_2.run("hostname")

Paramiko debug

import logging
paramiko_logger = logging.getLogger("paramiko")
paramiko_logger.setLevel(logging.DEBUG)
logging.basicConfig(level=logging.INFO)

1系

Error

メンテ対象外

現在、Fabricは2系にアップグレードされており、1系とは互換性が無い。
最終バージョンは 1.14.1 と思われるがメンテナンスされる予定は無いため利用しない。
python3対応した fabric3 もしくは fab-classic というパッケージもある。

Install

バージョンを指定しない限り2系がインストールされる。

pip install fabric==1.14.1

コマンドライン実行

コマンドラインから複数のターゲットを対象に任意のコマンドを発行できる

(fabric) C:\Users\ruy\PycharmProjects\ainoniwa-tools>fab -H 192.168.122.42 -u yuki -- uname -a
[192.168.122.42] Executing task '<remainder>'
[192.168.122.42] run: uname -a
[192.168.122.42] Login password for 'yuki':
[192.168.122.42] out: Linux fumika 4.4.0-98-generic #121-Ubuntu SMP Tue Oct 10 14:24:03 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux
[192.168.122.42] out:


Done.
Disconnecting from 192.168.122.42... done.

最終更新日: 2021-05-19 14:16:14