2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

screenしてpythonでscreenをsplitしてリモートサーバーにsshログインする

Posted at

screen上で実行する。
対象サーバーはssh鍵認証を事前に設定しておく

screen.py
# !/usr/bin/python

import os
import time
from subprocess import Popen, PIPE, STDOUT


_SERVER_LIST     = (#('hostname', 'ipaddress'),
                     ('s1',      '192.168.0.1'),
                     ('s2',      '192.168.0.2'),
                     ('s3',      '192.168.0.3'),)

_SSH_USER        = 'admin'

_SUBPROCESS_ARGS = {'stdin'     : None,
                    'stdout'    : PIPE,
                    'stderr'    : STDOUT,
                    'shell'     : False,}

# ----------------------------------------------------------
# SCREEN
# ----------------------------------------------------------
def screen_ssh():

    for server_name, server_ip in _SERVER_LIST:

        screen_ssh = ('screen',
                      '-X',
                      'eval',
                      'split',
                      'focus',
                      'screen',
                      'title %s' % server_name,
                      'stuff "ssh -l %s %s\n"' % (_SSH_USER, server_ip))

        p = Popen(screen_ssh, **_SUBPROCESS_ARGS)
        time.sleep(1.0)

        for i in p.stdout : print i

# ----------------------------------------------------------
# MAIN
# ----------------------------------------------------------
if os.environ['TERM'] == "screen" :

    screen_ssh()

else :
    print "not screen"
2
2
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
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?