一, 安装 expect
yum -y install expect
二, 常用程序
expect 程序:
- #!/usr/bin/expect -f
- set ip [lindex $argv 0 ] #接收第一个参数, 并设置 IP
- set password [lindex $argv 1 ] #接收第二个参数, 并设置密码
- set timeout 10 #设置超时时间
- spawn SSH root@$ip "hostname" #发送 SSH 请滶
- expect { #返回信息匹配
- "*yes/no" { send "yes\r"; exp_continue} #第一次 SSH 连接会提示 yes/no, 继续
- "*password:" { send "$password\r" } #出现密码提示, 发送密码
- }
- interact #交互模式, 用户会停留在远程服务器上面
- #!/bin/bash
- SERVERS="192.168.1.241 192.168.1.242"
- PASSWD="123456"
- function sshcopyid
- {
- expect -c "
- set timeout -1;
- spawn SSH-copy-id $1;
- expect {
- \"yes/no\" { send \"yes\r\" ;exp_contine; }
- \"password:\" { send \"$PASSWD\r\";exp_continue; }
- };
- expect eof;
- "
- }
- for server in $SERVERS
- do
- sshcopyid $server
- done
来源: http://www.bubuko.com/infodetail-2908617.html