Linux 之 expect 非交换式命令
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Linux 之 expect 非交换式命令相关的知识,希望对你有一定的参考价值。
expect 非交换式命令
测试实例:非交换式登陆某一台服务器,通常第一次登陆一台服务器的时候,需要按一下yes,然后再输入密码,我们目的是通过expect模拟输入
[[email protected] ~]# ssh 10.3.151.31
The authenticity of host ‘10.3.151.31 (10.3.151.31)‘ can‘t be established.
RSA key fingerprint is 08:1e:93:79:5c:0f:6c:de:68:d5:e8:57:8c:44:a5:57.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added ‘10.3.151.31‘ (RSA) to the list of known hosts.
[email protected]‘s password:
Last login: Wed Jun 6 18:32:57 2018 from 10.4.50.229
1、安装expect
yum -y install expect
2、编写test.exp文件
#!/usr/bin/expect
set host [lindex $argv 0]
set password "kang"
spawn ssh "[email protected]$host"
expect {
"yes/no" {send "yes\r";exp_continue}
"*password" {send "$password\r"}
}
expect eof
3、运行test.exp文件
[[email protected] ~]# expect test.exp 10.3.151.31
spawn ssh [email protected]
[email protected]‘s password:
[[email protected] ~]$
以上是关于Linux 之 expect 非交换式命令的主要内容,如果未能解决你的问题,请参考以下文章