再谈苹果脚本自动连接远程机器的方法

2022-11-22 10:48:35
标签: applescript remote expect properties focus

  之前做了一段时间的BRE,有很多连接远程机器的需求,因此曾经做过一些连接远程机器的苹果脚本,由于密码更新,以及Windows软件版本理新,所以需要重新更新一版。

  遇到的第一个问题是curl,原来用它来访问https://master-XX.jenkins.guessmycompany.com/computer/project_win_ecs_v3-2_rev1_6xl_29612d/ 类型的网站。以前用的方法是直接curl -u username:password。但是这次失败了,经过同事提醒,尝试curl -u username -p这样就可以了,但是每次需要输入密码。于是实现成expect脚本。并且https://master-XX.jenkins.guessmycompany.com/computer/project_win_ecs_v3-2_rev1_6xl_296bea/config.xml具有更方便的属性。它会有如下内容

project_win_ecs_v3-2_rev1_6xl_29612d

Swarm agent from 10.41.97.45

我们其实只用关心下面一行中的IP地址就可以了。

于是苹果脚本调同目录的SH脚本,SH脚本调同目录EXPECT脚本。

AppleScript

set AppleScriptPath to quoted form of POSIX path of ((path to me astext) & "::")

set curlCommand to AppleScriptPath & "ShowIP.sh " & computeLabel

这里包括了如何获得苹果脚本当前路径的逻辑。computeLabel就是我们需要传递的具体Node的label,通过它去Jenkins读取config.xml.

Shell脚本长下面这样

#!/bin/bash

`dirname $0`/ShowIP.exp $@ | grep description | rev | cut -d' ' -f1 | rev | sed 's/<.*//g'

这里同样包括了如何得到当前路径。

最后是Expect脚本

#!/usr/bin/expect

if {$argc != 1} { exit 0}

if {$argc == 1} {spawn curl -u username -p https://project-XX.jenkins.guessmycompany.com/computer/$argv/config.xml}

expect "host password" {send "pass\$word\r"}

expect eof

由于公司密码有要求,我有特殊字符$所以要用\$来转义。有其他密码照此修改。

接下来就是把这个得到的IP地址传递给后续的苹果脚本

set machineIpAddress to do shell scriptcurlCommand

这样可以得到上面的脚本输出,也就是IP地址。

然后接下来就是两个分支,分别操作苹果和Windows电脑。

操作Mac用的是自带ScreenSharing,而Windows则是Microsoft Remote Desktop

也正是因为微软的程序版本有变化,所以以前做的基本接近重做。目前我用的版本是Version 10.3.9 (1767)

苹果方面

on connect mac machine computeLabel

set computeLabel to "vnc://" & computeLabel

tell application "Screen Sharing"

activate

GetURL computeLabel

end tell

tell application "System Events"

tell application process "Screen Sharing"

repeat until window 1 exists

end repeat

tell text field 1 of window 1

    set the value to "username"

end tell

tell text field 2 of window 1

    set the value to "password"

end tell

tell checkbox 1 of window 1

    set isChecked to get the value

    if isChecked is equal to 0 then

        click

    end if

end tell

tell button 1 of window 1

    click

end tell

end tell

end tell

end connect mac machine

而Windows方面则复杂一些

on connect windows machine internal computeLabel with machineIpAddress

tell application "Microsoft Remote Desktop"

activate

end tell

tell application "System Events"

tell application process "Microsoft Remote Desktop"

tell text field 1 of group 1 of toolbar 1 of window 1

set value to computeLabel

tell button 1

click

end tell

end tell

delay 2

if not (exists list 1 of list 1 of scroll area 1 of window 1) then

#tell menu item "Add PC" of menu "Connections" of menu bar item "Connections" of menu bar 1

#click

#end tell

tell menu button 2 of group 1 of toolbar 1 of window 1

     click

     tell menu item "Add PC" of menu 1

      click

end tell

end tell

repeat until sheet 1 of window 1 exists

end repeat

tell sheet 1 of window 1

tell text field 1

set value to machineIpAddress

end tell

tell pop up button 1

click

clickmenu item "jenkins" of menu 1

end tell

tell text field 1 of tab group 1

key code 48 # TAB to activate, otherwise cannot save label

set value to computeLabel

end tell

tell button "Add"

click

end tell

end tell

delay 2

end if

tell text field 1 of group 1 of toolbar 1 of window 1

tell button 1

click

end tell

end tell

delay 2

tell scroll area 1 of window 1

set focused to true

key code 48 # TAB

key code 36 # Enter

end tell

#repeat until menu item "Connect" of menu "Connections" of menu bar item "Connections" of menu #bar 1 exists

#end repeat

#tell menu item "Connect" of menu "Connections" of menu bar item "Connections" of menu bar 1

#click

#end tell

repeat until button "Continue" of group 2 of sheet 1 of window computeLabel exists

end repeat

tell button "Continue" of group 2 of sheet 1 of window computeLabel

click

end tell

end tell

end tell

end connect windows machine internal

其中,最主要的是

set focused to true

可以将焦点设置在某个具体的控件上。

另:

tell application "System Events"

tell application process "Microsoft Remote Desktop"

tell text field 1 of group 1 of toolbar 1 of window 1

get properties

end tell

end tell

end tell

其中get properties可以是到形如下的输出,各种属性都能获得,也包括focused

{minimum value:missing value, orientation:missing value, position:{1023, 69}, class:text field, accessibility description:"Searches", role description:"search text field", focused:false, title:missing value, size:{169, 25}, help:"Start typing to filter items", entire contents:{}, enabled:true, maximum value:missing value, role:"AXTextField", value:"", subrole:"AXSearchField", selected:missing value, name:missing value, description:"Searches"}

 

 

 

 

 

 

 

阅读(0) 收藏(0) 转载(0) 举报/Report
相关阅读

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有