linux 使用expect

user=root
ip=1.1.1.1
passwd=1111
expect<<!
spawn  ssh $user@$ip
expect {
    "(yes/no)?" {
        send "yes
"
        expect "assword:"
        send "$passwd
"
    }
        "assword:" {
        send "$passwd
"
     }
 }
expect "#" 
send "sh /root/sbin/get_master.sh
"
send "exit
"
expect eof
!

原文地址:https://www.cnblogs.com/hzcya1995/p/13351580.html