25

I am facing an issue when I run simply these commands.

The remote server want to pass yes to add the key in RSA file because first time connection established with scp.

commands are given below

#!/bin/bash

scp  -P58222 root@IP:/root/K /N
/usr/bin/expect -c 'expect "\n" { expect "Are you sure you want to continue connecting (yes/no)?" }'
send "yes\r"
expect "$ "
send "exit\r"

Actually I have to pass yes in my script while asking

The authenticity of host 'ip address (ip address)' can't be established.

RSA key fingerprint is xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx.
Are you sure you want to continue connecting (yes/no)? 

Are you sure you want to continue connecting (yes/no)?

how can I get rid of this problem?

with

scp -o  StrictHostKeyChecking=no

it is still asking.

saravanakumar
  • 1,747
  • 4
  • 20
  • 38
Harry shah
  • 387
  • 1
  • 3
  • 9
  • 2
    You can just pass arguments to ssh/scp to tell it not to ask the question; no need to use `expect` to script the answer. – Charles Duffy Mar 31 '15 at 16:56
  • 1
    That said -- which *specific* question are you telling it to auto-answer yes to? If it's an unknown-host-key question, for instance, that would help. – Charles Duffy Mar 31 '15 at 16:57
  • specifically, `scp -q` enables quiet mode, which doesn't need to ask for confirmation. – Hans Z Mar 31 '15 at 17:01

3 Answers3

33
scp -o StrictHostKeyChecking=no root@IP:/root/K 

Obviously, this isn't a very secure solution. Works for one-shots where you're not concerned about man in the middle, though.

economy
  • 4,035
  • 6
  • 29
  • 37
  • Actually, when connection established it show connection can't established because remote host info not added when we type yes it display host machine info added successfully. – Harry shah Mar 31 '15 at 17:05
  • I'd suggest looking at `man scp` under the `-o` option for a full list of ssh/scp options. Depending on your setup, you may need another option flag set. – economy Mar 31 '15 at 17:09
  • I am checking it is helpful but now testing ..!! – Harry shah Mar 31 '15 at 17:25
  • The authenticity of host 'ip address (ip address)' can't be established. RSA key fingerprint is xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx. Are you sure you want to continue connecting (yes/no)? it is still asking that – Harry shah Mar 31 '15 at 17:41
  • Try redirecting the known hosts file to /dev/null: `scp -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no`... – economy Mar 31 '15 at 19:56
9

For me, this works:

 yes | scp -r /opt/MyFiles root@<MyNewServerIP>:/opt/MyFiles

Regards. =)

Chuss
  • 142
  • 1
  • 3
9

Probably the best way to do this would be to use the following command before your scp command:

ssh-keyscan -H ${SSH_HOST} >> ~/.ssh/known_hosts

This will add the SSH_HOST to your known hosts and scp will not complain. Substitute ${SSH_HOST} for the IP address you are trying to connect to.

user1847
  • 3,571
  • 1
  • 26
  • 35