无法输出" ps"在expect脚本中正确命令

时间:2016-12-23 11:27:50

标签: tcl expect

我在下面复制了我的脚本。对于变量" userName"我没有在正确的位置获得预期的输出。预期的输出是" pload",它最终会到来。有任何建议可以正确使用吗?

脚本:

#!/usr/bin/expect
exp_internal 1
set timeout 30
set prompt {[$]}
spawn ssh -o StrictHostKeyChecking=no $username@$server.com
expect "assword"
send $password\r
expect "$prompt"
expect *
send "ps aux | grep -v grep | tr ' ' '\\n' | grep pload | uniq\r"
expect -re "(.*)\n"
sleep 20
set userName $expect_out(buffer)
puts "buffer:$userName end"
if { $userName eq "pload" } {
   send "sudo su - pload\r"
} else {
send "sudo su - pdev\r"
}
expect "assword"
send $password\r
expect "$prompt"

以下是日志:

.
.
3997@fd0441a2:~\u0007\u001b[?1034h[f5103997@fd0441a2 ~]$"
expect: does " " (spawn_id exp4) match glob pattern "*"? yes
expect: set expect_out(0,string) " "
expect: set expect_out(spawn_id) "exp4"
expect: set expect_out(buffer) " "
send: sending "ps aux | grep -v grep | tr ' ' '\n' | grep pload | uniq\r" to { exp4 }
Gate keeper glob pattern for '(.*)
' is '*
'. Activating booster.

expect: does "" (spawn_id exp4) match regular expression "(.*)\n"? Gate "*\n"? gate=no
ps aux | grep -v grep | tr ' ' '\n' | grep pload | uniq

expect: does "ps aux | grep -v grep | tr ' ' '\n' | grep pload | uniq\r\n" (spawn_id exp4) match regular expression "(.*)\n"? Gate "*\n"? gate=yes re=yes
expect: set expect_out(spawn_id) "exp4"
expect: set expect_out(buffer) "ps aux | grep -v grep | tr ' ' '\n' | grep pload | uniq\r\n"
buffer:ps aux | grep -v grep | tr ' ' '\n' | grep pload | uniq
 end
send: sending "sudo su - pdev\r" to { exp4 }

expect: does "" (spawn_id exp4) match glob pattern "assword"? no
pload
[f5103997@fd0441a2 ~]$
expect: does "pload\r\n\u001b]0;f5103997@fd0441a2:~\u0007[f5103997@fd0441a2 ~]$ " (spawn_id exp4) match glob pattern "assword"? no
sudo su - pdev

2 个答案:

答案 0 :(得分:0)

我建议对期望代码进行一些编辑:

  • 使用expect -re并使用模式。例如,提示设置为[f5103997@fd0441a2 ~]$,但匹配的模式为[$]expect -re {.*$]\s*}更好,IMO。
  • 首次登录后,立即将默认提示更改为已知值(不包含任何TCL特殊字符(如$[)。下一组命令应顺利执行。我试过它,这里是一个片段:

    expect -re {.*[pP]assword}
    send $password\r
    expect -re "$prompt"
    set newPrompt {MY_NEW_PROMPT> }
    send "export PS1=\"$newPrompt\"\r"
    expect -re ".*$newPrompt"
    send "\r"
    after 2000 ;# flush out the MOTD, etc.
    expect -re ".*$newPrompt"
    send "pwd\r"
    expect -re ".*$newPrompt"
    puts -->$expect_out(buffer)
    send "exit\r"
    

<强>输出:

sharad@sharad:~$ expect sharad.expect
spawn ssh -o StrictHostKeyChecking=no sharad@localhost
sharad@localhost's password: 
export PS1="MY_NEW_PROMPT> "

Welcome to Ubuntu 16.04.1 LTS (GNU/Linux 4.4.0-53-generic x86_64)

 * Documentation:  https://help.ubuntu.com
 * Management:     https://landscape.canonical.com
 * Support:        https://ubuntu.com/advantage

175 packages can be updated.
4 updates are security updates.

export PS1="MY_NEW_PROMPT> "

Last login: Fri Dec 23 23:02:22 2016 from 127.0.0.1
sharad@sharad:~$ export PS1="MY_NEW_PROMPT> "
MY_NEW_PROMPT> 
MY_NEW_PROMPT> pwd
/home/sharad
MY_NEW_PROMPT> -->pwd
/home/sharad
MY_NEW_PROMPT> 
sharad@sharad:~$ 

答案 1 :(得分:0)

我通常会在命令的输出周围添加一些特殊字符,因此编写期望RE会更容易一些。所以试试这样:

send "echo ==\">\$(ps aux | grep -v grep | tr ' ' '\\n' | grep pload | uniq)<\"==\r"
expect -re "==>(.*)<=="
set userName $expect_out(1,string)
#                        ^^^^^^^^
expect "$prompt"