【问题标题】:A trouble with automation of Windows RDP through console通过控制台自动化 Windows RDP 的问题
【发布时间】:2019-05-14 11:18:44
【问题描述】:

我的问题的简要描述:

1。 我的 Jenkins 工作需要建立与另一台机器的 RDP 连接以执行某些活动。

2。 直到最近,会话之间还保留了默认密码。但是现在一些设置已经改变,每次创建新的 RDP 会话时都需要手动重新输入密码。

我准备了一个简短的 python 脚本,通过 win32gui 包与 Windows gui 交互。

我使用 pyinstaller 从这个脚本构建了一个独立的可执行文件。 最后,我将对该可执行文件的调用直接添加到作业中。

类似的东西:

while attempts:
    security_window_title = "Windows Security"
    try:
        hwnd_credentials = win32gui.FindWindow(0, security_window_title)

        window_controls = []
        win32gui.EnumChildWindows(hwnd_credentials, collect_window_control, None)

        focus_on_window(hwnd_credentials)
        sleep(0.5)
        prev_user_login = window_controls[2]["hwnd"]

        x = int(window_controls[1]["x"] + 80)
        y = int(window_controls[1]["y"] + 20)

        click(x, y)
        type_message(password)

        ok_button = window_controls[6]["hwnd"]
        push_button(ok_button)

    except win32gui.error:
        sleep(1)
        attempts -= 1
        if not attempts:
            raise RuntimeError("Can't interact with window: {}.".format(security_window_title))
    else:
        break

while attempts:
    sleep(timeout)
    attempts -= 1
    if check_connection_started():
        break

    if check_certificate_errors():
        for control in window_controls[::-1]:
            if control["text"] == "&Yes":
                push_button(control["hwnd"])

    if not attempts:
        raise RuntimeError("Connection not established.")

3。 当脚本从使用功能齐全的 Windows ui 的作业中运行时,这不会成为问题。我可以找到一个窗口,我的脚本应该在其中使用 win32gui python 包指定密码。我可以生成所有适当的键盘事件来输入密码。

通过控制台使用 RDP 为我提供了一组非常奇怪的类 windows 对象,我无法像使用普通 windows 一样使用 win32gui python 包与之交互。例如,我确实找到了一个 hwnd 非零且文本属性等于“远程桌面连接”的窗口。但是我不能使用基本方法win32gui.SetForegroundWindow(hwnd)来关注这样的窗口。这会导致一个未命名的 win32gui 异常。

是否有可能将密码传递给所需的类窗口结构的所需控件,以便作业不会中断其执行?

非常感谢您的帮助。

【问题讨论】:

标签: python winapi rdp


【解决方案1】:

我可以通过win32gui.SetForegroundWindow(hwnd) 专注于“远程桌面连接”和“Windows 安全”。 示例代码:

import win32api
import win32gui
import win32con
import time
from pynput.keyboard import Key, Controller

def main():
    Remote = "Remote Desktop Connection"
    Security = "Windows Security"
    try:
        hwnd_Remote = win32gui.FindWindow(0, Remote)
        print(hwnd_Remote)
        win32gui.ShowWindow(hwnd_Remote,win32con.SW_SHOWNORMAL)
        win32gui.SetForegroundWindow(hwnd_Remote)
        keyboard = Controller()
        keyboard.type('ipaddress')
        keyboard.press(Key.enter)
        keyboard.release(Key.enter)
        time.sleep(3)
        hwnd_Security = win32gui.FindWindow(0, Security)
        print(hwnd_Security)
        win32gui.ShowWindow(hwnd_Security,win32con.SW_SHOWNORMAL)
        win32gui.SetForegroundWindow(hwnd_Security)
        keyboard.type('password')
        keyboard.press(Key.enter)
        keyboard.release(Key.enter)
    except win32gui.error:
        raise RuntimeError("Can't interact with window: {}.".format(Remote))

if __name__ == "__main__":
    main()

确保前台进程没有禁用对SetForegroundWindow 函数的调用。添加LockSetForegroundWindow(LSFW_UNLOCK)AllowSetForegroundWindow(ASFW_ANY),开启SetForegroundWindow的调用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-07-14
    • 1970-01-01
    • 1970-01-01
    • 2019-11-18
    • 2013-04-25
    • 2010-11-27
    • 1970-01-01
    相关资源
    最近更新 更多