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

时间:2013-12-17 07:42:16

标签: python ssh paramiko openssh

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

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

win32api.ChangeDisplaySettings(dm, 0)

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

import paramiko

ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(remote_win_host_ip, username= host_user, password=host_pswd)
ssh_stdin, ssh_stdout, ssh_stderr = ssh.exec_command('/cygdrive/e/test/change_screen_res.exe')

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

from win32api import GetSystemMetrics

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

但是,我发现远程主机的分辨率始终为1024*768

如何修改分辨率?

由于

2 个答案:

答案 0 :(得分:0)

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上运行。 另一个简短的解决方案是(但你需要公钥):

from subprocess import Popen, PIPE, STDOUT

x = Popen("ssh -t -t root@hostname.com 'echo \"done\" > /root/testing.txt'", shell=True, stdout=PIPE, stdin=PIPE, stderr=STDOUT)
while x.poll() == None:
    pass
x.stdout.close()
x.stdin.close()

答案 1 :(得分:0)

似乎Windows根本不支持此类操作。我尝试了许多不同的ssh客户端和屏幕分辨率修改工具,没有工作。

但是,感谢Jenkins slave代理并参考jenkins-on-windows-and-gui-tests-without-rdc,有一个解决方法。