Your script is attempting to combine two interpreters. You have both
#!/usr/bin/expect. That won't work. You can only use one of the two. Since
bash was first, your script is being run as a bash script.
However, within your script, you have
expect commands such as
send. Since the script is being read by
bash and not by
expect, this fails. You could get around this by writing different
expect scripts and calling them from your
bash script or by translating the whole thing to
The best way though, and one that avoids the horrible practice of having your passwords in plain text in a simple text file, is to set up passwordless ssh instead. That way, the
scp won't need a password and you have no need for
First, create a public ssh key on your machine:
ssh-keygen -t rsa
You will be asked for a passphrase which you will be asked to enter the first time you run any ssh command after each login. This means that for multiple ssh or scp commands, you will only have to enter it once. Leave the passphrase empty for completely passwordless access.
Once you have generated your public key, copy it over to each computer in your network :
while read ip; do
ssh-copy-id -i ~/.ssh/id_rsa.pub user1@$ip
done < IPlistfile.txt
IPlistfile.txt should be a file containing a server's name or IP on each line. For example:
Since this is the first time you do this, you will have to manually enter the password for each IP but once you've done that, you will be able to copy files to any of these machines with a simple:
scp file user@host1:/path/to/file
Remove the expect from your script. Now that you have passwordless access, you can use your script as:
echo "I will fail if you give junk values!!"
echo " "
echo "Enter file name: "
echo " "
echo "Where you want to copy?"
echo "1. if Host1"
echo "2. if Host2"
echo "3. if Host3"
echo " "
if [ $choice -eq "1" ]
scp filePath uname@host1:/usr/tmp
elif [ $choice -eq "2" ]
scp filePath uname@host2:/usr/tmp
elif [ $choice -eq "3" ]
scp filePath uname@host3:/usr/tmp
echo "Wrong input"