SSH to multiple servers using Expect

Script will login to multiple servers and perform certain command automatically.

Requires an input file of format HostName:IP:Pass

#!/bin/bash
# Require an input.txt file with the format of CLLI:IP:Pass
# This scirpt will add the RSA key for lss user

if [ -e “input.txt” ]; then
while read i; do

/usr/bin/expect <(cat << EOD
set timeout 15
spawn ssh “user@$(echo $i | cut -d: -f 2)”
#######################

expect “yes/no” {
send “yes\r”
expect “Password:” { send {$(echo $i | cut -d: -f 3)}; send \r }
} “Password:” { send {$(echo $i | cut -d: -f 3)}; send \r }
expect -re “day: $” { send “\r” }
expect “:” { send “\r” }
expect -re “# $” { send “ll\r” }
expect -re “# $” { send “mkdir .ssh\r” }
expect -re “# $” { send “cd .ssh\r” }
expect -re “# $” { send “touch authorized_keys\r” }
expect -re “# $” { send “chmod 700 authorized_keys\r” }
expect -re “# $” { send “echo ‘ssh-rsa KEY’ >> authorized_keys\r” }
expect -re “# $” { send “cat authorized_keys\r” }
expect -re “# $” { send “exit\r” }
EOD
)
done < input.txt
fi