AppleScript |将 PPTP VPN 设置部署到 MAC 客户端

发布于 2024-10-04 07:41:37 字数 874 浏览 4 评论 0原文

需要一个 applescript(我以前没有使用过)来在我们的远程工作人员的 mac 盒子上部署我们的 VPN 连接设置。这是用于 PPTP VPN 的。

通过谷歌搜索,我发现了以下链接:
http://discussions.apple.com/thread.jspa?messageID=10368307

但我还是不能:
a) 似乎为 VPN 连接命名
b) 给它一个 pptp vpn 服务器地址
c) 提供 DNS、默认网关和任何自定义路由以使该 VPN 正常工作

我如何添加这些字段/输入?

告诉应用程序“系统偏好设置”
显示窗格“网络”
激活
告诉应用程序“系统事件”
告诉进程“系统偏好设置”
告诉窗口 1
单击按钮“添加服务”
告诉表 1
单击弹出按钮 1
点击弹出按钮1的菜单1的菜单项“VPN”
延迟1
点击弹出按钮2
点击弹出按钮2的菜单1的菜单项“PPTP”
单击按钮“创建”
结束告诉
单击按钮“应用”
结束告诉
结束告诉
延迟 1 — 可选(仅用于视觉反馈)
使用 {command down} 击键“w”
结束告诉
结束告诉

似乎可以很好地创建连接,但没有详细信息 - 我不确定我将使用什么语法来实现此目的。

感谢您的投入!
问候
谢谢

Need an applescript (which I haven't used before) to deploy our VPN connection settings on our remote workers mac boxes. this is for PPTP VPN.

Upon googling I came across the following link :
http://discussions.apple.com/thread.jspa?messageID=10368307

But still I can't :
a) seem to name the VPN connection
b) give it a pptp vpn server address
c) give dns, default gateway and any custom routes for this vpn to work
.

How can I add those fields / enteries in ?

tell application “System Preferences”
reveal pane “Network”
activate
tell application “System Events”
tell process “System Preferences”
tell window 1
click button “Add Service”
tell sheet 1
click pop up button 1
click menu item “VPN” of menu 1 of pop up button 1
delay 1
click pop up button 2
click menu item “PPTP” of menu 1 of pop up button 2
click button “Create”
end tell
click button “Apply”
end tell
end tell
delay 1 — optional (just for visual feedback)
keystroke “w” using {command down}
end tell
end tell

The above script seems to create the connection just fine but without the details - and I am not sure about what syntax I would use to get this going.

Thanks for your input!
Regards
Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

别再吹冷风 2024-10-11 07:41:38

Rihatum,这是完整的 Applescript,其中包含您想做的所有事情。

--create new VPN PPTP service on the ethernet interface
do shell script "networksetup -createnetworkservice \"VPN (PPTP)\" en0 - where en0" with administrator privileges

--set the IP, subnet, & router IP ( order = ip subnet route )
do shell script "networksetup -setmanual \"VPN (PPTP)\" 192.168.2.50 255.255.255.0 192.168.2.1" with administrator privileges

--set VPN service using DHCP  -- if using DHCP don't use manual setting above
do shell script "networksetup -setdhcp \"VPN (PPTP)\"" with administrator privileges

--set DNS
do shell script "networksetup -setdnsservers \"VPN (PPTP)\" 208.67.222.222" with administrator privileges  

--set search domain 
do shell script "networksetup -setsearchdomains \"VPN (PPTP)\" my_company_domain.com" with administrator privileges

--rename network service -- replace CISCO_VPN with your preferred name
do shell script "networksetup -renamenetworkservice \"VPN (PPTP)\" CISCO_VPN" with administrator privileges

Rihatum, here's the full Applescript with all the things you want to do.

--create new VPN PPTP service on the ethernet interface
do shell script "networksetup -createnetworkservice \"VPN (PPTP)\" en0 - where en0" with administrator privileges

--set the IP, subnet, & router IP ( order = ip subnet route )
do shell script "networksetup -setmanual \"VPN (PPTP)\" 192.168.2.50 255.255.255.0 192.168.2.1" with administrator privileges

--set VPN service using DHCP  -- if using DHCP don't use manual setting above
do shell script "networksetup -setdhcp \"VPN (PPTP)\"" with administrator privileges

--set DNS
do shell script "networksetup -setdnsservers \"VPN (PPTP)\" 208.67.222.222" with administrator privileges  

--set search domain 
do shell script "networksetup -setsearchdomains \"VPN (PPTP)\" my_company_domain.com" with administrator privileges

--rename network service -- replace CISCO_VPN with your preferred name
do shell script "networksetup -renamenetworkservice \"VPN (PPTP)\" CISCO_VPN" with administrator privileges
百善笑为先 2024-10-11 07:41:38

