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
Asked
Active
Viewed 660 times
2
-
Questions asking us to recommend or find a tool, software library, tutorial or other off-site resource are [off-topic for Stack Overflow](https://stackoverflow.com/help/on-topic) as they tend to attract opinionated answers and spam. Instead, describe your specific problem and what has been done so far to solve it. Thank you! – Opsse Jan 28 '19 at 12:08
-
The problem I'm looking for way to run shell commands on all ESXi host without knowing the root password of ESXi host I was working on workaround by creating local user using HostLocalAccountManager but then you need to add permission using AuthorizationManager from VMware SDK I can't find sub AuthorizationManager for ESXi host only I can access the one for vCenter which doesn't see local users on esxi. So is there a way to get service instance connection directly to Host by using vCenter SSO ? – Hisham Ayyad Jan 28 '19 at 12:19
1 Answers
0
I see here 2 main building-blocks:
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)
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()

Jossef Harush Kadouri
- 32,361
- 10
- 130
- 129
-
Thanks Jossef but I believe this is assuming you are connecting to ESXi host directly and you know root password. My question that how run shell/cli command not necessarily over ssh on any host in vCenter inventory using, so in my use case I will smartconnect to vcenter then create view of ESXi hosts and I need to run host specific cli on each host at, To give more related example assume you want to upload file to all esxi host to tmp from MOB methods I can request generic service ticket which I can use in auth header while uploading file to ESXi without knowing root password – Hisham Ayyad Feb 13 '19 at 06:10
-
So Do we have any method on MOB I can use to run command on ESXi host or method can give me token or ticket when smartconnnecting to ESXi host directly without need of root password ? – Hisham Ayyad Feb 13 '19 at 06:13