Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

login to guest example #22

Closed
sijis opened this issue Mar 25, 2014 · 8 comments
Closed

login to guest example #22

sijis opened this issue Mar 25, 2014 · 8 comments

Comments

@sijis
Copy link

sijis commented Mar 25, 2014

I'm looking for an example on how to use pyvmomi to login to the guest.

As i understand i should be possible if the guest has vmwaretools installed.

Thanks.

@dnaeon
Copy link

dnaeon commented Apr 3, 2014

Hi,

If you are referring to login to the guest by establishing a mouse-keyboard-screen connection then you could use the AcquireTicket() method of a VirtualMachine managed object.

By using the acquired ticket in combination with the VMware Remote Console (VMRC) SDK you should be able to get a mouse-keyboard-screen session.

I should also mention that I haven't tried that myself, but reading through the documentation you should be able to do so.

Hope that helps.

Regards,
Marin

@sijis
Copy link
Author

sijis commented Apr 3, 2014

I'm actually thinking in terms of being able to leverage the guest tools to manipulate files on the guestvm.

I've been looking at GuestOperationsManager.

@sijis
Copy link
Author

sijis commented Apr 4, 2014

This is some sample code i'm running though. I'm having a hard time getting the NamePasswordAuthentication passed to the method.

I'm just not sure how to create the auth object

si = SmartConnect(host=host, user=username, pwd=password, port=443)
sc = si.RetrieveServiceContent()
pm = sc.guestOperationsManager.processManager
pm.ListProcesses(vm) -->  TypeError: Required field "auth" not provided (not @optional)

@dnaeon
Copy link

dnaeon commented Apr 4, 2014

Hi,

I see now what you are trying to do. Here is an example which shows how to login to a VM and get all processes:

import pyVmomi
from pyVim.connect import SmartConnect                                                  
si = SmartConnect(user='root', pwd='password', host='vc01')

# Credentials used to login to the guest system
creds = pyVmomi.vim.vm.guest.NamePasswordAuthentication(username='guest_user', password='guest_password')

# Get a view ref to all VirtualMachines
view_ref = si.content.viewManager.CreateContainerView(container=si.content.rootFolder, type=[pyVmomi.vim.VirtualMachine], recursive=True)

# Pick up a random VM
vm = view_ref.view[0]
print "VM Name: %s" % vm.name

# Get VM processes 
processes = si.content.guestOperationsManager.processManager.ListProcessesInGuest(vm=vm, auth=creds)

# Print some process info
print "Process name: %s" % processes[0].name
print "Process owner: %s" % processes[0].owner
print "Process PID: %d" % processes[0].pid

When I run this script on my host here is the result:

$ python vm_processes.py
VM Name: mysql01.example.org
Process name: init [2]
Process owner: root
Process PID: 1

Hope that helped you.

Regards,
Marin

@dnaeon
Copy link

dnaeon commented Apr 4, 2014

Btw, I've just committed a new vm.process.get method to vPoller project which allows you to accomplish the task using vPoller. Here is an example of getting the processes in a VM using vPoller.

$ vpoller-client -m vm.process.get -V vc01.example.org -n vm01.example.org -U username -P password

And this command here will request additional process properties:

$ vpoller-client -m vm.process.get -V vc01.example.org -n vm01.example.org -U username -P password -p name,cmdLine,owner,pid

You can find vPoller project at the link below:

Regards,
Marin

@sijis
Copy link
Author

sijis commented Apr 4, 2014

That example made a breakthrough for me.

As my original goal was to execute something on the vm this is what's needed to add on your example.

# executes /bin/touch /tmp/hello.txt
cmdspec = pyVmomi.vim.vm.guest.ProcessManager.ProgramSpec(arguments='/tmp/hello.txt', programPath='/bin/touch')

# cmdoutput returns a pid
cmdoutput = si.content.guestOperationsManager.processManager.StartProgramInGuest(vm=vm, auth=creds, spec=cmdspec)

I will have to checkout vPoller.

Thank you!!

@ghost
Copy link

ghost commented Feb 4, 2016

Can I get an example for login to the remote vm console and run a command like pinging vcenter ip from the vm console ?
Thanks

@thinksabin
Copy link

"If you are referring to login to the guest by establishing a mouse-keyboard-screen connection then you could use the AcquireTicket() method of a VirtualMachine managed object."

Can this be done in the case where we have to trigger keyboard action while installing live cd in the virtualmachine? (selecting options before actually os installation begin)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants