我使用sshpass使scp传输非交互式。它从文件夹中收集所有文件,并尝试将它们同步到SFTP服务器。我需要检查传输是否成功,所以我使用popen()来发出命令。但是,如果失败,我确实会在运行我的应用程序的终端中看到错误消息,但我的代码无法捕获该输出..这是我的代码:
strcat(buf,"sshpass -p "); //Use sshpass to make scp
strcat(buf,ftppass); //non-interactive
strcat(buf," scp -o StrictHostKeyChecking=no -o ConnectTimeout=5 -P ");
strcat(buf,ftpport);
strcat(buf," ");
strcat(buf,locdlog); //Location of logfiles
strcat(buf,arr[j]); //Logname
strcat(buf," ");
strcat(buf,ftpname);
strcat(buf,"@");
strcat(buf,ftpadr);
strcat(buf,":");
strcat(buf,ftpdloc); //Location of logfiles on FTP
strcat(buf,arr[j]);
fp = popen(buf, "r"); //Give actual sync command
if (fgets(buf,sizeof(buf)-1, fp) != NULL)//Check if sync succeeded
printf("%s.\n",buf); //If not, give error
else //Else, move file to backup
{
strcpy(buf,"mv -f ");
strcat(buf,locdlog);
strcat(buf,arr[j]);
strcat(buf," ");
strcat(buf,locdlog);
strcat(buf,"backup/ ");
system(buf);
}
pclose(fp);
我首先构建一个字符串来提供正确的sshpass / scp命令,然后用popen执行它。当我打印“buf”时,if语句似乎失败了。在失败之后,它仍然包含sshpass的命令,就像fgets不知何故什么也没做什么?
感谢您的帮助