如果通过对expect重定向的方式写脚本的话,这样的脚本适合出现在脚本文件中,如果是通过expect -c这样的方式调用,则运行在命令行上
示例一
该示例适合运行在脚本文件中,然后通过expect命令运行脚本文件,比如脚本名test.sh
1
2
3
4
5
6
7
8
9
10
11
12
|
expect << 'END'
set pass "segfault"
spawn ssh root@segfault.net
expect {
"yes/no" { send "yes\r"; exp_continue }
"password:" { send -- "$pass\r" }
}
expect "Continuing in 60 sec..." { sleep 60; send "\r" }
# expect "Press any key to continue" { send "\r" }
expect "Would you like to see your SECRET now? (y/N)" { send "y" }
interact
END
|
示例二
该示例适合运行在命令行,如果将该示例添加到脚本文件运行则会执行不了,直接复制该示例粘贴到命令行上回车运行即可
1
2
3
4
5
6
7
8
9
10
11
12
|
expect -c '
set pass "segfault"
spawn ssh root@segfault.net
expect {
"yes/no" { send "yes\r"; exp_continue }
"password:" { send -- "$pass\r" }
}
expect "Continuing in 60 sec..." { sleep 60; send "\r" }
# expect "Press any key to continue" { send "\r" }
expect "Would you like to see your SECRET now? (y/N)" { send "y" }
interact
'
|
示例三
这是一个常规示例,同样,运行在命令行上
1
2
3
4
5
6
7
8
9
|
expect -c '
set pass "Mind@123"
spawn ssh root@192.168.0.2
expect {
"yes/no" { send "yes\r"; exp_continue }
"password:" { send -- "$pass\r" }
}
interact
'
|