#!/usr/bin/expect
set timeout 120 #设置执行超时时间,任何输入120秒后退出set password "password" #set设置password名,值为passwordspawn ssh -p 端口 root@ip #连接用户主机账号expect "*password" { send "$password\r"} #定义命令开始,并发送字符,\r为回车符expect "*]#" #定义命令开始send "eval sed -i 's!gcomm://!gcomm://$IP_2!g' /etc/my.cnf.d/server.cnf\r" #发送命令并执行expect "*]#"send "\r"expect "*]#"send "/etc/init.d/mysql restart\r"expect "*]#"send "logout\r" expect eof #最后别忘记加这句,否则会得不到正确的结果