如何使用pyVmomi从Vsphere集群中的服务器获取文件到远程(主机)pc
How to get a file from a server in a cluster in Vsphere using pyVmomi to remote(host) pc
我能够在服务器内执行命令,但我想在我的本地电脑上获得该输出。我不想使用 ssh 密钥。我只想使用主机的用户名和密码以及虚拟机(服务器)的用户名和密码。我找不到在我的电脑中获取服务器的 shell 输出的直接方法,但这种方法似乎完成了一半,比如在服务器中生成并保存输出,然后从服务器获取文件。我发现很难从服务器获取文件 (sample.txt) 到本地主机 (pc)。
[编辑:我可以使用 via via 方法来做到这一点(将输出存储到服务器,然后将其返回到本地 pc,是否有直接方法?)]
from pyVim import connect
from config import *
from pyVmomi import vim, vmodl
import ssl
service_instance = connect.SmartConnect(host="yyyyyyy", port=some_number,user="xxx" , pwd=pwd,sslContext=ssl._create_unverified_context())
content = service_instance.RetrieveContent()
vm = searcher.FindByIp(ip="zzzzzz", vmSearch=True)
creds = vim.vm.guest.NamePasswordAuthentication(username='root', password=vmpwd)
pm = service_instance.content.guestOperationsManager.processManager
#checks python version and stores in sample.txt in server
ps = vim.vm.guest.ProcessManager.ProgramSpec(programPath='/usr/bin/python', arguments='--version &> sample.txt')
res = pm.StartProgramInGuest(vm, creds, ps)
print(res) #Prints pid
这可以完成工作,但如果有人知道如何直接从服务器获取 shell 命令的输出到我的本地电脑,我将不胜感激。此代码在服务器内部创建一个包含 cmd 输出的文件,并将其复制到我的本地 pc
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 into file
with open(dest, 'wb') as f:
f.write(resp.content)
我能够在服务器内执行命令,但我想在我的本地电脑上获得该输出。我不想使用 ssh 密钥。我只想使用主机的用户名和密码以及虚拟机(服务器)的用户名和密码。我找不到在我的电脑中获取服务器的 shell 输出的直接方法,但这种方法似乎完成了一半,比如在服务器中生成并保存输出,然后从服务器获取文件。我发现很难从服务器获取文件 (sample.txt) 到本地主机 (pc)。
[编辑:我可以使用 via via 方法来做到这一点(将输出存储到服务器,然后将其返回到本地 pc,是否有直接方法?)]
from pyVim import connect
from config import *
from pyVmomi import vim, vmodl
import ssl
service_instance = connect.SmartConnect(host="yyyyyyy", port=some_number,user="xxx" , pwd=pwd,sslContext=ssl._create_unverified_context())
content = service_instance.RetrieveContent()
vm = searcher.FindByIp(ip="zzzzzz", vmSearch=True)
creds = vim.vm.guest.NamePasswordAuthentication(username='root', password=vmpwd)
pm = service_instance.content.guestOperationsManager.processManager
#checks python version and stores in sample.txt in server
ps = vim.vm.guest.ProcessManager.ProgramSpec(programPath='/usr/bin/python', arguments='--version &> sample.txt')
res = pm.StartProgramInGuest(vm, creds, ps)
print(res) #Prints pid
这可以完成工作,但如果有人知道如何直接从服务器获取 shell 命令的输出到我的本地电脑,我将不胜感激。此代码在服务器内部创建一个包含 cmd 输出的文件,并将其复制到我的本地 pc
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 into file
with open(dest, 'wb') as f:
f.write(resp.content)