Search code examples
ioswindowsbashauthenticationscp

How to add carriage return to bash when prompted for SCP password to a remote host?


Trying to automate a file copy from a MAC to a PC running Solarwinds SCP server. The server is configured to allow Anonymous login. However, by default it prompts for a password. Pressing enter manually copies the file successfully.

Trying a simple command:

scp myfile.txt 192.168.0.188:/myfile.txt
password:
{carriage return entered}
Output: myfile.txt                                 100%  432    43.8KB/s   00:00

On the MAC I have tried to use generated public keys copied it to the appropriate folder on the PC but that doesn't work. I've also experimented with different Bash scripts but cannot find the correct solution to send a carriage return at the password prompt.


Solution

  • Try using expect in your shell script:

    -D is for debugging, change to 1 to get more information

    expect -D 0 -c "
    spawn scp User@destingation
    set ret 1
    set timeout 20
    expect {
        timeout {
            send_user \"Timeout reached!\"
            exit \$ret
        }
        eof {
            puts \"End of test connection reached!\"
            if { \$ret == 0 } {
              puts \"Connection test Successful!\"
              puts \"Exiting $destination ...\"
            } else {
              puts \"Connection Failure!\"
            }
            exit \$ret
        }
        \"assword:\" {
            puts \"\r\nSending password\"
            send \"\r\"
            exp_continue
        }
        \"sftp>\" {
            send \"put \\\"file name\\\"\r\"
            set ret 0
            exp_continue
        }
    }"
    
    # get the exit value from expect statement above
    exit_val=$?