古老的答案

我尝试使用网络设置工具解决方案,但它对我不起作用:SI 实际上使用 UI 自动化想出了一个成功的脚本。它在这里发布了一篇关于它的文章: https://web.archive.org/web/20160731014105/http://blog.affirmix.com/2011/01/ 12/how-to-configure-a-vpn-in-mac-os-x-usingapplescript/

复制/粘贴答案

免责声明:这已经有 12 多年的历史了,是为 Mac OS 编写的X 版本以猫命名。里程可能会有所不同,请自行承担使用风险,如果不进行修改,它可能无法工作,yada yada...希望它为更好的答案提供基础:)

  1. 打开Finder,导航到应用程序,然后导航到实用程序,然后打开 AppleScript 编辑器。
  2. 选择本文末尾的代码,然后按 ⌘(Command)+ C。这会将代码复制到剪贴板中。
  3. 返回 AppleScript 编辑器,然后按 ⌘ (Command) + V。这会将剪贴板中的代码粘贴到编辑器中。
  4. 第一行代码定义将在本地计算机上定义的服务器名称。将服务器名称替换为您希望在客户端计算机上显示的网络服务的名称。
  5. 第二行代码定义服务器地址。将 127.0.0.1 替换为 VPN 服务器的 URL 或 IP 地址。
  6. 第三行定义 VPN 的共享密钥。将 s3cr3t 替换为您 VPN 的共享密钥。
  7. 转到第32行,删除“单击弹出按钮2”之前的两个破折号
    • 对于 L2TP over IPSec 连接:转到第 33 行,删除“单击弹出按钮 2 的菜单 1 的菜单项“L2TP over IPSEC”之前的两个破折号”
    • 对于 PPTP 连接:转到第 34 行并删除“单击弹出按钮 2 的菜单 1 的菜单项“PPTP””之前的两个破折号
    • 对于 Cisco IPSec 连接:转到第 35 行并删除“单击弹出按钮 2 的菜单 1 的菜单项“Cisco IPSec””之前的两个破折号 [有关配置 Cisco IPSec VPN 的信息,请参阅下面 Sergio 的评论]< /里>
  8. 转至脚本菜单,然后选择运行菜单项
set vpnname to "Server Name"
set vpnserver to "127.0.0.1"
set vpnsecret to "s3cr3t"

tell application "System Events"
    if not (UI elements enabled) then
        tell application "System Preferences"
            activate
            set current pane to pane id "com.apple.preference.universalaccess"
            display dialog "This script requires access for assistive devices be enabled." & return & return & "To continue, click the OK button and enter an administrative password in the security dialog." with icon note
        end tell
        set UI elements enabled to true
        if UI elements enabled is false then return "User Cancelled"
        delay 1
    end if
    tell application "System Preferences"
        activate
        set current pane to pane id "com.apple.preference.network"
        display dialog "This script will now create the Stracor VPN connection." & return & return & "Please wait as the operation is in progress, and do not click anything. This should not take longer than 30 seconds. You will be notified when the process is complete." with icon caution
        set vpnusername to display dialog "Please enter your username:" default answer "" with icon note
        set vpnpassword to display dialog "Please enter your password:" default answer "" with icon note with hidden answer
    end tell
    tell application "System Events"
        tell process "System Preferences"
            tell window 1
                click button 5
                delay 1
            end tell
            tell sheet 1 of window 1
                click pop up button 1
                click menu item "VPN" of menu 1 of pop up button 1
                --click pop up button 2
                --click menu item "L2TP over IPSEC" of menu 1 of pop up button 2
                --click menu item "PPTP" of menu 1 of pop up button 2
                --click menu item "Cisco IPSec" of menu 1 of pop up button 2
                set focused of text field 1 to true
                keystroke "a" using command down
                keystroke vpnname
                click button 1
                delay 1
            end tell
            tell group 1 of window 1
                click checkbox 1
                set focused of text field 1 to true
                keystroke vpnserver
                keystroke tab
                keystroke text returned of vpnusername
                click button 2
                delay 1
            end tell
            tell sheet 1 of window 1
                set focused of text field 3 to true
                keystroke text returned of vpnpassword
                keystroke tab
                keystroke vpnsecret
                click button 2
                delay 1
            end tell
            tell window 1
                click button 1
                delay 1
            end tell
            tell sheet 1 of window 1
                click button 1
            end tell
        end tell
    end tell
    tell application "System Preferences"
        quit saving yes
    end tell
    display alert "The VPN has been configured. Click on the \"Connect\" button to access the network." & return & return & "The connection status and various options are available through the menu bar at the top of the screen." as informational
end tell

如果您要设置 Cisco IPSec VPN,则以下是“Sergio 的评论”供参考:

