1

I have a bash script to copy a file to a remote server using scp and expect. The script is as follows:

#!/usr/bin/expect -f
set port [lindex $argv 0]
set file_to_send [lindex $argv 1]
set login [lindex $argv 2]@[lindex $argv 3]
set username [lindex $argv 2]
set hostname [lindex $argv 3]
set password [lindex $argv 4]
set dest_folder [lindex $argv 5]

spawn scp -P $port $file_to_send $login:$dest_folder
expect {
    # this will loop forever
    # {*fingerprint*}
    # sleep 0.8
    {Are you sure you want to continue connecting (yes/no/\[fingerprint\])?}
    {
        exp_send "yes\r"
        exp_continue
    }
    {password:} {send "$password\r"}    
}
expect eof

I want to get the initial prompt, given when my machine has never connected to the remote server. Thus, I have tried both removing the added key inside known_hosts or that whole file in my machine. However, I cannot get the initial prompt anymore. This doesn't happen on another machine. Either method gives me the initial prompt as expected. Why is this happening? What can I do to get what I want?

The outpout of grep -i StrictHostKeyChecking ~/.ssh/config /etc/ssh/ssh_config on all 3 machines is:

grep: /home/ubuntu/.ssh/config: No such file or directory
/etc/ssh/ssh_config:#   StrictHostKeyChecking ask
4
  • What is the OS? Assuming it's Linux, what is the output of grep -i StrictHostKeyChecking ~/.ssh/config /etc/ssh/ssh_config? Please edit the question and add information. Feb 24, 2023 at 21:59
  • @KamilMaciorowski, done!
    – CaTx
    Feb 25, 2023 at 5:36
  • (1) StrictHostKeyChecking is commented out, but the default value is ask anyway. OK. (2) "I have tried both removing the added key inside known_hosts or that whole file in my machine." – Check man 5 ssh_config, GlobalKnownHostsFile and UserKnownHostsFile. Not only one can use another file; one can use many files (the default setup uses four in total). So maybe you altered the wrong file or there is another file where information survived. Please investigate. Feb 25, 2023 at 6:18
  • 1
    I figured it out! Thank you so much.
    – CaTx
    Feb 26, 2023 at 4:12

1 Answer 1

1

Found the culprit! UserKnownHostsFile default values are ~/.ssh/known_hosts and ~/.ssh/known_hosts2. I renamed the original known_hosts into known_hosts2 for comparison/debugging purpose. Renaming it to known_hosts3 triggers the initial prompt again.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .