如何使用 pyVmomi 从 Vsphere 集群中的服务器获取文件到远程(主机)PC [英] How to get a file from a server in a cluster in Vsphere using pyVmomi to remote(host) pc

查看:30
本文介绍了如何使用 pyVmomi 从 Vsphere 集群中的服务器获取文件到远程(主机)PC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我能够在服务器内执行命令,但我想在我的本地电脑中获得该输出.我不想使用 ssh 密钥.我只想使用主机的用户名和密码以及 vm(服务器的)用户名和密码.我找不到在我的电脑中获取服务器的 shell 输出的直接方法,但是这种方法似乎完成了一半,就像在服务器中生成并保存输出,然后从服务器获取文件一样.我发现很难从服务器获取文件(sample.txt)到本地主机(pc).

I am able to execute a command inside the server but I want to get that output in my local pc. I don't want want to use ssh keys. I want to use just the host's username and password as well as vm's(server's) username and password. I couldn't find a direct method to get the shell output of server in my pc, but this method seems half done like generate and save the output in server and then get the file from server. I am finding it difficult to get file(sample.txt) from server to local host(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 输出的文件,并将其复制到我的本地电脑中

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 cmd inside the server and it gets copied into my local 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)

这篇关于如何使用 pyVmomi 从 Vsphere 集群中的服务器获取文件到远程(主机)PC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