Saturday, 15 June 2013

Expect - how to set output from $expect_out(0,string) as variable and use it further -



Expect - how to set output from $expect_out(0,string) as variable and use it further -

i totally new expect ... need write script telnet ms server, displays current sessions there , logoff specific user. script attached.

#!/bin/bash # execute expect part /usr/bin/expect <<eof # # telnet pc # spawn telnet 9.9.9.9; expect "login:"; send "admin\r"; expect "password:"; send "admin\r"; expect "*>"; send "query session test\r"; expect -re " \[0-9] "; set val [\$expect_out(0,string)]; send "logoff $val\r"; send "\r"; expect "*>"; send "\x1d"; expect "*>"; send "q\r"; exit 1; eof;

i can't working. seems i'm doing wrong defining variable. when run script i'm getting next result:

c:\users\admin>query session test sessionname username id state type device rdp-tcp#0 test 3 active rdpwd c:\users\admin>invalid command name " 3 " while executing "$expect_out(0,string)" invoked within "set val [$expect_out(0,string)]"

btw when used send_user in script instead, got right input in telnet session, reason not accepting command @ all.

any help appreciated. give thanks you.

expect treat things between matching brackets command.

set val [\$expect_out(0,string)] means executing command named $expect_out(0,string) (in case, it's 3.) , assigning command output val. that's why output shows invalid command name " 3 " because there no such command.

change line set val $expect_out(0,string) , believe things better.

btw, send_user puts. prints message standard output, not spawned process.

expect

No comments:

Post a Comment