카테고리 없음

Python SSH remote - Compute Shell command

NATONY 2020. 4. 2. 19:08

서버 3대에 대한 hostname호출 ( Python SSH 사용)

[root@compute1:/root] $ vi ./show_sshcmdall.py 
#!/usr/bin/env python

from __future__ import print_function
from time import sleep
import sys
import os
import subprocess
import paramiko
import time

#OPENSTACK FILESYSTEM
host_lists = [ '192.168.0.11', '192.168.0.12', '192.168.0.13']

ssh_client = paramiko.SSHClient()
ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())

for host_list in host_lists:
    try:
        ssh_client.connect(host_list,username='root',password='root')
        stdin, stdout, stderr = ssh_client.exec_command('hostname')
        stdin.flush()
        time.sleep(1)
        print(stdout.read())
        time.sleep(1)
        print(stderr.read())
    except IOError:
        continue
    except:
        break

[root@compute1:/root] $ 

[root@compute1:/root] $ python ./show_sshcmdall.py
compute1

compute2

compute3

 

SSH를 사용하기 위해서는 paramiko를 설치해야 한다.

$ curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 1764k  100 1764k    0     0   145k      0  0:00:12  0:00:12 --:--:-- 83691
$ python get-pip.py
DEPRECATION: Python 2.7 reached the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 is no longer maintained. A future version of pip will drop support for Python 2.7. More details about Python 2 support in pip, can be found at https://pip.pypa.io/en/latest/development/release-process/#python-2-support
Collecting pip
  Downloading pip-20.0.2-py2.py3-none-any.whl (1.4 MB)
     |????????????????????????????????????????????????????????????????| 1.4 MB 599 kB/s 
Collecting wheel
  Downloading wheel-0.34.2-py2.py3-none-any.whl (26 kB)
Installing collected packages: pip, wheel
Successfully installed pip-20.0.2 wheel-0.34.2
$ pip2 install paramiko
DEPRECATION: Python 2.7 reached the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 is no longer maintained. A future version of pip will drop support for Python 2.7. More details about Python 2 support in pip, can be found at https://pip.pypa.io/en/latest/development/release-process/#python-2-support
Requirement already satisfied: paramiko in /usr/lib/python2.7/site-packages (2.1.1)
Requirement already satisfied: cryptography>=1.1 in /usr/lib64/python2.7/site-packages (from paramiko) (2.1.4)
Requirement already satisfied: pyasn1>=0.1.7 in /usr/lib/python2.7/site-packages (from paramiko) (0.3.7)
Requirement already satisfied: idna>=2.1 in /usr/lib/python2.7/site-packages (from cryptography>=1.1->paramiko) (2.5)
Requirement already satisfied: asn1crypto>=0.21.0 in /usr/lib/python2.7/site-packages (from cryptography>=1.1->paramiko) (0.23.0)
Requirement already satisfied: six>=1.4.1 in /usr/lib/python2.7/site-packages (from cryptography>=1.1->paramiko) (1.11.0)
Requirement already satisfied: cffi>=1.7 in /usr/lib64/python2.7/site-packages (from cryptography>=1.1->paramiko) (1.11.2)
Requirement already satisfied: enum34 in /usr/lib/python2.7/site-packages (from cryptography>=1.1->paramiko) (1.0.4)
Requirement already satisfied: ipaddress in /usr/lib/python2.7/site-packages (from cryptography>=1.1->paramiko) (1.0.18)
Requirement already satisfied: pycparser in /usr/lib/python2.7/site-packages (from cffi>=1.7->cryptography>=1.1->paramiko) (2.14)