1

I'm using sshpass to make scp transfers non-interactive. It gathers all files from a folder and tries to sync them to a SFTP server. I need to check whether the transfer was successful or not, so I'm using an popen() to give the command. However, on failure I do see error messages appear in the terminal in which I run my application, but my code somehow fails to capture that output.. Here's my code:

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);

I first build a string to give the proper sshpass/scp command, then I execute it with popen. The if statement seems to fail, when I print 'buf' after the failure it still contains the command for sshpass, like fgets somehow just does nothing?

Thanks for your help

4

0 回答 0