trigger host custom shell command using VMware SDK

747 Views Asked by At

I'm looking a method to run custom shell command on all ESXi host connected to vCenter using VMware Web service SDK e.g. Pyvmomi

1

There are 1 best solutions below

2
On

I see here 2 main building-blocks:

  1. You need to to ensure SSH is enabled for remote access (It may be disabled by default on the EXSi server. Enabling it can be done via the SDK)

  2. You'll need a python SSH client to connect via SSH and execute remote commands


I'm the author of a python package called vmwc (a high-level VMware SDK client based on pyvmomi). Combining it with an SSH library such as paramiko will give you a simple solution.

Installation

pip install vmwc paramiko 

usage:

#!/usr/bin/env python

from vmwc import VMWareClient
import paramiko


def main():
    host = '192.168.1.1'
    username = '<username>'
    password = '<password>'
    remote_ssh_command = 'touch /tmp/hello-world' # Your remote command

    with VMWareClient(host, username, password) as client:
        client.enable_ssh()

        ssh = paramiko.SSHClient()
        ssh.connect(host, username=username, password=password)
        ssh.exec_command(remote_ssh_command)

        client.disable_ssh() # optional in case you want to close the ssh access


if __name__ == '__main__':
    main()