LoginSignup
6
4

More than 5 years have passed since last update.

Fabricによる リモート操作の自動化

Last updated at Posted at 2017-09-19

IT automation libraryである fabric (python製)を使います。

fabricは python2.5-2.7で使う必要があります(python3非対応)

fabricの使い方はとても簡単です:
1. fabfile.py 内に実行したい操作の関数func()を作る
2. terminalで、$ fab func を実行する

fabricのインストールは pipでどうぞ

$ pip install fabric

1. 実行スクリプトの作成

$ sudo nano fabfile.py
fabfile.py
# coding: utf-8
from fabric.api import env, run, sudo

env.hosts = ["192.168.11.161",
             "192.168.11.162",
             "192.168.11.163",
             "192.168.11.164"]
env.user = "pi"
env.password = "raspberry"

def hello():
    run("ifconfig wlan0")
    run("ls")

def fix_100m_full():
    sudo("ethtool -s eth0 autoneg off speed 100 duplex full")
    sudo("ethtool eth0")

2. fabricの実行

実行可能タスクの確認

$ fab -l

hello
fix_100m_full

タスクの実行(Auto-negotiationをオフにする自動スクリプトを実行してみます)

#$ fab <タスク名>
$ fab fix_100m_full | grep Auto-nego

[192.168.11.161] out:   Auto-negotiation: off
[192.168.11.162] out:   Auto-negotiation: off
[192.168.11.163] out:   Auto-negotiation: off
[192.168.11.164] out:   Auto-negotiation: off

参考

6
4
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
6
4