安德鲁,

我还尝试配置 Cisco IPSec VPN 连接,这些是我为使其正常工作所做的调整。 (10.6.6/10.6.7) 我必须添加变量“vpngroup”来添加我们在配置中使用的组名称。希望这会有所帮助。

tell sheet 1 of window 1
    click pop up button 1
    click menu item “vpn” of menu 1 of pop up button 1
    delay 1
    repeat until exists pop up button 2
      delay 0.2
    end repeat
    click pop up button 2
    delay 0.5
    --click menu item “l2tp over ipsec” of menu 1 of pop up button 2
    --click menu item “pptp” of menu 1 of pop up button 2
    click menu item “cisco ipsec” of menu 1 of pop up button 2
    delay 1
    set focused of text field 1 to true
    keystroke “a” using command down
    keystroke vpnname
    click button 1
    delay 1
end tell

delay 1

tell group 1 of window 1
    set focused of text field 3 to true
    keystroke vpnserver
    keystroke tab
    keystroke text returned of vpnusername
    keystroke tab
    keystroke text returned of vpnpassword
    click checkbox 1
    click button 2
    delay 1
end tell

tell sheet 1 of window 1
    delay 0.2
    set focused of text field 1 to true
    keystroke vpngroup
    keystroke tab
    keystroke vpnsecret
    click button 2
    delay 1
end tell

Ancient answer

I tried the solution using the networksetup tool, but it didn't work for me :S I actually came up with a successful script using UI automation. It put up a post about it here: https://web.archive.org/web/20160731014105/http://blog.affirmix.com/2011/01/12/how-to-configure-a-vpn-in-mac-os-x-usingapplescript/

Copy/pasted answer

Disclaimer: This is now 12+ years old, and was writen for a Mac OS X version named after a cat. Milage may vary, use at your own risk, it probably won't work without modifications, yada yada... Hopefully it provides a foundation for a better answer :)

  1. Open Finder, navigate to Applications, then Utilities, and open AppleScript Editor.
  2. Select the code at the end of this article, and press ⌘ (Command) + C. This copies the code into the clipboard.
  3. Return to AppleScript Editor, and press ⌘ (Command) + V. This pastes the code from the clipboard into the editor.
  4. The first line of code defines the server name that will be defined on the local computer. Replace Server Name with the name of the network service that you would like to appear on the client computer.
  5. The second line of code defines the server address. Replace 127.0.0.1 with the URL or IP address of the VPN server.
  6. The third line defines the shared secret for the VPN. Replace s3cr3t with the shared secret of you VPN.
  7. Go to line 32, and remove the two dashes before “click pop up button 2”
    • For an L2TP over IPSec connection: Go to line 33 and remove the two dashes before ‘click menu item “L2TP over IPSEC” of menu 1 of pop up button 2’
    • For a PPTP connection: Go to line 34 and remove the two dashes before ‘click menu item “PPTP” of menu 1 of pop up button 2’
    • For a Cisco IPSec connection: Go to line 35 and remove the two dashes before ‘click menu item “Cisco IPSec” of menu 1 of pop up button 2’ [See Sergio’s comment below for information on configuring Cisco IPSec VPNs]
  8. Go to Script menu, and select the Run menu item
set vpnname to "Server Name"
set vpnserver to "127.0.0.1"
set vpnsecret to "s3cr3t"

tell application "System Events"
    if not (UI elements enabled) then
        tell application "System Preferences"
            activate
            set current pane to pane id "com.apple.preference.universalaccess"
            display dialog "This script requires access for assistive devices be enabled." & return & return & "To continue, click the OK button and enter an administrative password in the security dialog." with icon note
        end tell
        set UI elements enabled to true
        if UI elements enabled is false then return "User Cancelled"
        delay 1
    end if
    tell application "System Preferences"
        activate
        set current pane to pane id "com.apple.preference.network"
        display dialog "This script will now create the Stracor VPN connection." & return & return & "Please wait as the operation is in progress, and do not click anything. This should not take longer than 30 seconds. You will be notified when the process is complete." with icon caution
        set vpnusername to display dialog "Please enter your username:" default answer "" with icon note
        set vpnpassword to display dialog "Please enter your password:" default answer "" with icon note with hidden answer
    end tell
    tell application "System Events"
        tell process "System Preferences"
            tell window 1
                click button 5
                delay 1
            end tell
            tell sheet 1 of window 1
                click pop up button 1
                click menu item "VPN" of menu 1 of pop up button 1
                --click pop up button 2
                --click menu item "L2TP over IPSEC" of menu 1 of pop up button 2
                --click menu item "PPTP" of menu 1 of pop up button 2
                --click menu item "Cisco IPSec" of menu 1 of pop up button 2
                set focused of text field 1 to true
                keystroke "a" using command down
                keystroke vpnname
                click button 1
                delay 1
            end tell
            tell group 1 of window 1
                click checkbox 1
                set focused of text field 1 to true
                keystroke vpnserver
                keystroke tab
                keystroke text returned of vpnusername
                click button 2
                delay 1
            end tell
            tell sheet 1 of window 1
                set focused of text field 3 to true
                keystroke text returned of vpnpassword
                keystroke tab
                keystroke vpnsecret
                click button 2
                delay 1
            end tell
            tell window 1
                click button 1
                delay 1
            end tell
            tell sheet 1 of window 1
                click button 1
            end tell
        end tell
    end tell
    tell application "System Preferences"
        quit saving yes
    end tell
    display alert "The VPN has been configured. Click on the \"Connect\" button to access the network." & return & return & "The connection status and various options are available through the menu bar at the top of the screen." as informational
