通过SSH更改远程windows主机的屏幕分辨率

2024-04-25 15:05:29 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图通过SSH修改远程windows主机的屏幕分辨率。 首先,我使用python编写一个小脚本来更改本地桌面的分辨率。在

import win32api
dm = win32api.EnumDisplaySettings(None, 0)
dm.PelsHeight = 1024    
dm.PelsWidth = 1280

win32api.ChangeDisplaySettings(dm, 0)

然后,使用pyinstaller将其构建为独立的.exe文件,将输出的文件放到远程主机上,并通过SSH执行该工具。在

^{pr2}$

同时,我编写了一个脚本来显示当前的分辨率,并以同样的方式在远程主机上使用它。在

from win32api import GetSystemMetrics

print "width =", GetSystemMetrics (0)
print "height =",GetSystemMetrics (1)

然而,我发现远程主机的分辨率总是1024*768。在

如何修改分辨率?在

谢谢


Tags: 文件importnone远程屏幕windows分辨率dm
2条回答
from os import fork, waitpid, execv, read, write
import pty, sys

class ssh():
    def __init__(self, host, execute='echo "done" > /root/testing.txt', askpass=False, user='root', password='UberPassword'):
        self.exec = execute
        self.host = host
        self.user = user
        self.password = password
        self.askpass = askpass
        self.run()

    def run(self):
        command = [
                '/usr/bin/ssh',
                self.user+'@'+self.host,
                '-o', 'NumberOfPasswordPrompts=1',
                self.exec,
        ]

        pid, child_fd = pty.fork()

        if not pid: # Child process
            # Replace child process with our SSH process
            execv(command[0], command)

        ## if we havn't setup pub-key authentication
        ## we can loop for a password promt and "insert" the password.
        while self.askpass:
            try:
                output = read(child_fd, 1024).strip()
            except:
                break
            lower = output.lower()
            # Write the password
            if b'password:' in lower:
                write(child_fd, self.password + b'\n')
                break
            elif b'are you sure you want to continue connecting' in lower:
                # Adding key to known_hosts
                write(child_fd, b'yes\n')
            elif b'MOTD and Leagal warning' in lower:
                pass # This is an understood message
            else:
                print('Error:',output)

        waitpid(pid, 0)

多亏了pty,它只能在linux上运行。 另一个简短的解决方案是(但您需要公钥):

^{pr2}$

Windows似乎根本不支持这种操作。我尝试了许多不同的ssh客户机和屏幕分辨率修改工具,但都不起作用。在

然而,多亏了Jenkins slave agent并引用了jenkins-on-windows-and-gui-tests-without-rdc,因此有了一个解决方法。在

相关问题 更多 >