login to guest example #22

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

Comments

Projects
None yet
4 participants
@sijis

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

This comment has been minimized.

Show comment
Hide comment
@dnaeon

dnaeon Apr 3, 2014

Member

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

Member

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

This comment has been minimized.

Show comment
Hide comment
@sijis

sijis 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 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

This comment has been minimized.

Show comment
Hide comment
@sijis

sijis 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)

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

This comment has been minimized.

Show comment
Hide comment
@dnaeon

dnaeon Apr 4, 2014

Member

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

Member

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

This comment has been minimized.

Show comment
Hide comment
@dnaeon

dnaeon Apr 4, 2014

Member

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

Member

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

This comment has been minimized.

Show comment
Hide comment
@sijis

sijis 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!!

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!!

@hartsock hartsock added sample request and removed question labels May 30, 2014

@hartsock hartsock referenced this issue in vmware/pyvmomi-community-samples May 30, 2014

Open

Sample: login to guest example #44

@hartsock hartsock closed this May 30, 2014

@ghost

This comment has been minimized.

Show comment
Hide comment
@ghost

ghost 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

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

This comment has been minimized.

Show comment
Hide comment
@thinksabin

thinksabin Feb 21, 2017

"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)

"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