expect点滴

1. 赋值命令

set


2. 输出到屏幕

puts


3. 解析参数

[lrange $argv 0 0]表示第一个参数(脚本自身名字不出现在argv数组中)

[lrange $ragv 0 2]表示第一到第二个参数


示例:

#!/usr/bin/expect
set aaa [lrange $argv 0 2]
puts "example: $aaa"
                   
[root@localhost home]# ./ttt.sh 1 2 3
example: 1 2 3

4. 期望匹配命令

expect "*#"

期望匹配相应的字符串,可以使用通配符。既可以匹配一条字符串,也可以匹配多个字符串


5. 循环进入下一条匹配

exp_continue,用在expect匹配多个字符串


6. 发送命令

send "ls -l\r"

向终端发送指定的内容,以'\r'结尾

#!/usr/bin/expect

set timeout 3
set passwd 123456
spawn ssh [email protected]
expect {
"yes/no" { puts "111"; send "yes\r"; exp_continue}
"password:" { send "$passwd\r" }
}

运行结果:

[root@localhost home]# ./aaa.sh
spawn ssh [email protected]
The authenticity of host '127.0.0.1 (127.0.0.1)' can't be established.
RSA key fingerprint is b9:67:af:0a:c1:5d:8a:c0:3b:25:40:3d:1a:e5:50:0a.
Are you sure you want to continue connecting (yes/no)? 111
yes
Warning: Permanently added '127.0.0.1' (RSA) to the list of known hosts.
[email protected]'s password: [root@localhost home]#

7. for循环

for {set i 3} {$i < 2} {incr i} {
    puts "I inside second loop: $i"
}


8. while循环

while {$i < 10} {
    puts "I inside third loop: $i"
    incr i
    puts "I after incr: $i"
}




你可能感兴趣的:(expect点滴)