##1.shell腳本 #!/bin/sh BASE_DIR=`dirname $0` && cd $BASE_DIR function ssh_copy_id(){ for ip in `cat .ips-other`;do expect auto-ssh-copy-id.exp $ip container `cat .password` if [ $? -ne 0 ]; then echo "免認證失敗" fi done } ##2.expect腳本 #!/usr/bin/expect set timeout 5 set ip [lindex $argv 0] set user [lindex $argv 1] set password [lindex $argv 2] #複製密鑰到目標機器 spawn ssh-copy-id $user@$ip -p 1433 expect { "yes/no" {send "yes\r";exp_continue;} "password" {send "$password\r";exp_continue;} }