You're confusing expect syntax and shell syntax. You want:
#!/usr/bin/expect
set timeout 9
set passwrd PASSWORD
set username USER_NAME
set host host.example.com
spawn ssh $username@$host
expect "Password:"
send "$passwrd\r"
interact
Question
I'm trying to login to a remote host using expect & spawn. While automating this it's going till ssh username@host.example.com
and password prompt came and it is terminating wihtout supplying the password. What is wrong with this script ?
#!/usr/bin/expect
set timeout 9
passwrd=PASSWORD
username=USER_NAME
host=host.example.com
/usr/bin/expect << EOF
spawn ssh $username@$host
expect "Password:"
send "$passwrd\r"
interact
Solution
You're confusing expect syntax and shell syntax. You want:
#!/usr/bin/expect
set timeout 9
set passwrd PASSWORD
set username USER_NAME
set host host.example.com
spawn ssh $username@$host
expect "Password:"
send "$passwrd\r"
interact