I want to monitor our SFTP to send email to us if a file is added. For now I tried to make a condition with if/else with a batch script, but the batch environment does not accept my condition.
I am new with batch and automation, so what I tried to do is synchronise the SFTP file with a local file in first place and run a batch schedule to try to synchronise again; if it does then it is going to send na email (I did not make the script for the email at the moment and to be honest I do not know how to do so for now), if it did not synchronise then exit script.
Here is my script:
option batch on
option confirm off
open sftp://x#sftp.x/ -privatekey=privateKey.ppk -hostkey="ssh-rsa 2048 x"
option transfer binary
if synchronize local "C:\Users\Administrateur\Desktop\x\x" "/x/x/rx" (
ECHO nouveau fichier ajouter au repertoir
)
else (ECHO aucun nouveau fichier exit
)
Here is the error:
Commande inconnue 'if'.
Add -preview switch to your synchronize command to make it only check for changes, instead of actually synchronizing the files.
Add option failonnomatch on to your script to make the synchronize command report no differences as an error.
option failonnomatch on
synchronize -preview local "C:\Users\Administrateur\Desktop\x\x" "/x/x/rx"
In the actual batch file, check WinSCP exit code to determine, if there are any differences or not. Something like this:
winscp.com /script=script.txt /log=script.log
if errorlevel 1 (
echo Nothing to synchronize or other problem
) else (
echo There are files to synchronize
)
If you want to send an email, see WinSCP guide for Emailing script results.
Related
I have a backup script that works. I won't put in the entire script, because I don't think it's necessary, but it looks like this:
set backup_dir=R:\__MYBACKUPS
REM *** <<< Right here, I want to have a command that says "Mount everything you know how to mount"
if not exist %backup_dir% (
echo The backup directory does not exist.
echo Making the backup directory %backup_dir%
)
call :get_datetime_stamp
set BACKUPNAME=%backup_dir%\bak%DATEANDTIME%.zip
set default_to_backup="c:\Users\%username%\documents\*.doc*" "c:\Users\%username%\documents\*.csv" "c:\Users\%username%\documents\*.xls*" "c:\Users\%username%\documents\*.pdf" ^
"c:\Users\%username%\documents\*.ppt*" "c:\Users\%username%\documents\*.txt"
set what_to_backup=%default_to_backup% "c:\Users\%username%\etc"
set zip_program="c:\Program Files\winzip\winzip64.exe"
set zip_parameters=-min -a -r
echo EXECUTING THE FOLLOWING BACKUP COMMAND:
echo %zip_program% %zip_parameters% %BACKUPNAME% %what_to_backup%
%zip_program% %zip_parameters% %BACKUPNAME% %what_to_backup%
It works fine, if I mount the R drive. The way I normally do this is when I login I manually go to the explorer window and just click on the R drive ... and that action magically "connects" the R shared network drive. HOWEVER, what I would prefer to do is have the script "mount the drive." I don't want to go in and click all the network drives to connect them. I don't want to specify all the connection garbage. I just want to tell windows, "Mount everything that you already know how to mount."
This is all information that our computer services people have already figured out and configured on all our machines.
I have a .bat file. If I run it manually (double click), it works. If I schedule it, it doesn't.
I tried solutions from similar questions on StackOverflow but none of them is working. Like:
Batch not scheduled
WIN 10 task scheduler
I changed general options.
Run no matter user connection
I changed the user starting the scheduled task.
Mine, BTW, has maximum permissions and when double clicked, it works.
I allowed maximum permissions.
Modified "Add argument" and "Start in"
I set no conditions in the "conditions" tab
I'm new to scripting, so it is possible I set something wrong.
That's my code:
#echo off
"C:\Program Files (x86)\WinSCP\winscp.com" /log=winscp.log /ini=nul /command ^
"open sftp://user:password#mySite.com -hostkey=""ssh-rsa 4096 xxxxxxxxx/yyyyw=""" ^
"option batch" ^
"option transfer binary" ^
"synchronize remote -filemask=""*.png>=2016-01-01<4M;*.jpg>=2016-01-01<4M "" \\FromFolder /destination/ -nopreservetime"
"exit"
If I put md C:\Temp & echo %TIME%>C:\Temp\Test.txt as 2° row, it writes the Test.txt file. If i put it in the end of the file it doesn't. No problems if i run it by double click.
I need the task to run the .bat file just as if I am double clicking it. I don't know if is it possible to open the shell or run it in background, as both of them are good for me. Let me know.
Thanks for suggestions.
Edit_1)
I removed the echo %TIME% >> C:\Temp\Test.txt and kept the "exit" >> C:\Temp\Test.txt .
If I double click my bat file, this is the log:
Host Search ...
Host connection ...
Authentication ...
Use username "xxxx".
Authentication with preset password
Authenticated.
Starting session ...
Session started.
Active session: [1] xxx#site.com
batch abort
transfer binary
Comparison...
Local 'file\MyFolder1' => remote '/ images'
Nothing to synchronize
Comparison...
Local 'H:\MyPath\MyFolder2' => remote '/ images'
Synchronization...
Local 'H:\MyPath\MyFolder2' => remote '/ images'
H:\MyPath\MyFolder2 image.JPG | 617 KB | 50.4 KB / s | binary | 100%
And it works perfectly.
If i run it from schedule (using my same user)
this is the log.
Host Search ...
Host connection ...
Authentication ...
Use username "xxxx".
Authentication with preset password
Authenticated.
Starting session ...
Session started.
Active session: [1] xxx#site.com
batch abort
transfer binary
Comparison...
Local 'file\MyFolder1' => remote '/ images'
Nothing to synchronize
Comparison...
Local 'H:\MyPath\MyFolder2' => remote '/ images'
Folder List Error Request 'H:\MyPath\MyFolder2\*.*'.
Error retrieving file list for "H:\MyPath\MyFolder2\*.*'.
System error. Code: 3.
The specified path could not be found
(I) nterrupt, (R) etry, (S) top: Stop
Folder List Error Request 'H:\MyPath\MyFolder2\*.*'
Error retrieving file list for "H:\MyPath\MyFolder2\*.*'
System error. Code: 3.
The specified path could not be found
I schedule the bat file using my user and max privileges so it should find the folder in the H:\ disk.
WinSCP most probably fails. I would guess that because the batch file working directory is not hat you think and winscp.log cannot be created there. Try using a path like for your debug test: C:\Temp\winscp.log.
The reason why the md C:\Temp & echo %TIME%>C:\Temp\Test.txt does not work at the end of your batch file is the exit command. You probably believe it's interpreted as WinSCP command. But it's not. You are missing ^ after the synchronize command. And additionally, you have a blank space after it. As a consequence, the exit is not part of WinSCP command-line, but it exits the batch file instead. Also the lines have to be indented (though I assume that it's rather due to your post formatting).
Try something like this:
#echo off
md C:\Temp
echo %TIME% > C:\Temp\Test.txt
"C:\Program Files (x86)\WinSCP\winscp.com" /log=C:\temp\winscp.log /ini=nul /command ^
"open sftp://user:password#mySite.com -hostkey=""ssh-rsa 4096 xxxxxxxxx/yyyyw=""" ^
"option batch" ^
"option transfer binary" ^
"synchronize remote -filemask=""*.png>=2016-01-01<4M;*.jpg>=2016-01-01<4M "" \\FromFolder /destination/ -nopreservetime" ^
"exit" >> C:\Temp\Test.txt
echo %TIME% >> C:\Temp\Test.txt
See also WinSCP FAQ Why are some WinSCP scripting commands specified in a batch file not executed/failing?
I need to submit a JCL via FTP.
Wrote below code for it:
open server.com
uname
password
quote site LRECL=80 BLKSIZE=27920 RECFM=FB
literal SITE FILETYPE=JES
GET 'PDS.NAME(JCLNAME)' 'LOCAL\PATH\file.txt'
disconnect
bye
The problem is that even after the job gets completed in spool, it takes around 10 minutes before this script gets completed.
It seems to get stuck at 125 When Job is done, Will retrieve its output.
Maybe i am missing some pre initialization. Please advise.
It looks like the JES system doesn't really know how to process the request. If you take a read of the Steps for submitting a job and automatically receiving output article it explains that in order to get the output of the job automatically, the JCL JOBNAME must be USERIDx. So if my userid is ABC123, then my JOBNAME should be ABC123A. They also recommend a slightly different set up than you. Try this:
open server.com
USERID
password
SITE FILEtype=JES NOJESGETBYDSN
GET 'PDS.NAME(USERIDx)' 'LOCAL\PATH\file.txt'
disconnect
bye
When I tried your FTP commands, I got the same results (waiting for 10 minutes). I think it has to do with the JES interface level and how long the different files are held for. Using the commands above (and using the proper naming), the SYSOUTS will comeback when the job has completed as long as they are in HELD status. If there are some outputs that are not in HELD status, it will skip them.
Confused why you're using 'GET'.
I have always used 'PUT' to submit a JCL via FTP.
Here's a snippet of a batch job I'd use to submit JCL via FTP:
::: -- FTP Compress DOC --
:ftp_COMPRESS
echo.
echo " ----------------------------------- "
echo " COMPRESSING dataset "
echo " --------------------------------------- "
IF EXIST ftptemp.txt del ftptemp.txt
echo user %FTPUserID%>> ftptemp.txt
echo %FTPPwd%>> ftptemp.txt
echo cd ..>> ftptemp.txt
echo cd DATASET>> ftptemp.txt
echo del %filename%>> ftptemp.txt
echo quote site file=jes>> ftptemp.txt
echo put compit.jcl>> ftptemp.txt
echo quote site file=seq
echo quit>> ftptemp.txt
ftp -n -s:ftptemp.txt %host%
pause
Here's the JCL job on my local machine saved as compit.jcl, same directory as .bat:
//COMPIT JOB CARD
//*-------------------------------------------------------------
//COMPRESS EXEC PGM=IEBCOPY
//SYSPRINT DD SYSOUT=*
//LIB DD DSN=DATASET,DISP=OLD
//SYSIN DD *
COPY INDD=LIB,OUTDD=LIB
/*
The job itself was a quick way I'd compress a dataset before uploading more members to it.
I am writing a batch command to send data via FTP. Before sending the actual data I need to find if the FTP server is active/running. How do I check that in batch command?
The server responds with "220 server ready" message when it is connected.
Do something like this:
YourFTPCommand | find /i /v "220 server ready" && goto :ServerNotReady
Explanation:
Pipe the output of your FTP command to FIND
Do a case insensitive (/i) search for output that does not contain (/v) the string "220 server ready"
Go to someplace if such a string is found (&&)
I do not think there's a reliable way to do this with Windows ftp.exe.
It blindly keeps running the commands, no matter if connection or previous commands succeeded.
It won't even report the result via exit code.
All you can do is to parse the ftp.exe output.
You should better use a 3rd-party FTP client.
For example with WinSCP scripting you can use:
#echo off
winscp.com /log=ftp.log /command ^
"open ftp://user:password#example.com/" ^
"put c:\local\path\file.txt" ^
"exit"
If connection (the open command) fails, WinSCP won't execute the following commands (the put).
See also Converting Windows FTP script to WinSCP FTP script.
(I'm the author of WinSCP)
I have to call two bat files.
One name cbpp_job and other upload.bat.
In first .bat files, I have called cbppservice.exe and after that I have call upload.bat.
cbpp_job.bat
call d:\csdb_exe\CBPPService.exe
call ftp -n -s:"d:\csdb\Success\upload.bat" xxxx.produrl.com
upload.bat
user XXXXXX
XXXXXXXXX
PUT ZA1P.FTP.CBPP.INTRFACE.GRP(+1) 'ZA1P.FTP.CBPP.INTRFACE.GRP(+1)'
BYE
EXIT
But when I call csdb_job through command prompt it works well. When I scheduled it in task scheduler it only calls cbppservice.exe and it is not doing the ftp.
The operating System is windows server 2008.
If your event viewer doesn't show you why your script is failing, Try modifying cbpp_job.bat to redirect stderr to a log file.
(
d:\csdb_exe\CBPPService.exe
ftp -n -s:"d:\csdb\Success\upload.bat" xxxx.produrl.com
) 2>"c:\csdbtask.log"
Maybe that'll help you figure out why task scheduler is failing.