1. 程式人生 > >Linux 之 expect 非交換式命令

Linux 之 expect 非交換式命令

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.
[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 "kang@$host"
expect {
                "yes/no"        {send "yes\r";exp_continue}
                "*password"     {send "$password\r"}
}
expect eof

3、運行test.exp文件

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

Linux 之 expect 非交換式命令