像SSH一样使用pyVMomi连接设置DISPLAY =:10

问题描述

从myjenkinsserver.jen开始,我可以ssh进入ubunturunner1服务器并成功执行xeyes命令:

$ ssh kcason@ubunturunner1 xeyes -display :10

与预期的一样,这引起了对myjenkinsserver.jen的关注。如果我将显示更改为:

$ ssh kcason@ubunturunner1 xeyes -display :1
正如所料,

ubunturunner1引起了人们的注意。很好,因为它是通过ssh运行的,可以正确设置disPLAY变量。 (myjenkinsserver.jen是ESXi VM,但这对于ssh并不重要)

需要要做的是不使用ssh,而是使用pyvim和pyvmomi连接到ubunturunner1(VM)并执行命令,如pyvmomi发行版随附的示例程序所示:execute_program_in_vm .py我正在使用以下命令运行它:

python execute_program_in_vm.py \
    -s myesxiserver.esxi \
    -u root \
    -p <mypass> \
    -v "564d0866-beb6-7a6a-5fad-e6542733005f" \
    -r kcason \
    -w <mypass> \
    -l "/usr/bin/bash" \
    -f "xeyes"

我得到的只是ubunturunner1 syslog中的错误错误:无法打开显示,并且过程失败。 这是execute_program_in_vm.py代码

    from __future__ import with_statement
    import atexit
    from tools import cli
    from pyVim import connect
    from pyvmomi import vim,vmodl
    import pprint
    import re
    import time
    import ssl
    
    
    def get_args():
        """Get command line args from the user.
        """
    
        parser = cli.build_arg_parser()
    
        parser.add_argument('-v','--vm_uuid',required=False,action='store',help='Virtual machine uuid')
    
        parser.add_argument('-r','--vm_user',help='virtual machine user name')
    
        parser.add_argument('-w','--vm_pwd',help='virtual machine password')
    
        parser.add_argument('-l','--path_to_program',help='Path inside VM to the program')
    
        parser.add_argument('-f','--program_arguments',help='Program command line options')
    
        args = parser.parse_args()
    
        cli.prompt_for_password(args)
        return args
    
    
    def main():
        """
        Simple command-line program for executing a process in the VM without the
        network requirement to actually access it.
        """
    
        args = get_args()
        args.disable_ssl_verification = True  # for Now at least
        try:
            pprint.pprint(args)
            if args.disable_ssl_verification:
                service_instance = connect.SmartConnectNoSSL(host=args.host,user=args.user,pwd=args.password,port=int(args.port))
            else:
                s = ssl.SSLContext(ssl.PROTOCOL_TLSv1)
                s.verify_mode = ssl.CERT_NONE
                service_instance = connect.SmartConnect(host=args.host,sslContext=s,port=int(args.port))
    
            atexit.register(connect.disconnect,service_instance)
            content = service_instance.RetrieveContent()
    
            # if instanceUuid is false it will search for VM BIOS UUID instead
            vm = content.searchIndex.FindByUuid(datacenter=None,uuid=args.vm_uuid,vmSearch=True,instanceUuid=False)
    
            if not vm:
                raise SystemExit("Unable to locate the virtual machine.")
    
            tools_status = vm.guest.toolsstatus
            if (tools_status == 'toolsnotinstalled' or
                    tools_status == 'toolsNotRunning'):
                raise SystemExit(
                    "VMwaretools is either not running or not installed. "
                    "Rerun the script after verifying that VMwaretools "
                    "is running")
    
            creds = vim.vm.guest.NamePasswordAuthentication(
                username=args.vm_user,password=args.vm_pwd
            )
    
            try:
                pm = content.guestOperationsManager.processManager
    
    
                ps = vim.vm.guest.ProcessManager.ProgramSpec(
                    programPath=args.path_to_program,arguments=args.program_arguments
                )
                res = pm.StartProgramInGuest(vm,creds,ps)
    
                if res > 0:
                    print("Program submitted,PID is %d" % res)
                    #print ("Program submitted,PID is " + res)
                    pid_exitcode = pm.ListProcessesInGuest(vm,[res]).pop().exitCode
                    # If its not a numeric result code,it says None on submit
                    while (re.match('[^0-9]+',str(pid_exitcode))):
                        print ("Program running,PID is %d" % res)
                        time.sleep(5)
                        pid_exitcode = pm.ListProcessesInGuest(vm,[res]).pop().\
                            exitCode
                        if (pid_exitcode == 0):
                            print ("Program %d completed with success" % res)
                            break
                        # Look for non-zero code to fail
                        elif (re.match('[1-9]+',str(pid_exitcode))):
                            print ("ERROR: Program %d completed with Failure" % res)
                            #print ("  tip: Try running this on guest %r to debug" \
                            #    % summary.guest.ipAddress)
                            print ("ERROR: More info on process")
                            print (pm.ListProcessesInGuest(vm,[res]))
                            break
    
            except IOError as e:
                print (e)
        except vmodl.MethodFault as error:
            print ("Caught vmodl fault : " + error.msg)
            return -1
    
        return 0
    
    # Start program
    if __name__ == "__main__":
        main()

摘要:只要使用SSH,一切X11似乎都已正确设置以进行显示转发。如果我使用python模块,则不会设置disPLAY。

我可以更改上面的代码来将disPLAY设置为与ssh相似吗?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)