【vSphere | Python】vSphere Automation SDK for Python Ⅵ—— VM Guest Processes APIs
目录
- 12. VM APIs
- 12.1 VM Guest Processes APIs
- Processes 进程
- Operations 操作
- (1)List Guest Processes
- (2)Get Guest Processes
- (3)Create Guest Processes
- (4)Delete Guest Processes
- 参考资料
12. VM APIs
12.1 VM Guest Processes APIs
Processes 进程
进程服务(Processes service)提供了管理客户操作系统中的进程的操作。
脚本演示的操作系统是Centos 7。
Operations 操作
- List Guest Processes
- Get Guest Processes
- Create Guest Processes
- Delete Guest Processes
(1)List Guest Processes
关键方法:vm.guest.Processes.list(vm, credentials)
方法说明:列出来宾操作系统中运行的进程,以及最近完成的由 Processes.create 启动的进程。
参数说明:vm
:VM标识符;credentials
:来宾操作系统身份验证数据。
"credentials": {
"interactive_session": false, //
"password": "string", /
"saml_token": "string", //
"type": "USERNAME_PASSWORD", //
//
"user_name": "string"
}
interactive_session
:如果设置,该操作将与来宾中登录的桌面会话交互。 这要求登录用户与凭据指定的用户相匹配。 目前仅支持USERNAME_PASSWORD
。password
:该字段是可选的,只有当Credentials.type的值为USERNAME_PASSWORD
时,它才有意义。saml_token
:SAML Bearer Token,字段是可选的,只有当Credentials.type的值是SAML_BEARER_TOKEN的时候,它才有意义。type
:凭证的类型。目前支持两种认证类型:USERNAME_PASSWORD
和SAML_BEARER_TOKEN
。USERNAME_PASSWORD
:包含了使用用户名和密码在来宾操作系统内部进行认证的必要信息。这种认证方法是无状态的。要使用USERNAME_PASSWORD
时,需要填写登录操作系统的用户名和密码。SAML_BEARER_TOKEN
: 包含了使用SAML令牌在来宾操作系统中进行认证的必要信息。SAML Bearer Token凭证依赖于一个来宾别名,该别名将来宾帐户与从VMware SSO服务器获得的SAML Bearer Token中编码的主题和证书联系起来。
user_name
:该字段是可选的,对于SAML_BEARER_TOKEN
,这是要与证书关联的来宾用户。对于USERNAME_PASSWORD
,这是登录操作系统的用户名。如果没有为SAML_BEARER_TOKEN
指定用户,一个依赖来宾的映射将决定应用什么样的来宾用户账户。
方法返回值:
command
:完整的进程命令name
:进程的名称。owner
:进程所有者。pid
:进程的ID。started
:进程开始的时间。
脚本:
import time
from vSphere_Automation_SDK.Connect_to_vCenter_Server import vsphere_client
credentials = {
"interactive_session": False,
"password": "password",
# "saml_token": "string",
"type": "USERNAME_PASSWORD",
"user_name": "root"
}
vm = "vm-13247856"
start_time = time.time()
try:
b = vsphere_client.vcenter.vm.guest.Processes.list(vm, credentials)
for k,v in enumerate(b):
print(
"Name:".ljust(17),v.name,
"\nPID:".ljust(20),v.pid,
"\nOwner:".ljust(18),v.owner,
"\nCommand:".ljust(14),v.command,
"\nStarted:".ljust(20),v.started,
"\n--------------------------------------------"
)
except Exception as err:
for i in err.messages:
id = i.id,
default_message = i.default_message
args = i.args
params = i.params
localized = i.localized
print("\033[1;31m Encountered an error, Please see the following information \033[0m",
"\n\tError Class:", id,
"\n\tMessage:", default_message,
"\n\tArgs:", args,
"\n\tParams:", params,
"\n\tLocalized:", localized,
"\nError Data:", err.data,
"\nError Type:", err.error_type
)
end_time = time.time()
run_time = end_time - start_time
print("Used Time:".ljust(43), run_time)
脚本效果图(部分):
(2)Get Guest Processes
关键方法:vm.guest.Processes.get(vm, credentials,pid)
方法说明:返回在来宾操作系统中运行的进程的状态,包括最近可能已完成的由 Processes.create 启动的进程。
参数说明:vm
:VM标识符;credentials
:来宾操作系统身份验证数据;pid
:进程号
关于credentials
的具体信息请参阅 List Guest Processes 小节
方法返回值:
command
:完整的进程命令name
:进程的名称。owner
:进程所有者。Exit Code
:如果进程是用Processes.create启动的,那么如果在它完成后5分钟内查询,就可以得到进程的退出代码。started
:进程开始的时间。finished
:如果进程是用Processes.create启动的,那么如果在它完成后的5分钟内进行查询,将可以得到进程的完成时间。
脚本:
import time
from vSphere_Automation_SDK.Connect_to_vCenter_Server import vsphere_client
credentials = {
"interactive_session": False,
"password": "password",
# "saml_token": "string",
"type": "USERNAME_PASSWORD",
"user_name": "root"
}
vm = "vm-13247856"
pid = 1
start_time = time.time()
try:
b = vsphere_client.vcenter.vm.guest.Processes.get(vm, credentials,pid)
print( "Name:".ljust(17),b.name,
"\nOwner:".ljust(18),b.owner,
"\nCommand:".ljust(14),b.command,
"\nStarted:".ljust(19),b.started,
"\nFinished:".ljust(18),b.finished ,
"\nExit Code:".ljust(17),b.exit_code ,)
except Exception as err:
for i in err.messages:
id = i.id,
default_message = i.default_message
args = i.args
params = i.params
localized = i.localized
print("\033[1;31m Encountered an error, Please see the following information \033[0m",
"\n\tError Class:", id,
"\n\tMessage:", default_message,
"\n\tArgs:", args,
"\n\tParams:", params,
"\n\tLocalized:", localized,
"\nError Data:", err.data,
"\nError Type:", err.error_type
)
end_time = time.time()
run_time = end_time - start_time
print("Used Time:".ljust(43), run_time)
脚本效果图:
(3)Create Guest Processes
关键方法:vm.guest.Processes.create(vm, credentials,spec)
方法说明:在来宾操作系统中启动(创建)进程。以这种方式启动的进程可以使用 Processes.list 或 Processes.get 查询其状态。进程完成后,其退出代码和结束时间将在完成后 5 分钟内可用。
参数说明:vm
:VM标识符;credentials
:来宾操作系统身份验证数据;spec
:描述要启动的程序的参数。
关于credentials
的具体信息请参阅 List Guest Processes 小节
"spec": {
"path": "string"
}
-
path
:要启动的程序的绝对路径。对于Linux操作系统,/bin/bash被用来启动程序。对于Solaris客体操作系统,如果/bin/bash存在,就用它来启动程序,否则就用/bin/sh。如果使用/bin/sh,那么Processes.create返回的进程ID将是用于启动程序的shell的ID,而不是程序本身,因为/bin/sh和/bin/bash的工作方式不同。这个PID仍可用于用Processes.list观察该进程,以找到其退出代码和经过的时间。
对于Windows,不使用shell。如果在Processes.CreateSpec.arguments参数中传递,使用一个简单的批处理文件来代替c:\windows\system32\cmd.exe /c将允许stdio重定向工作。
方法返回值:进程号。
脚本:
我们先在Centos的/root目录下创建一个名为sleep.sh的脚本,该脚本内容是:sleep 100000
import time
from vSphere_Automation_SDK.Connect_to_vCenter_Server import vsphere_client
credentials = {
"interactive_session": False,
"password": "password",
# "saml_token": "string",
"type": "USERNAME_PASSWORD",
"user_name": "root"
}
vm = "vm-13247856"
spec= {
"path": "/root/sleep.sh"
}
start_time = time.time()
try:
b = vsphere_client.vcenter.vm.guest.Processes.create(vm, credentials,spec)
print(b)
except Exception as err:
for i in err.messages:
id = i.id,
default_message = i.default_message
args = i.args
params = i.params
localized = i.localized
print("\033[1;31m Encountered an error, Please see the following information \033[0m",
"\n\tError Class:", id,
"\n\tMessage:", default_message,
"\n\tArgs:", args,
"\n\tParams:", params,
"\n\tLocalized:", localized,
"\nError Data:", err.data,
"\nError Type:", err.error_type
)
end_time = time.time()
run_time = end_time - start_time
print("Used Time:".ljust(43), run_time)
脚本效果图:
get方法获取结果:
(4)Delete Guest Processes
关键方法:vm.guest.Processes.delete(vm, credentials, pid)
方法说明:终止来宾操作系统中的进程。 在 Posix 客户机上,进程会收到一个 TERM 信号。 如果这没有终止进程,则发送 KILL 信号。 如果进程卡住,它可能仍在运行。
参数说明:vm
:VM标识符;credentials
:来宾操作系统身份验证数据;pid
:进程号
关于credentials
的具体信息请参阅 List Guest Processes 小节
方法返回值:N/A
脚本:
import time
from vSphere_Automation_SDK.Connect_to_vCenter_Server import vsphere_client
credentials = {
"interactive_session": False,
"password": "password",
# "saml_token": "string",
"type": "USERNAME_PASSWORD",
"user_name": "root"
}
vm = "vm-13247856"
pid = 2510
start_time = time.time()
try:
b = vsphere_client.vcenter.vm.guest.Processes.delete(vm, credentials, pid)
print("%s has been deleted"%pid)
except Exception as err:
for i in err.messages:
id = i.id,
default_message = i.default_message
args = i.args
params = i.params
localized = i.localized
print("\033[1;31m Encountered an error, Please see the following information \033[0m",
"\n\tError Class:", id,
"\n\tMessage:", default_message,
"\n\tArgs:", args,
"\n\tParams:", params,
"\n\tLocalized:", localized,
"\nError Data:", err.data,
"\nError Type:", err.error_type
)
end_time = time.time()
run_time = end_time - start_time
print("Used Time:".ljust(43), run_time)
脚本效果:
我们删除create方法创建的进程,同时删除后再使用get方法查看。
5分钟内查看该进程:
5分钟后查看:
参考资料
vCenter REST APIs v7.0U3
vSphere-Python-Automation-Scripts/v1/
关于本专栏其它博文,请关注专栏,会有更多关于vSphere Python自动化的内容:vSphere python自动化