ios - 当提示输入远程主机的 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 脚本,但找不到在密码提示符处发送回车的正确解决方案。
解决方案
尝试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=$?
推荐阅读
- javascript - 如何使用 forEach 从数组创建新数组
- java - BiConsumer 异常测试
- databricks - Databricks 删除通配符
- regex - 如何使用 Grok Patterns 跳过消息的开头?
- clojure - 命名空间未加载到 clojure 基座中
- python - 试图打印打印所在的代码行
- c# - 访问应用程序时是否可以获取上一个站点的url
- asp.net - 关于我在 Visual Studio 中编译源代码时遇到的错误
- python - 从授权的网站加载 json 文档文件
- python - 为什么在 numpy 中某些方法(例如 np.dot)可以用点表示法调用,而其他方法(例如 np.insert)不能?