VMware Cloud Community
mozilla_firefox
Contributor
Contributor

Get the output of shell command on server to local pc.

This does the job but I would appreciate if someone knows how to directly get the output of shell command from server to my local pc. This code makes a file with the output of command inside the server and it gets copied into my local pc. Reading somewhere I felt powershell also works the same way i.e  Temporarily storing the shell commands on server and then getting that data back using InitiateFileTransferFromGuest and then eventually delete the file from server. Does anyone know how can I get output of shell command on server to my local pc without actually saving files on server ? I don't have ssh keys.

from pyVim import connect
from config import *
from pyVmomi import vim, vmodl
import ssl
import os
import requests


service_instance
= connect.SmartConnect(host="xxxx", port=aaa,user="yyy" , pwd=pwd,sslContext=ssl._create_unverified_context())
content
= service_instance.RetrieveContent()


# # Find a VM
vm
= searcher.FindByIp(ip="aaaa", vmSearch=True)
creds
= vim.vm.guest.NamePasswordAuthentication(username='root', password=vmpwd)
pm
= service_instance.content.guestOperationsManager.processManager

#executes and saves sample.txt into server
ps
= vim.vm.guest.ProcessManager.ProgramSpec(programPath='/usr/bin/python', arguments='--version &> sample.txt')
res
= pm.StartProgramInGuest(vm, creds, ps)

dest
="/Users/username/Desktop/vcenter/sample.txt" #My local pc

src
="/root/sample.txt" #Server's directory
fti
= content.guestOperationsManager.fileManager.InitiateFileTransferFromGuest(vm, creds, src)

resp
=requests.get(fti.url, verify=False)

#Writes file into local directory
with open(dest, 'wb') as f:
  f
.write(resp.content)

Tags (2)
Reply
0 Kudos
0 Replies