0

I try to use python for get VM name on VMware (vSphere) when I execute this python script:

https://github.com/vmware/pyvmomi-community-samples/blob/master/samples/get_vm_names.py

I have this message :

python3 test2.py --host ip_of_vmware
usage: test2.py [-h] -s HOST [-o PORT] -u USER [-p PASSWORD] [-S]
test2.py: error: the following arguments are required: -u/--user

I don't know how to execute this script.

I think this line which used to put in parameter:

 si = SmartConnectNoSSL(host=args.host,
                               user=args.user,
                               pwd=args.password,
                               port=int(args.port))
        atexit.register(Disconnect, si)

I want to know how to execute this script.

serenesat
  • 4,611
  • 10
  • 37
  • 53
Erica
  • 1
  • 1
  • Does this answer your question? [Python: pass arguments to a script](https://stackoverflow.com/questions/22846858/python-pass-arguments-to-a-script) – amsh Oct 09 '20 at 06:57

2 Answers2

0

The arguments required for the program are built by setup_args function in that program which in turn appears to be constructed by this line:

parser = cli.build_arg_parser()

This is in a package I don't have so I can't see what it is doing.

Nevertheless the help message, without explicitly stating which arguments are mandatory, is hinting in that general direction. I believe the arguments in [ ] are optional and everything else is required, so you need at least -s HOST and -u USER

python3 test2.py -s HOST -u USER

or

python3 test2.py --host HOST --user USER
Jonathan
  • 181
  • 7
  • my script is executed with : python3 test4.py --host xxx --port 80 --user xxx --password xxx, but i have this message : Traceback (most recent call last): File "test4.py", line 171, in main() File "test4.py", line 130, in main si = SmartConnect(host=args.host, user=args.user, TypeError: SmartConnect() got an unexpected keyword argument 'unverified' – Erica Oct 09 '20 at 07:19
  • You have changed to using SmartConnect from using SmartConnectNoSSL...'unverified' smells suspiciously of SSL related errors and not syntax errors relating the the execution of the script. – Jonathan Oct 11 '20 at 22:28
0

If you read message carefully it already tells how to use.

usage: test2.py [-h] -s HOST [-o PORT] -u USER [-p PASSWORD] [-S]

You need to pass all required arguments to run script successfully. Arguments given in [] means optional.

[-h] is to show the help message:

$ python get_vm_names.py -h
usage: get_vm_names.py [-h] -s HOST [-o PORT] -u USER [-p PASSWORD] [-nossl]

Arguments for talking to vCenter

options:
  -h, --help            show this help message and exit

standard arguments:
  -s HOST, --host HOST  vSphere service address to connect to
  -o PORT, --port PORT  Port to connect on
  -u USER, --user USER  User name to use when connecting to host
  -p PASSWORD, --password PASSWORD
                        Password to use when connecting to host
  -nossl, --disable-ssl-verification
                        Disable ssl host certificate verification

Look at the standard arguments in above message.

To run the script pass the arguments like this:

$ python get_vm_names.py -s <vSphere Server IP> -u <username> -p <Password>

or

$ python get_vm_names.py --host <vSphere Server IP> --user <username> --password <Password>

vSphere server IP, username and password are the same value which you use to connect to vSphere manually.

serenesat
  • 4,611
  • 10
  • 37
  • 53