Batch script to Secure FTP and mail notification
Moderator: DosItHelp
Batch script to Secure FTP and mail notification
All:have the following requirement:
Need to write a batch script to securely FTP a file from Windows to Unix. This script will run on Windows. The script should also send mail notification post processing
Assumptions:
- winscp is used for secure file transfer
- Mail is sent using a standard w
indows client(Not Outlook exchange)
- File name is fixed
The flow that I am thinking is:
Send mail that the FTP is started
SFTP file to Unix
Archive the file
Send mail that the FTP is complete
i want to create a matured script with error handling(great if could be achieved with functions/routines since it looks structured)
Please pass me on the sample that you may have.
Thanks in Advance !!!
Need to write a batch script to securely FTP a file from Windows to Unix. This script will run on Windows. The script should also send mail notification post processing
Assumptions:
- winscp is used for secure file transfer
- Mail is sent using a standard w
indows client(Not Outlook exchange)
- File name is fixed
The flow that I am thinking is:
Send mail that the FTP is started
SFTP file to Unix
Archive the file
Send mail that the FTP is complete
i want to create a matured script with error handling(great if could be achieved with functions/routines since it looks structured)
Please pass me on the sample that you may have.
Thanks in Advance !!!
Re: Batch script to Secure FTP and mail notification
How Do I copy files Directly from ftp to another ftp, without downloading them to my pc ? i have 2 ftp accounts, and i wanna copy backup files from one ftp to another, but there are many files, and downloading them to my pc then uploading them to the second ftp would take alot of time, therefor i wanna know how to copy files directly from ftp to another.
___________________________
market samurai ~ marketsamurai
___________________________
market samurai ~ marketsamurai
Last edited by akeyalee on 25 May 2011 01:51, edited 1 time in total.
Re: Batch script to Secure FTP and mail notification
Why are you posting this query here ???? plz dont divert the topic
-
- Posts: 2
- Joined: 06 Jun 2011 23:27
Re: Batch script to Secure FTP and mail notification
Hi Hershcel,
I also require same kind of script. So, can you share me if you find the solution for your below mentioned question
Regards,
Srikar.Shiva
I also require same kind of script. So, can you share me if you find the solution for your below mentioned question
Regards,
Srikar.Shiva
-
- Posts: 287
- Joined: 16 Mar 2011 19:17
- Location: scriptingpros.com
- Contact:
Re: Batch script to Secure FTP and mail notification
akeyalee wrote:How Do I copy files Directly from ftp to another ftp, without downloading them to my pc ? i have 2 ftp accounts, and i wanna copy backup files from one ftp to another, but there are many files, and downloading them to my pc then uploading them to the second ftp would take alot of time, therefor i wanna know how to copy files directly from ftp to another.
A quick and dirty example on how to automate ftp in a bash script:
Code: Select all
#!/bin/bash
/bin/ftp -inv ip_address_of_target<<ENDFTP
user username user_password
cd folder_on_target_station
bin
lcd folder_on_local_box
put filename.txt
bye
ENDFTP
You should replace all of the filenames, addresses and paths for your particular server.
This is a more verbose example I found, study it to understand what's going on....
Code: Select all
#!/bin/bash
### getFTP v.1 #################
#
# Variables : use backquotes!#
DATE=`date +%Y%m%d`
HOME='/home/itdev/scripts/mepco'
#
HOST='remotehost'
USER='myUser'
PASSWD='myPasswd'
FILE='*.txt'
#
####################################
# Make directory of current date, make that directory local
mkdir $HOME/$DATE
cd $HOME/$DATE
# Login, run get files
ftp -inv $HOST <<END_SCRIPT
quote USER $USER
quote PASS $PASSWD
mget $FILE
!ls > $DATE.list
bye
END_SCRIPT
for file in `cat $DATE.list`
do
ftp -inv $HOST <<END_SCRIPT
quote USER $USER
quote PASS $PASSWD
rm $file
bye
END_SCRIPT
done
# Cleanup
exit 0
You could also use CoreFTP its free. http://www.coreftp.com/download.html
...and take a look at http://www.unix.com/answers-frequently-asked-questions/14020-automate-ftp-scripting-ftp-transfers.html
-
- Posts: 2
- Joined: 06 Jun 2011 23:27
Re: Batch script to Secure FTP and mail notification
Hi,
I am using DOS Script to transfer files using WinSCP exe.
Script:
@ECHO OFF
> sfput.sftp ECHO open <user>:<pw>@<ip>:22
>>sfput.sftp ECHO cd
>>sfput.sftp ECHO lcd C:\Upload\Ready
>>sfput.sftp ECHO option confirm off
>>sfput.sftp ECHO put *.xml
>>sfput.sftp ECHO exit
:: Use the temporary script for unattended SFTP
winscp /console /script=sfput.sftp > sfput.log
DEL sfput.sftp
CD /D C:\Upload\Ready
MOVE F*.txt C:\Upload\Done
I want to add one scenario like IF *.xml files doesnot exist then it is became a problem to me. Console requires some input. How to avoid that?
How should i introduce mailing concept in the above script.
Regards,
Srikar.Shiva
I am using DOS Script to transfer files using WinSCP exe.
Script:
@ECHO OFF
> sfput.sftp ECHO open <user>:<pw>@<ip>:22
>>sfput.sftp ECHO cd
>>sfput.sftp ECHO lcd C:\Upload\Ready
>>sfput.sftp ECHO option confirm off
>>sfput.sftp ECHO put *.xml
>>sfput.sftp ECHO exit
:: Use the temporary script for unattended SFTP
winscp /console /script=sfput.sftp > sfput.log
DEL sfput.sftp
CD /D C:\Upload\Ready
MOVE F*.txt C:\Upload\Done
I want to add one scenario like IF *.xml files doesnot exist then it is became a problem to me. Console requires some input. How to avoid that?
How should i introduce mailing concept in the above script.
Regards,
Srikar.Shiva
-
- Posts: 287
- Joined: 16 Mar 2011 19:17
- Location: scriptingpros.com
- Contact:
Re: Batch script to Secure FTP and mail notification
@srikarshiva
You can use jscript to accomplish this.
Run the script with command:
Following batch file shown how to continue with processing based on file exitence:
You can use jscript to accomplish this.
Code: Select all
// Configuration
// Remote file search for
var FILEPATH = "/home/user/myfile.dat";
// Session to connect to
var SESSION = "session";
// Path to winscp.com
var WINSCP = "c:\\program files\\winscp\\winscp.com";
var filesys = WScript.CreateObject("Scripting.FileSystemObject");
var shell = WScript.CreateObject("WScript.Shell");
var logfilepath = filesys.GetSpecialFolder(2) + "\\" + filesys.GetTempName() + ".xml";
var p = FILEPATH.lastIndexOf('/');
var path = FILEPATH.substring(0, p);
var filename = FILEPATH.substring(p + 1);
var exec;
// run winscp to check for file existence
exec = shell.Exec("\"" + WINSCP + "\" /log=\"" + logfilepath + "\"");
exec.StdIn.Write(
"option batch abort\n" +
"open \"" + SESSION + "\"\n" +
"ls \"" + path + "\"\n" +
"exit\n");
// wait until the script finishes
while (exec.Status == 0)
{
WScript.Sleep(100);
WScript.Echo(exec.StdOut.ReadAll());
}
if (exec.ExitCode != 0)
{
WScript.Echo("Error checking for file existence");
WScript.Quit(1);
}
// look for log file
var logfile = filesys.GetFile(logfilepath);
if (logfile == null)
{
WScript.Echo("Cannot find log file");
WScript.Quit(1);
}
// parse XML log file
var doc = new ActiveXObject("MSXML2.DOMDocument");
doc.async = false;
doc.load(logfilepath);
doc.setProperty("SelectionNamespaces",
"xmlns:w='http://winscp.net/schema/session/1.0'");
var nodes = doc.selectNodes("//w:file/w:filename[@value='" + filename + "']");
if (nodes.length > 0)
{
WScript.Echo("File found");
// signalize file existence to calling process;
// you can also continue with processing (e.g. downloading the file)
// directly from the script here
WScript.Quit(0);
}
else
{
WScript.Echo("File not found");
WScript.Quit(1);
}
Run the script with command:
Code: Select all
cscript /nologo example.js
Following batch file shown how to continue with processing based on file exitence:
Code: Select all
cscript /nologo example.js
if errorlevel 1 goto error
echo File exists, do something
rem Do something
exit
:error
echo Error or file not exists
-
- Posts: 1
- Joined: 09 Feb 2015 06:59
Re: Batch script to Secure FTP and mail notification
FTP Manager Lite is a better freeware FTP and SFTP client software. The clean interface is super easy to use. It will perform your files transfers quickly and efficiently. It is one of the best freeware FTP clients that also supports FXP.