当提示您将SCP密码发送到远程主机时,如何将回车符添加到bash?

问题描述 投票:0回答:2

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

尝试一个简单的命令:

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脚本,但是找不到正确的解决方案来在密码提示符下发送回车。

ios windows bash authentication scp
2个回答
1
投票

尝试在您的shell脚本中使用expect

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

0
投票

使用您的部分建议和以前的帖子(How can I fix my expect script for scp download?)解决了。看来我没有正确缩进。非常感谢!

© www.soinside.com 2019 - 2024. All rights reserved.