[CENTOS] screen and split screen with python and ssh login to remote server

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

screen and split screen with python and ssh login to remote server
Fractal to make and play with Python
[ev3dev × Python] SSH Control (remote control with keyboard)
I tried to communicate with a remote server by Socket communication with Python.
Scraping tabelog with python and outputting to CSV
MessagePack-Try to link Java and Python with RPC
ODBC connection to FileMaker 11 Server Advanced with Python 3
Launch a web server with Python and Flask
Introduction and usage of Python bottle ・ Try to set up a simple web server with login function
Ssh connect to Cisco Catalyst with CentOS7 + Python3 + netmiko and save config locally
SSH login to the target server from Windows with a click of a shortcut
[Python] How to create a local web server environment with SimpleHTTPServer and CGIHTTPServer
Python and Ruby split
Local server with python
Procedure to load MNIST with python and output to png
I want to handle optimization with python and cplex
Try to operate DB with Python and visualize with d3
[Python] How to split and modularize files (simple, example)
Ssh to virtual environment with remote development of vscode
Something to enjoy with Prim Pro (X-Play) and Python
ODBC access to SQL Server from Linux with Python
Easy to use Nifty Cloud API with botocore and python
Programming with Python and Tkinter
Connect to BigQuery with Python
Encryption and decryption with Python
Python and hardware-Using RS232C with Python-
Login to website in Python
Connect to Wikipedia with Python
Post to slack with Python 3
[Python] How to play with class variables with decorator and metaclass
Send experiment results (text and images) to slack with Python
[Python] Summary of how to use split and join functions
I made a server with Python socket and ssl and tried to access it from a browser
Try to bring up a subwindow with PyQt5 and Python
Build a Python environment and transfer data to the server
How to do Bulk Update with PyMySQL and notes [Python]
[Let's play with Python] Image processing to monochrome and dots
Switch python to 2.7 with alternatives
Write to csv with Python
Easy HTTP server with Python
python with pyenv and venv
Convert video to black and white with ffmpeg + python + opencv
I tried to make GUI tic-tac-toe with Python and Tkinter
Library for specifying a name server and dig with python
Get additional data to LDAP with python (Writer and Reader)
How to log in to AtCoder with Python and submit automatically
Works with Python and R
Split screen into 3 with keyhac
Operate Jupyter with REST API to extract and save Python code
[Introduction to Python] How to split a character string with the split function
I want to solve APG4b with Python (only 4.01 and 4.04 in Chapter 4)
How to import CSV and TSV files into SQLite with Python
Linking Python and Arduino to display IME On / Off with LED
How to make a surveillance camera (Security Camera) with Opencv and Python
I tried to make a periodical process with Selenium and Python
How to deal with errors when installing Python and pip with choco
Try to display google map and geospatial information authority map with python
Throw something to Kinesis with python and make sure it's in
List split and join strings with split and join (Perl / PowerShell / Java / Kotlin / Python)
I tried to easily detect facial landmarks with python and dlib
Move data to LDAP with python Change / Delete (Writer and Reader)