Python 更改远程windows主机';通过SSH的屏幕分辨率
我正在尝试通过SSH修改远程windows主机的屏幕分辨率。 首先,我使用python编写了一个小脚本来更改本地桌面的分辨率Python 更改远程windows主机';通过SSH的屏幕分辨率,python,ssh,paramiko,openssh,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文件,将输出的文件放到远程主机
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
如何修改分辨率
谢谢
由于pty,只能在linux tho上工作。
另一个简短的解决方案是(但您需要公钥):
Windows似乎根本不支持这种操作。我尝试了许多不同的ssh客户端和屏幕分辨率修改工具,但都没有成功
但是,感谢Jenkins slave agent和参考,它有一个解决方法。在远程主机上手动运行可执行文件是否会改变屏幕分辨率?否,使用paramiko或plink通过SSH远程执行文件。您的SSH代码是什么样子的?(SSH不提供任何更改分辨率的工具,也不“存在”windows)。您试图运行什么命令来更改远程主机上的分辨率?@SUT:我的意思是,可执行文件实际工作吗?它是否在没有安装Python的计算机上运行?我记得Py2EXE或PyInstaller对.NET framework有一定的依赖性。是的,我尝试直接在远程主机上执行这些工具,它们工作正常。
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)
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()