expect简单自动交互-用于密码、命令输入

1. 安装expect

#yum -y install expect

2. 新建.exp文件,用于ssh交换机

#vi exp.exp

#!/bin/expect
set f [open ipfile.txt]
while {1} {
        set line [gets $f]
        if {[eof $f]} {
                close $f
                break
        }
        set timeout 20
        spawn ssh -l user $line
        expect {
                "(yes/no)?" { send "yes\r"; exp_continue }
                "Enter password:" { send "pwd\r"; exp_continue }
                "Password:" { send "pwd\r"; exp_continue }
                "user@$line's password:" { send "pwd\r"; exp_continue }
                "#" { send "exit\r"}
                ">" { send "quit\r"}
        }
        interact
}

3. 交换机IP地址存入ipfile.txt

#vi ipfile.txt

192.168.1.1
192.168.1.2
192.168.1.3
192.168.1.4

4. 执行

#expect exp.exp

 

posted @ 2019-03-08 14:18  妖怪梧桐  阅读(591)  评论(0编辑  收藏  举报