【发布时间】: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 异常。
是否有可能将密码传递给所需的类窗口结构的所需控件,以便作业不会中断其执行?
非常感谢您的帮助。
【问题讨论】:
-
使用pywinauto 和
backend="uia"。