The use case of this script is I have various servers with different ssh keys. I am trying to write a script so when called will log into the specified server. An example of usage would be:
./ServerLogin.sh Server1
I feel that I am fairly close, but The last part of expect interact is tripping me up. This is a simplified version:
#!/bin/bash
ServerName="$1"
case $ServerName in
"Server1") IP="1.2.3.4" ; keyPath="/path/to/key.pem" ; password="password" ; break ;;
*) echo "Server not recognized" ; exit ;;
esac
/usr/bin/expect << EOD
spawn ssh -i $keyPath user@$IP
expect "*.pem': "
send "$password\r"
interact
EOD
The result of this is it logs in and immediately closes. I want for the session to remain interactable.
Any ideas?
The problem is in expect << EOF
. With expect << EOF
, expect's stdin is the here-doc rather than a tty. But the interact
command only works when expect's stdin is a tty. Your answer is one solution. Another solution is to use expect -c
if you prefer not using a tmp file.
expect -c "
spawn ssh -i $keyPath user@$IP
expect \"*.pem': \"
send \"$password\r\"
interact
"