我想使用来对远程服务器执行一些命令expect
。但是,当我使用.*\$$
它时,总是无法匹配远程提示来发送命令。
以下是我的脚本:
#!/bin/ksh
while read -r line; do
/usr/linux/bin/expect -c "
set timeout 20
spawn ssh padmin@$line
expect {
-re \".*(P|p)assword:\" {send \"$2\r\"}
}
expect {
-re \".*\$$\" {send \"ls -lt cfgbackups|grep gz|head -n 1|awk '{print \$9}'\r\"}
}
expect {
-re \".*\$$\" {exit 0}
eof {exit 0}
}
send_user \"$expect_out(1,string)\n\"
" 2>"$line".errlog &
done <"$1"
当我使用调试模式进行检查时,收到以下消息:
expect version 5.45.4
spawn ssh [email protected]
parent: waiting for sync byte
parent: telling child to go ahead
parent: now unsynchronized from child
spawn: returns {3474502}
Gate keeper glob pattern for '(P|p)assword:' is ''. Not usable, disabling the performance booster.
expect: does "" (spawn_id exp4) match regular expression "(P|p)assword:"? (No Gate, RE only) gate=yes re=no
[email protected]'s password:
expect: does "[email protected]'s password: " (spawn_id exp4) match regular expression "(P|p)assword:"? (No Gate, RE only) gate=yes re=yes
expect: set expect_out(0,string) "password:"
expect: set expect_out(1,string) "p"
expect: set expect_out(spawn_id) "exp4"
expect: set expect_out(buffer) "[email protected]'s password:"
send: sending "padmin\r" to { exp4 }
Gate keeper glob pattern for '^:.*$' is ':*'. Activating booster.
expect: does " " (spawn_id exp4) match regular expression "^:.*$"? Gate ":*"? gate=no
"send_user"? no
expect: does " \r\n" (spawn_id exp4) match regular expression "^:.*$"? Gate ":*"? gate=no
"send_user"? no
Last unsuccessful login: Tue Jul 7 03:04:50 BEIST 2020 on ssh from 192.168.0.5
Last login: Tue Jul 7 15:18:11 BEIST 2020 on ssh from 192.168.0.4
Welcome!
/etc/profile[63]: hostname: not found.
expect: does " \r\nLast unsuccessful login: Tue Jul 7 03:04:50 BEIST 2020 on ssh from 192.168.0.5\r\nLast login: Tue Jul 7 15:18:11 BEIST 2020 on ssh from 192.168.0.4\r\nWelcome!\r\n/etc/profile[63]: hostname: not found.\r\n" (spawn_id exp4) match regular expression "^:.*$"? Gate ":*"? gate=yes re=no
"send_user"? no
:/home/padmin$
expect: does " \r\nLast unsuccessful login: Tue Jul 7 03:04:50 BEIST 2020 on ssh from 192.168.0.5\r\nLast login: Tue Jul 7 15:18:11 BEIST 2020 on ssh from 192.168.0.4\r\nWelcome!\r\n/etc/profile[63]: hostname: not found.\r\n:/home/padmin$ " (spawn_id exp4) match regular expression "^:.*$"? Gate ":*"? gate=yes re=no
"send_user"? no
expect: timed out
Gate keeper glob pattern for '^:.*$' is ':*'. Activating booster.
expect: does " \r\nLast unsuccessful login: Tue Jul 7 03:04:50 BEIST 2020 on ssh from 192.168.0.5\r\nLast login: Tue Jul 7 15:18:11 BEIST 2020 on ssh from 192.168.0.4\r\nWelcome!\r\n/etc/profile[63]: hostname: not found.\r\n:/home/padmin$ " (spawn_id exp4) match regular expression "^:.*$"? Gate ":*"? gate=yes re=no
sighandler: handling signal(2)
async event handler: Tcl_Eval(exit 130)
那么我该如何解决该问题呢?非常感谢。
我发现了问题。问题是$
符号。当我使用\\\$
预期的输出时,脚本可以正常工作。因为$
也是正则表达式中的特殊符号。当我们使用\$
正则表达式时,会将其表示为正则表达式结束符号。因此,我们需要使用\\\$
将其转换为\$
。就这样。非常感谢。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句