i trying automate password-less ssh setup master account other slave accounts.i have script named addssh.ksh
setup.when script run manually,it asks same password same times,it copied keys using scp. slave accounts saved in file env.txt
.so now, have shell script(run.ksh
) reads accounts file(env.txt
) 1 one , uses expect
script auto_ssh.ksh
handle interaction , enters password accordingly.
env.txt
account1@machine1 account2@machine2 account3@machine3 account4@machine4
run.ksh:
#!/usr/bin/ksh while read env username=`echo $env | cut -d"@" -f1`; hostname=`echo $env | cut -d"@" -f2`; password='unix_11' ssh -n -o passwordauthentication=no ${env} ' ' 2>/dev/null if [ $? -eq 0 ]; printf "\nconnection ok : $env \n" else expect auto_ssh.ksh $username $hostname $password fi done<env.txt
auto_ssh.ksh:
#!expect set timeout 6 set user [lindex $argv 0] set machine [lindex $argv 1] set password [lindex $argv 2] spawn addssh.ksh $user $machine expect "password:" send "$password\r"; expect "password:" send "$password\r"; interact
if run script auto_ssh.ksh
like
./auto_ssh.ksh account1 machine1 password
it runs fine when call inside shell script,this expect script exits @ second password.when ran shell script in debug mode, see instead of sending password second time moves reading next env env.txt
, exits.
this line of output in debug mode fails.
account1@machine1's password: + read env
add exp_internal 1
expect script additional debugging. suspect might need refine expect: expect -re {password:\s*$}
if don't need interact addssh.ksh, change interact
expect eof
why expect script have ".ksh" extension?
Comments
Post a Comment