Expect Question

I'm using expect to log into a remote host and then execute another script. A log of the script is being created but I can't see to get the script to display while it's running. Any ideas would be appreciated. Here is the script.

#!/bin/bash
cd /root
cat /root/hostname1.txt | while read line
do
   expect -c "
log_user 1
spawn ssh -o UserKnownHostsFile=/dev/null -o strictHostKeyChecking=no user@$line
expect \"word:\"
send \"password\r\"
expect \"# \"
send \"whoami\r\"
expect \"# \"
send \"su -\r\"
expect \"word:\"
send \"password\r\"
expect \"# \"
send \"pwd\"
expect \"# \"
send \"!\r\"
" 

done

Honestly,

You are better off to set up password-less ssh using shared keys versus what you are attempting to do.,

2 Likes