1

I am trying to move/copy some files from my local machine to another host which I am able to access via ssh. But unfortunately this below script is not working.

when I run this script it is asking me Are you sure you want to continue connecting (yes/no)? every time I run it.

I have 3 file file on my local machine ranfull ranhalf midran which I need to transfer/copy it to other host via scp script, but only the first file is getting copied.

It won't be possible to set sshkeygen.

#!/bin/bash

HOSTS="VM2 VM3 VM4 VM1"

read -p "Password: " PASSWORD

for HOST in $HOSTS
do
expect -c "
spawn /usr/bin/scp /tmp/ranfull ranhalf midran root@$HOST:/tmp/
expect {
"*password:*" { send $PASSWORD\r;interact }
}
exit
"
done
  • Where are the files ranhalf and midran? In /tmp? – Ipor Sircer Nov 8 '16 at 6:41
  • all are in the same location /tmp. – user155159 Nov 8 '16 at 6:42
  • spawn /usr/bin/scp /tmp/ranfull ranhalf midran root@$HOST:/tmp/ should be spawn /usr/bin/scp -v /tmp/ranfull /tmp/ranhalf /tmp/midran root@$HOST:/tmp/ – SHW Nov 8 '16 at 6:43
  • @SHW I changed worked but i am getting this error, I don't know why The authenticity of host VM3 (192.168.0.119)' can't be established. & every time it is waiting for Are you sure you want to continue connecting (yes/no)? caan i skip this message – user155159 Nov 8 '16 at 6:50
  • Are you changing the IP of either source or target machine ? – SHW Nov 8 '16 at 6:51
1

First, your quoting is suspect: you have double quotes in a double quoted string. Let's fix that, and improve the indentation:

for HOST in $HOSTS; do
    expect -c <<END_EXPECT
        spawn /usr/bin/scp /tmp/ranfull ranhalf midran root@$HOST:/tmp/
        expect {
            "*password:*" {
                send $PASSWORD\r
                interact
            }
        }
        exit
END_EXPECT
done

Now, to accomodate a prompt that may or may not appear, with expect you do this:

        expect {
            "Are you sure you want to continue connecting*" {
                send yes\r
                exp_continue
            }
            "*password:*" {
                send $PASSWORD\r
                interact
            }
        }

exp_continue essentially "loops" back to the expect command so it can still watch for another "Are you sure..." prompt or the password prompt.

1

-o StrictHostKeyChecking=no

#!/bin/bash HOSTS="VM2 VM3 VM4 VM1" read -p "Password: " PASSWORD for HOST in $HOSTS do expect -c " spawn /usr/bin/scp -o StrictHostKeyChecking=no /tmp/ranfull ranhalf midran root@$HOST:/tmp/ expect { "password:" { send $PASSWORD\r;interact } } exit " done

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy