Run on screen. Target server has ssh key authentication set in advance
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"
Recommended Posts