end tell

If you are setting up a Cisco IPSec VPN, then here is "Sergio's comment" for reference:

Andrew,

I also was trying to configure a Cisco IPSec VPN connection and these are the tweaks I made to get it to work. (10.6.6/10.6.7) I had to add the variable “vpngroup” to add the group name we use in our configuration. Hope this helps.

tell sheet 1 of window 1
    click pop up button 1
    click menu item “vpn” of menu 1 of pop up button 1
    delay 1
    repeat until exists pop up button 2
      delay 0.2
    end repeat
    click pop up button 2
    delay 0.5
    --click menu item “l2tp over ipsec” of menu 1 of pop up button 2
    --click menu item “pptp” of menu 1 of pop up button 2
    click menu item “cisco ipsec” of menu 1 of pop up button 2
    delay 1
    set focused of text field 1 to true
    keystroke “a” using command down
    keystroke vpnname
    click button 1
    delay 1
end tell

delay 1

tell group 1 of window 1
    set focused of text field 3 to true
    keystroke vpnserver
    keystroke tab
    keystroke text returned of vpnusername
    keystroke tab
    keystroke text returned of vpnpassword
    click checkbox 1
    click button 2
    delay 1
end tell

tell sheet 1 of window 1
    delay 0.2
    set focused of text field 1 to true
    keystroke vpngroup
    keystroke tab
    keystroke vpnsecret
    click button 2
    delay 1
end tell
小嗷兮 2024-10-11 07:41:38

我认为你想要做的是使用networksetup命令构建一个脚本
并在 AppleScript 中使用一系列“do shell script”行,而不是尝试使用 GUI 脚本进行配置。

例如,在 AppleScript 中,此命令将在以太网接口上创建新的 VPN PPTP 连接

do shell script "networksetup -createnetworkservice \"VPN (PPTP)\" en0 -where en0" with administrator privileges

:您将使用以下路由器 IP:
(-setmanual networkservice ip 子网路由器)

do shell script "networksetup -setmanual \"VPN (PPTP)\" 192.168.2.50 255.255.255.0 192.168.2.1" with administrator privileges

之后,您可以使用其他选项来配置其余部分。例如,
要将 DNS IP 添加到新创建的 VPN PPTP 连接,您可以将其添加到 AppleScript:

do shell script "networksetup -setdnsservers \"VPN (PPTP)\" 208.67.222.222" with administrator privileges

查看网络设置命令的手册页。它可以做很多事情。另外,您可以在 Google 上搜索该命令,您会发现很多论坛,人们在讨论 Mac 命令行网络配置。单个关键字 networksetup 效果不太好,因为它返回很多 Windows 页面,因此 Google 搜索“sudo networksetup”,这将返回 Mac 相关页面。

I think what you want to do is build a script using the networksetup command
and use a series of "do shell script" lines in your AppleScript instead of trying to GUI script the configuration.

Fo example, in your AppleScript this command will create a new VPN PPTP connection on the ethernet interface:

do shell script "networksetup -createnetworkservice \"VPN (PPTP)\" en0 -where en0" with administrator privileges

To set the IP, subnet, & router IPs you would use this:
( -setmanual networkservice ip subnet router )

do shell script "networksetup -setmanual \"VPN (PPTP)\" 192.168.2.50 255.255.255.0 192.168.2.1" with administrator privileges

After that you can use the other options to configure the rest. For example,
to add a DNS IP to your newly created VPN PPTP connection you would add this to your AppleScript:

do shell script "networksetup -setdnsservers \"VPN (PPTP)\" 208.67.222.222" with administrator privileges

Check out the man page for the networksetup command. It does a tremendous amount of things. Also, you can Google that command and you will find a lot of forums with people discussing Mac command line network configurations. The single keyword networksetup doesn't work too well because it returns a lot of Windows pages so Google for "sudo networksetup" and that will return Mac related pages.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文