当前位置:首页 > 操作系统 > Linux

Linux 之 expect 非交换式命令

expect 非交换式命令

测试实例:非交换式登陆某一台服务器,通常第一次登陆一台服务器的时候,需要按一下yes,然后再输入密码,我们目的是通过expect模拟输入

[root@localhost ~]# 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.
root@10.3.151.31‘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 "kang@$host"
expect {
                "yes/no"        {send "yesr";exp_continue}
                "*password"     {send "$passwordr"}
}
expect eof

3、运行test.exp文件

[root@localhost ~]# expect test.exp 10.3.151.31
spawn ssh kang@10.3.151.31
kang@10.3.151.31‘s password: 
[kang@localhost ~]$

原文:http://blog.51cto.com/12965094/2125676


【说明】本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:254677821)!

相关教程推荐

其他课程推荐