Batch script to Secure FTP and mail notification

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
herschel
Posts: 2
Joined: 04 May 2011 20:39

Batch script to Secure FTP and mail notification

#1 Post by herschel » 04 May 2011 20:42

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 !!!

akeyalee
Posts: 1
Joined: 19 May 2011 23:37

Re: Batch script to Secure FTP and mail notification

#2 Post by akeyalee » 23 May 2011 05:03

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
Last edited by akeyalee on 25 May 2011 01:51, edited 1 time in total.

herschel
Posts: 2
Joined: 04 May 2011 20:39

Re: Batch script to Secure FTP and mail notification

#3 Post by herschel » 23 May 2011 11:39

Why are you posting this query here ???? plz dont divert the topic

srikarshiva
Posts: 2
Joined: 06 Jun 2011 23:27

Re: Batch script to Secure FTP and mail notification

#4 Post by srikarshiva » 06 Jun 2011 23:30

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

Cleptography
Posts: 287
Joined: 16 Mar 2011 19:17
Location: scriptingpros.com
Contact:

Re: Batch script to Secure FTP and mail notification

#5 Post by Cleptography » 06 Jun 2011 23:47

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

srikarshiva
Posts: 2
Joined: 06 Jun 2011 23:27

Re: Batch script to Secure FTP and mail notification

#6 Post by srikarshiva » 07 Jun 2011 00:17

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

Cleptography
Posts: 287
Joined: 16 Mar 2011 19:17
Location: scriptingpros.com
Contact:

Re: Batch script to Secure FTP and mail notification

#7 Post by Cleptography » 07 Jun 2011 00:23

@srikarshiva
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

Sarin Samuel
Posts: 1
Joined: 09 Feb 2015 06:59

Re: Batch script to Secure FTP and mail notification

#8 Post by Sarin Samuel » 09 Feb 2015 07:01

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.

Post Reply