如何在atlasvpn的shell脚本中使expect命令更快?

问题描述 投票:0回答:1

有没有办法优化expect命令以使其更快?这是我当前的代码,运行良好,但需要大约 25-30 秒才能完成,这与连接到 VPN 需要多长时间无关,如果我手动执行,VPN 连接过程大约需要 3-5 秒,看来缓慢与expect命令更相关:

#!/usr/bin/expect -f

spawn atlasvpn connect
expect "*Enter a number corresponding to the desired category:*" 
send "1\r"
expect "*Enter a number corresponding to the desired server:*" 
send "1\r"
interact

有什么我可以改进的地方吗?

此屏幕截图位于第一个

send "1\r"
之前,选择这些选项中的任何一个都需要 1 秒才能显示下一个屏幕截图(服务器选择)。

此屏幕截图是在发送第一个

send "1\r"
之后。选择这些选项中的任何一个大约需要 3-5 秒才能连接到 VPN。

expect -d script.sh
输出以下内容,似乎正在根据我的预期匹配验证输出中的每个结果:

expect: does "1 - Standard\r\n2 - Streaming\r\n3 - SafeSwap\r\n4 - MultiHop\r\n\u001b[?25l\u001b[2K\r\u001b[1m\u001b[31m\u2717\u001b[0m \u001b[1mEnter a number corresponding to the desired category\u001b[0m\u001b[1m:\u001b[0m \u2588\r\n\u001b[J\u001b[2K\r \u0008\u001b[J\u001b[2K\r1\u001b[J\u001b[2K\r\u001b[1A\u001b[2K\r\u001b[2K\r\u001b[1m\u001b[32m\u2714\u001b[0m \u001b[1mEnter a number corresponding to the desired category\u001b[0m\u001b[1m:\u001b[0m 1\u2588\r\n1\u001b[J\u001b[2K\r \u0008\u001b[J\u001b[2K\r \u0008\u001b[J\u001b[2K\r\u001b[1A\u001b[2K\r\u001b[2K\r\u001b[1m\u001b[32m\u2714\u001b[0m \u001b[1mEnter a number corresponding to the desired category\u001b[0m\u001b[1m:\u001b[0m 1\u2588\r\n \u0008\u001b[J\u001b[2K\r\u001b[1A\u001b[2K\r\u001b[2K\r\u001b[2mEnter a number corresponding to the desired category\u001b[0m\u001b[2m:\u001b[0m 1\r\n \u0008\u001b[J\u001b[2K\r\u001b[?25h \u0008\u001b[J\u001b[2K\r1 - Netherlands - Amsterdam\r\n2 - United States - New York\r\n3 - Hong Kong - Hong Kong\r\n4 - United States - Dallas\r\n5 - United States - Los Angeles\r\n6 - Singapore - Singapore\r\n7 - Finland - Helsinki\r\n8 - Norway - Oslo\r\n9 - New Zealand - Auckland\r\n10 - Argentina - Buenos Aires\r\n11 - Hungary - Budapest\r\n12 - Denmark - Copenhagen\r\n13 - Germany - Frankfurt\r\n14 - Bulgaria - Sofia\r\n15 - Sweden - Stockholm\r\n16 - United States - New Jersey\r\n17 - Netherlands - Amsterdam\r\n18 - UAE - Dubai\r\n19 - Serbia - Belgrade\r\n20 - Japan - Tokyo\r\n21 - United Kingdom - London\r\n22 - United Kingdom - Manchester\r\n23 - France - Paris\r\n24 - Poland - Warsaw\r\n25 - Latvia - Riga\r\n26 - Ireland - Dublin\r\n27 - Estonia - Tallinn\r\n28 - Czech Republic - Prague\r\n29 - United States - Las Vegas\r\n30 - Slovakia - Bratislava\r\n31 - United Kingdom - London\r\n32 - Italy - Milan\r\n33 - Spain - Madrid\r\n34 - Canada - Montreal\r\n35 - Turkey - Istanbul\r\n36 - Brazil - Sao Paulo\r\n37 - United States - Miami\r\n38 - Mexico - Mexico City\r\n39 - South Africa - Johannesburg\r\n40 - United States - Phoenix\r\n41 - Israel - Tel Aviv\r\n42 - Chile - Santiago\r\n43 - Thailand - Bangkok\r\n44 - Belgium - Brussels\r\n45 - Albania - Tirana\r\n46 - Romania - Bucharest\r\n47 - Switzerland - Z\u00fcrich\r\n48 - Portugal - Lisbon\r\n49 - Australia - Sydney\r\n50 - Austria - Vienna\r\n\u001b[?25l\u001b[2K\r\u001b[1m\u001b[31m\u2717\u001b[0m \u001b[1mEnter a number corresponding to the desired server\u001b[0m\u001b[1m:\u001b[0m \u2588\r\n\u001b[J\u001b[2K\r \u0008" (spawn_id exp4) match glob pattern "*Enter a number corresponding to the desired server:*"? no
expect: timed out
send: sending "1\r" to { exp4 }
tty_raw_noecho: was raw = 0  echo = 1
spawn id exp4 sent <\u001b[J\u001b[2K\r1\u001b[J\u001b[2K\r\u001b[1A\u001b[2K\r\u001b[2K\r\u001b[1m\u001b[32m\u2714\u001b[0m \u001b[1mEnter a number corresponding to the desired server\u001b[0m\u001b[1m:\u001b[0m 1\u2588\r\n1\u001b[J\u001b[2K\r \u0008\u001b[J\u001b[2K\r \u0008\u001b[J\u001b[2K\r\u001b[1A\u001b[2K\r\u001b[2K\r\u001b[1m\u001b[32m\u2714\u001b[0m \u001b[1mEnter a number corresponding to the desired server\u001b[0m\u001b[1m:\u001b[0m 1\u2588\r\n \u0008\u001b[J\u001b[2K\r\u001b[1A\u001b[2K\r\u001b[2K\r\u001b[2mEnter a number corresponding to the desired server\u001b[0m\u001b[2m:\u001b[0m 1\r\n \u0008\u001b[J\u001b[2K\r\u0Enter a number corresponding to the desired server: 1
spawn id exp4 sent <You have successfully connected to Netherlands - Amsterdam.\r\n>
You have successfully connected to Netherlands - Amsterdam.
interact: received eof from spawn_id exp4
tty_set: raw = 0, echo = 1
tty_set: raw = 3, echo = 0
shell scripting vpn expect
1个回答
0
投票

我已经解决了这个问题,根据调试的输出,默认值超时,因此我将超时更改为不同的值:

spawn atlasvpn connect
set timeout 1
expect "*Enter a number corresponding to the desired category:*" 
send "1\r"
expect "*Enter a number corresponding to the desired server:*" 
send "1\r"
interact

现在它会在 1 秒内运行回答每个问题。

© www.soinside.com 2019 - 2024. All rights reserved.