Tcl 无法获得“的输出”;ps";在expect脚本中正确使用命令

Tcl 无法获得“的输出”;ps";在expect脚本中正确使用命令,tcl,expect,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 "$

我在下面复制了我的脚本。对于变量“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

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

  • 使用
    expect-re
    并使用模式。例如,提示设置为
    [f5103997@fd0441a2~]$
    但要匹配的模式是
    [$]
    <代码>预期-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:~$ 

我通常会在命令的输出周围添加一些特殊字符,这样编写expect RE就容易一些。因此,请尝试这样做:

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

send“echo==\”>\$(ps aux | grep-v grep | tr'\\n'| grep pload | uniq)
ps aux | grep-v grep | tr''\n'| grep pload | uniq
命令的输出示例?输出是否可能是多行的?输出是否可能是空的?您希望在userName var中得到什么?@whjm,在userName变量中,我希望pload或empty作为输出。非常感谢,您让我度过了美好的一天:)