#!/bin/bash

# First, check if id_rsa.pub exists.
# If it does we're going to overwrite it.
# Otherwise, create it, the else part.
if [ -e $HOME/.ssh/id_rsa.pub ] 
then
/usr/bin/expect << EOD
spawn ssh-keygen
expect "Enter"
send "\n"
expect "Overwrite"
send "y\n"
expect "Enter"
send "\n"
expect "Enter"
send "\n"
set results $expect_out(buffer)
expect eof
EOD
else
/usr/bin/expect << EOD
spawn ssh-keygen
expect "Enter"
send "\n"
expect "Enter"
send "\n"
expect "Enter"
send "\n"
set results $expect_out(buffer)
expect eof
EOD
fi

pword="knc@123"
ip="172.31.1.1 172.31.2.1 172.31.3.1 172.31.4.1"
root="/root"
lscmd="ls -a"
mkdircmd="mkdir /root/.ssh"
src="/root/.ssh/id_rsa.pub"
dest="/root/.ssh/authorized_keys"
addr=""

setup_login()
{
echo $addr
echo cd /root/.ssh
cd /root/.ssh
/usr/bin/expect << EOD
spawn ssh root@$addr $lscmd
expect "password:"
send -- "$pword\n"
spawn ssh root@$addr $mkdircmd
expect "password:"
send -- "$pword\n"
spawn scp $src root@$addr:$dest
expect "password:"
send -- "$pword\n"
set results $expect_out(buffer)
expect eof
EOD
return
}

for i in $ip 
do
	addr=$i
	setup_login
	echo ssh-add
	ssh-add
done

echo $results

