首页 > 解决方案 > 当提示输入远程主机的 SCP 密码时,如何在 bash 中添加回车?

问题描述

尝试自动将文件从 MAC 复制到运行 Solarwinds SCP 服务器的 PC。服务器配置为允许匿名登录。但是,默认情况下它会提示输入密码。按回车键手动复制文件成功。

尝试一个简单的命令:

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

在 MAC 上,我尝试使用生成的公钥将其复制到 PC 上的相应文件夹中,但这不起作用。我还尝试了不同的 Bash 脚本,但找不到在密码提示符处发送回车的正确解决方案。

标签: ioswindowsbashauthenticationscp

解决方案


尝试expect在你的 shell 脚本中使用:

-D 用于调试,更改为 1 以获取更多信息

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=$?

推荐阅读