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

jacknad
  • 13,483
  • 40
  • 124
  • 194
IceBlackz
  • 11
  • 3
  • You have heard of [`snprintf`](http://en.cppreference.com/w/c/io/fprintf)? No need for all those `strcat` calls. – Some programmer dude May 08 '14 at 12:39
  • Ah, thats what I've been looking for for far too long.. That should clear up a lot of my code! – IceBlackz May 08 '14 at 13:10
  • OT: Is this part of a bigger application? If not using a shell script might be more straight forward to do the job. – alk May 08 '14 at 13:42
  • @alk: It is indeed part of a bigger application, just a small part of it actually. However, it could be possible to make the FTP synchronization function an external program – IceBlackz May 09 '14 at 08:44

0 Answers0