Posted to tcl by schelte at Sat Aug 25 20:44:32 GMT 2012view pretty

#!/usr/bin/expect

set file iplist.txt
set cmd "ssh-copy-id -i /home/karthick/.ssh/karthick.pub"
set user root
set password Password123

set f [open iplist.txt]
set hostlist [split [read $f] \n]
close $f

foreach host $hostlist {
    if {$host eq ""} continue

    spawn {*}$cmd $user@$host
    expect {
	-timeout 5
	-ex "Are you sure you want to continue connecting (yes/no)? " {
	    send -- "yes\r"
	    exp_continue
	}
	-ex "assword: " {
	    send "$password\r"
	    exp_continue
	}
	-ex "Permission denied" {
	    puts stdout "Permission denied for $user on following server $host"
	    set f [open login_issue.log a]
	    puts $f "Permission denied for $user on the server $host"
	    close $f
	    exp_continue
	}
	timeout {
	    set f [open unreachable.log a]
	    puts $f "Access to server $host timed out"
	    close $f
	    close
	    exp_continue
	}
	eof {}
    }
}