c++ - 使用 SCP 和 C 或 C++ 进行文件传输

标签 c++ linux scp

以下是我使用 SCP 将文件从本地机器传输到另一台机器的代码,一切正常,但当程序执行“system(exec)”命令时,它要求输入目标用户密码。

有没有办法在 sprintf 语句中使用另一个 '%s' 将密码附加(或添加)到 exec char 数组。

char exec[180];
sprintf(exec,"scp -o StrictHostKeyChecking=no %s/%s %s@%s:/home",current_path,filename,destination_user,dest_ip);

//printf("\n %s \n",exec);
if(system(exec)==0) 
printf("\nFile %s moved successfully\n",file);
else
printf("\nFile %s not moved successfully\n",file);

最佳答案

此处正确的解决方案是使用 RSA 或 DSA 公钥/私钥设置无密码登录。

本网站执行必要的步骤: http://www.linuxproblem.org/art_9.html

简而言之,运行 ssh-keygen -t rsa,并使用默认值,包括“空”密码(否则,系统会要求您输入密码)。 [在你的机器上做这个]

然后使用ssh b@B mkdir -p .ssh创建目标机器的.ssh目录(b@B'对应于远程机器上的远程用户)。

最后将第一步生成的 key 复制到远程机器上的相关文件中。 cat .ssh/id_rsa.pub | ssh b@B 'cat >> .ssh/authorized_keys'

现在你应该可以不用密码登录远程机器了。 (为此,我在家里和工作中都使用它)

关于c++ - 使用 SCP 和 C 或 C++ 进行文件传输,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27395555/

相关文章:

linux - 确定拥有磁盘的 linux 驱动程序

linux - 如何使用 winscp 控制台将文件从远程 Windows 8 服务器传输到 Linux 服务器

linux - 无法连接 ssh sh : 6000: command not found

c++ - 在 Visual Studio 中调用 std::swap 时的 std::bad_function_call

c++ - 使用 drawContours 绘制点序列

linux - linux 排序命令不起作用 "correctly"

linux - Firefox 不打开来自外部来源的链接(例如单击 Skype 中的链接)

bash - 在 GNU 中并行执行 SCP

c++ - 附加到 C++ 中的多态列表?

c++ - 在 C/C++ 预处理器中获取宏参数的不同部分