Execute python script with parameter

452 views Asked by At

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.

2

There are 2 answers

2
Jonathan On

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

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.