log_file命令不在Expect脚本中记录命令的输出

时间:2016-05-31 08:24:30

标签: expect

我正在运行一个debug命令来将我的命令输出记录到一个文件。我尝试了log_file命令,但它没有记录输出。我的代码如下:

log_file -a gdb.txt 
send "~/debugulator.sh $file mns20\r"
log_user 0
expect -re {DSP.*0x[0-9][0-9][0-9][0-9]}
log_user 1
send_log $expect_out(0,string)
log_file 

但是当我打开gdb.txt时,里面什么也没有。有人告诉我到底哪里出错。我正在使用的版本是5.26.0

更新 这是我正在尝试的另一段代码......它仍然无法运行..我在添加exp_internal 1命令后发布了输出。代码如下:

spawn telnet 10.1.1.2
expect "Login:"
send "xyz\r"
expect "Password:"
send "Nxyz1\r"
expect "xyz:/home/xyz>"
send "cd /sonus/support/GSX-[lindex $argv 0]\r"
log_file myfile.log
send_log "this is in the log file \n"
log_file
interact

输出     spawn telnet 10.1.1.2     parent: waiting for sync byte     parent: telling child to go ahead     parent: now unsynchronized from child     spawn: returns {17179}

`expect: does "" (spawn_id exp6) match glob pattern "Login:"? no`
`Trying 10.1.1.2...`

`expect: does "Trying 10.1.1.2...\r\n" (spawn_id exp6) match glob pattern "Login:"? no`
`Connected to 10.1.1.2.`
`Escape character is '^]'.`

`expect: does "Trying 10.1.1.2...\r\nConnected to 10.1.1.2.\r\nEscape     character is '^]'.\r\n" (spawn_id exp6) match glob pattern "Login:"? no`


`SunOS 5.9`


`expect: does "Trying 10.1.1.2...\r\nConnected to 10.1.1.2.\r\nEscape character is '^]'.\r\n\r\n\r\nSunOS 5.9\r\n\r\r\n\r" (spawn_id exp6) match glob pattern "Login:"? no
login: xyz\r`
`expect: does "Trying 10.1.1.2...\r\nConnected to 10.1.1.2.\r\nEscape character is '^]'.\r\n\r\n\r\nSunOS 5.9\r\n\r\r\n\rlogin: " (spawn_id exp6) match glob pattern "Login:"? no`
`expect: timed out`
`send: sending "xyz\r" to { exp6 }`

`expect: does "Trying 10.1.1.2...\r\nConnected to 10.1.1.2.\r\nEscape character is '^]'.\r\n\r\n\r\nSunOS 5.9\r\n\r\r\n\rlogin: " (spawn_id exp6) match glob pattern "Password:"? no

xyz`

 `expect: does "Trying 10.1.1.2...\r\nConnected to 10.1.1.2.\r\nEscape character is '^]'.\r\n\r\n\r\nSunOS 5.9\r\n\r\r\n\rlogin: xyz\r\n" (spawn_id exp6) match glob pattern "Password:"? no`
 `Password:` 
 `expect: does "Trying 10.1.1.2...\r\nConnected to 10.1.1.2.\r\nEscape character is '^]'.\r\n\r\n\r\nSunOS 5.9\r\n\r\r\n\rlogin: xyz\r\nPassword: " (spawn_id exp6) match glob pattern "Password:"? yes`
 `expect: set expect_out(0,string) "Password:"`
 `expect: set expect_out(spawn_id) "exp6"`
 `expect: set expect_out(buffer) "Trying 10.1.1.2...\r\nConnected to 10.1.1.2.\r\nEscape character is '^]'.\r\n\r\n\r\nSunOS 5.9\r\n\r\r\n\rlogin: xyz\r\nPassword:"`
 `send: sending "Nxyz1\r" to { exp6 }`

 `expect: does " " (spawn_id exp6) match glob pattern "xyz:/home/xyz>"? no`


 `expect: does " \r\n" (spawn_id exp6) match glob pattern "xyz:/home/xyz>"? no`
 `Last login: Tue May 31 05:10:20 from 10.253.6.98
 /home/xyz/.aliases: No such file or directory.`
 `[1]slate:xyz:/home/xyz>` 
 `expect: does " \r\nLast login: Tue May 31 05:10:20 from 10.253.6.98\r\n/home/xyz/.aliases: No such file or directory.\r\n\u001b]0;[slate:]/home/xyz\u0007[1]slate:\u001b[1mxyz\u001b[m:/home/xyz> " (spawn_id exp6) `match      glob pattern "xyz:/home/xyz>"? no
 `expect: timed out`
 `send: sending "cd /sonus/support/GSX-5\r" to { exp6 }`
 `tty_raw_noecho: was raw = 0  echo = 1`
 `spawn id exp6 sent <c>`
 `cspawn id exp6 sent <d /sonus/support/GSX-5\r\r\n\u001b]0;[slate:]/sonus/support/GSX-5\u0007[2]slate:\u001b[1msdwarampudi\u001b[m:/sonus/support    /GSX-5> >`
 `/sonus/support/GSX-5`
`[2]slate:xyz:/sonus/support/GSX-5>` 

1 个答案:

答案 0 :(得分:0)

您的提示中包含导致模式匹配失败的颜色代码:

expect: does "...slate:\u001b[1mxyz\u001b[m:/home/xyz> " (spawn_id exp6) `match      glob pattern "xyz:/home/xyz>"? no
# .................................^^^^^^^^........................................................^^^^^^^^^^^^^^

通常当我ssh到远程主机时,我要做的第一件事是(假设远程shell是sh / ksh / bash)将提示设置为易于匹配的东西:

spawn ssh remotehost
# ... password stuff
# now, set the prompt
send "PS1='>'\r"
expect -re {>$}    # use this to match the prompt from now on

实际上,您需要做的就是简化提示匹配:您的提示以>和空格结束,所以:

expect -re {> $}