linux-telnet-具有期望的脚本 [英] linux - telnet - script with expect
问题描述
我正在编写一个期望脚本,该脚本通过telnet与服务器通信,但是现在我需要评估服务器的答复.
用法:
./edit.expect
EXPECT脚本:
#!/usr/bin/expect<br>
spawn telnet ip port
expect "HUH?"
send "login testuser pass\r"
expect "good"
send "select 1\r"
expect "good"
send "me\r"
expect "nick=testuser id=ID group=testgroup login=testuser"
send "edit id=ID group=3\r"
expect "good"
send "quit\r"
如果我发送命令我",我将从服务器上得到我需要评估的答复. 来自服务器的回复看起来像这个例子……"nick = NICK id = ID group = GROUP login = LOGIN".
我如何提取回复的ID并将其用于发送命令中?
我希望你能帮助我.非常感谢!
您也可以尝试这种方式.
set user_id {}
expect -re {nick=(.*)\s+id=(.*)\s+group=(.*)\s+login=(.*)\n} {
#Each submatch will be saved in the the expect_out buffer with the index of 'n,string' for the 'n'th submatch string
puts "You have entered : $expect_out(0,string)"; #expect_out(0,string) will have the whole expect match string including the newline
puts "Nick : $expect_out(1,string)"
puts "ID : $expect_out(2,string)"
puts "Group : $expect_out(3,string)"
puts "Login : $expect_out(4,string)"
set user_id $expect_out(2,string)
}
send "This is $user_id, reporting Sir! ;)"
#Your further 'expect' statements goes below.
您可以根据需要自定义regexp
,并注意在expect
命令中使用带有-re
标志的大括号{}
.
如果使用大括号,Tcl
将不执行任何变量替换,并且如果需要在expect
中使用变量,则应使用双引号,并相应地需要转义反斜杠和通配符. /p>
I was writing an expect-script which communicate with a server via telnet, but right now i need to evaluate the reply from server.
Usage:
./edit.expect
EXPECT script:
#!/usr/bin/expect<br>
spawn telnet ip port
expect "HUH?"
send "login testuser pass\r"
expect "good"
send "select 1\r"
expect "good"
send "me\r"
expect "nick=testuser id=ID group=testgroup login=testuser"
send "edit id=ID group=3\r"
expect "good"
send "quit\r"
If i send the command "me" i get a reply from the server which i need to evaluate. The reply from server looks like this example... "nick=NICK id=ID group=GROUP login=LOGIN".
How do i extract the id of the reply and use it in a send-command?
I hope you could help me with that. Thanks a lot!
You can try this way too.
set user_id {}
expect -re {nick=(.*)\s+id=(.*)\s+group=(.*)\s+login=(.*)\n} {
#Each submatch will be saved in the the expect_out buffer with the index of 'n,string' for the 'n'th submatch string
puts "You have entered : $expect_out(0,string)"; #expect_out(0,string) will have the whole expect match string including the newline
puts "Nick : $expect_out(1,string)"
puts "ID : $expect_out(2,string)"
puts "Group : $expect_out(3,string)"
puts "Login : $expect_out(4,string)"
set user_id $expect_out(2,string)
}
send "This is $user_id, reporting Sir! ;)"
#Your further 'expect' statements goes below.
You can customize the regexp
as per your wish and note the use of braces {}
with -re
flag in the expect
command.
If you are using braces, Tcl
won't do any variable substitution and if you need to use variable in the expect
then you should use double quotes and correspondingly you need to escape the backslashes and wildcard operators.
这篇关于linux-telnet-具有期望的脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!