LoginSignup
11
9

More than 5 years have passed since last update.

tmux自動化のPythonスクリプトを作成した

Last updated at Posted at 2016-07-15

経緯

というわけで、最近使い始めたのですが、本番環境で運用しているサーバの台数が増えてきて、障害対応時などに複数台障害が発生していると、tmuxでセッション作成してウィンドウ作成して画面分割してを複数台手動でやるのはツライということでPythonの勉強がてら自動化してみました。

環境

  • tmux1.6.3
  • CentOS6.7
  • Python2.6.6

やりたいこと

  • 踏み台サーバへ自端末からSSH接続する
  • 踏み台サーバでtmuxを起動しシステム毎(サーバ10台程度)にセッションを作成
  • 各セッションの各ウィンドウは3ペイン構成にする

スクリプト

  • ホスト一覧はhosts.iniに記述する
#!/usr/bin/python
# -*- coding: utf-8 -*-

import subprocess
import ConfigParser

inifile = ConfigParser.SafeConfigParser()
inifile.read("./hosts.ini")

for section in inifile.sections():
    cmd = "tmux new-session -s %s -d" % section
    subprocess.call(cmd, shell=True)
    for key, value in inifile.items(section):
        new_window = "tmux new-window -t %s" % section
        rename     = "tmux rename-window %s" % key
        ssh_0      = "tmux send-keys -t %s.0 'ssh %s' C-m" % (key, value)
        ssh_1      = "tmux send-keys -t %s.1 'ssh %s' C-m" % (key, value)
        ssh_2      = "tmux send-keys -t %s.2 'ssh %s' C-m" % (key, value)
        subprocess.call(new_window, shell=True)
        subprocess.call(rename, shell=True)
        subprocess.call("tmux split-window" ,shell=True)
        subprocess.call("tmux split-window -h" ,shell=True)
        subprocess.call(ssh_0, shell=True)
        subprocess.call(ssh_1, shell=True)
        subprocess.call(ssh_2, shell=True)
    else:
        # new-session分のウィンドウが不要なため削除
        # この処理がなんだかイケていない
        del_window = "tmux kill-window -t bash"
        subprocess.call(del_window, shell=True)
hosts.ini
[webserver]
centos01=192.168.5.11
centos02=192.168.5.12
centos03=192.168.5.13
centos04=192.168.5.14
centos05=192.168.5.15
centos06=192.168.5.16

[database]
centos11=192.168.5.21
centos12=192.168.5.22
centos13=192.168.5.23
centos14=192.168.5.24
centos15=192.168.5.25
centos16=192.168.5.26

解説

  • 解説するまでもないカススクリプトですが、自分のための覚書として・・・\(^o^)/

ConfigParser

hosts.iniのパーサモジュールとしてConfigParserモジュールを使います!

  • 例えばこんなiniファイルがあった場合・・・。
hosts.ini
[webserver]
centos01=192.168.5.11
centos02=192.168.5.12
  • こんな感じでタプルのデータ型で返してくれる!便利!
>>> import ConfigParser
>>> inifile = ConfigParser.SafeConfigParser()
>>> inifile.read("./hosts.ini")
['./hosts.ini']
>>> print inifile.items("webserver")
[('centos01', '192.168.5.11'), ('centos02', '192.168.5.12')]

subprocess

tmuxコマンド実行のためにsubprocessモジュールを使います!

  • 外部コマンド実行のモジュールはいくつかあるけど、subprocess以外は現在推奨されていないようなのです!
さっきの続き
>>> import subprocess
>>> for key, value in inifile.items("webserver"):
...     cmd = "echo %s : %s" % (key, value)
...     subprocess.call(cmd, shell=True)
...
centos01 : 192.168.5.11
0
centos02 : 192.168.5.12
0

tmux

準備が整ったのでtmuxの以下のコマンドsubproseccモジュールを使ってを実行します!

新しいセッションを作成する
tmux new-session -s {session_name} -d
新しいウィンドウを作成する
tmux new-window -t {session_name}
ウィンドウ名の変更
tmux rename-window {window_name}
画面分割
tmux split-window
tmux split-window -h
各ペインでのSSH接続
tmux send-keys -t {window_name}.0 'ssh {IP}'
tmux send-keys -t {window_name}.1 'ssh {IP}'
tmux send-keys -t {window_name}.2 'ssh {IP}'

おわりに

なんだか全然スマートな感じではないですが、とりあえず最低限の要件は満たせたので、あとは少しづつ改良していければ良いなという気持ちです。

Rubyも楽しいけどPythonも楽しいなぁ〜\(^o^)/

11
9
1

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
11
9