0
0

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.

リモートからNutanix(CVM)を操作。はじめてのPython

Last updated at Posted at 2019-12-12

やりたいこと

運用担当者さんと会話すると自動化(スクリプト化)が必須と言われることが多く、
運用の自動化の第一歩としてローカルPCからCVMを触ってみます。
改めて一から整理

準備

・Pythonのインストール
検索してインストール
・Paramikoのインストール
Pythonインストール後、コマンドプロンプトから下記を実行すると、paramikoがインストールできます(既に入ってるけど)
pip install paramiko

c:\cli>pip install paramiko
Requirement already satisfied: paramiko in c:\users\tatsuhiromatsumoto\appdata\local\programs\python\python38\lib\site-packages (2.7.1)
Requirement already satisfied: bcrypt>=3.1.3 in c:\users\tatsuhiromatsumoto\appdata\local\programs\python\python38\lib\site-packages (from paramiko) (3.1.7)
Requirement already satisfied: pynacl>=1.0.1 in c:\users\tatsuhiromatsumoto\appdata\local\programs\python\python38\lib\site-packages (from paramiko) (1.3.0)
Requirement already satisfied: cryptography>=2.5 in c:\users\tatsuhiromatsumoto\appdata\local\programs\python\python38\lib\site-packages (from paramiko) (2.8)
Requirement already satisfied: cffi>=1.1 in c:\users\tatsuhiromatsumoto\appdata\local\programs\python\python38\lib\site-packages (from bcrypt>=3.1.3->paramiko) (1.13.2)
Requirement already satisfied: six>=1.4.1 in c:\users\tatsuhiromatsumoto\appdata\local\programs\python\python38\lib\site-packages (from bcrypt>=3.1.3->paramiko) (1.13.0)
Requirement already satisfied: pycparser in c:\users\tatsuhiromatsumoto\appdata\local\programs\python\python38\lib\site-packages (from cffi>=1.1->bcrypt>=3.1.3->paramiko) (2.19)
WARNING: You are using pip version 19.2.3, however version 19.3.1 is available.
You should consider upgrading via the 'python -m pip install --upgrade pip' command.

サンプルコード

cVMへログインし情報を取ってくる

サンプルスクリプト

import json, paramiko

IP = '1.1.1.1'
USER = 'nutanix'
PASSWORD = 'pass-pass'

def main():
  output = get_df(IP, USER, PASSWORD)
  print(output)

def get_df(ip, user, password):
  client = paramiko.SSHClient()
  client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
  client.connect(ip, username=user, password=password, timeout=3.0)
  command = '/usr/local/nutanix/bin/acli host.list'
  (stdin, stdout, stderr) = client.exec_command(command)
  output = stdout.read().decode()
  client.close()
  return output

if __name__ == '__main__':
  main()

実行結果

c:\cli>python acli-list.py
Hypervisor address  Host UUID                             Schedulable  Hypervisor Type  Hypervisor Name
1.1.1.1        a99e2678-3f60-4b37-b201-5d0cfd226ed5  True         kKvm             AHV
1.1.1.2        1a1d1102-64ab-4cc1-a736-684a4ed83464  True         kKvm             AHV
1.1.1.3        de80adb3-f889-4139-bd68-b6e1d974d036  True         kKvm             AHV
1.1.1.4        a0dd6b68-25e5-48af-ab5f-e9ba62f67657  True         kKvm             AHV

まとめ

Pythonを使ってCVMの情報を取ってみました。
サンプルコードのコマンド部分を変更すれば各種コマンドが実行できます。Hello Worldレベルです。
一度リモートから実行することでスクリプト化、自動化の敷居が下がると思います。

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?