This patch provided by Steve Friedl [steve@unixwiz.net] 2012/05/25 Synopsis: this adds a "-batchfail" parameter to PSFTP, so that if any command in a batch file fails, it will cause that failure to reflect in the PSFTP.EXE exit code. This is useful for automated scripts that need to know if everything worked or not. Patch applies against source 0.62 --- psftp.c-orig Tue Mar 01 23:04:38 2011 +++ psftp.c Fri May 25 17:45:24 2012 @@ -2368,10 +2368,11 @@ } } -void do_sftp(int mode, int modeflags, char *batchfile) +int do_sftp(int mode, int modeflags, char *batchfile) { FILE *fp; int ret; + int sftp_ret = 1; /* assume success */ /* * Batch mode? @@ -2401,7 +2402,7 @@ fp = fopen(batchfile, "r"); if (!fp) { printf("Fatal: unable to open %s\n", batchfile); - return; + return 0; } while (1) { struct sftp_command *cmd; @@ -2413,12 +2414,15 @@ break; if (ret == 0) { if (!(modeflags & 2)) + { + sftp_ret = 0; /* failure in case of -batchfail */ break; + } } } fclose(fp); - } + return sftp_ret; } /* ---------------------------------------------------------------------- @@ -2626,6 +2630,7 @@ printf(" -noagent disable use of Pageant\n"); printf(" -agent enable use of Pageant\n"); printf(" -batch disable all interactive prompts\n"); + printf(" -batchfail Exit with error if any batch cmd fails\n"); cleanup_exit(1); } @@ -2842,6 +2847,8 @@ int mode = 0; int modeflags = 0; char *batchfile = NULL; + int batchfail = 0; + int sftp_success = 1; flags = FLAG_STDERR | FLAG_INTERACTIVE #ifdef FLAG_SYNCAGENT @@ -2892,6 +2899,8 @@ modeflags = modeflags | 1; } else if (strcmp(argv[i], "-be") == 0) { modeflags = modeflags | 2; + } else if (strcmp(argv[i], "-batchfail") == 0) { + batchfail = 1; } else if (strcmp(argv[i], "--") == 0) { i++; break; @@ -2929,7 +2938,7 @@ " to connect\n"); } - do_sftp(mode, modeflags, batchfile); + sftp_success = do_sftp(mode, modeflags, batchfile); if (back != NULL && back->connected(backhandle)) { char ch; @@ -2941,6 +2950,9 @@ cmdline_cleanup(); console_provide_logctx(NULL); sk_cleanup(); + + if (batchfail && !sftp_success) + return 1; return 0; }