Use WinSCP to transfer log files remotely using Javascript

By: Eskalin Emailed: 1643 times Printed: 2109 times    

Latest comments
By: rohit kumar - how this program is work
By: Kirti - Hi..thx for the hadoop in
By: Spijker - I have altered the code a
By: ali mohammed - why we use the java in ne
By: ali mohammed - why we use the java in ne
By: mizhelle - when I exported the data
By: raul - no output as well, i'm ge
By: Rajesh - thanx very much...
By: Suindu De - Suppose we are executing

Normally winscp program is used to connect to a remote machine and transfer files. Recently I wanted to connect to my linux machine from a windows machine to download a file automatically everyday. The linux machine was generating daily log files and the name of the log file keeps changing as the current date. So i wrote this script to connect to that machine remotely and download the file every day by running the script in a scheduler.

Save the below as test.js

Change the path to the winscp.com file based on where you installed your winscp program,

Then run the file as

cscript /nologo test.js

 

//Scripts starts from here.

// Local path to download to (keep trailing slash)
var LOCALPATH = "d:\\NASShare\\C5\\billlog\\";
// Remote path to download from (keep trailing slash)
var REMOTEPATH = "/var/log/sipproxyd/billing/";
// Session to connect to
var SESSION = "root:siproot@10.92.1.115";
// Path to winscp.com
var WINSCP = "c:\\program files\\winscp\\winscp.com";
// helper function to pad zeroes to the left of number
function pad(n, len)
{
    var s = n.toString();
    while (s.length < len)
    {
        s = '0' + s;
    }
    return s;
}
var date = new Date();
// format timestamp
var stamp =
    pad(date.getFullYear(), 4) + '-' +
    pad(date.getMonth(), 2) +  '-' +
    pad(date.getDate(), 2);
// File to download
var FILE = "billing.log." + stamp;
var shell = WScript.CreateObject("WScript.Shell");
//WScript.echo(FILE);
run winscp to download the file into timestamped-filename
exec = shell.Exec("\"" + WINSCP + "\"");
exec.StdIn.Write(
    "option batch abort\n" +
    "open \"" + SESSION + "\"\n" +
    "get \"" + REMOTEPATH + FILE + "\" \"" + LOCALPATH + "\"\n" +
    "exit\n");
// wait until the script finishes
while (exec.Status == 0)
{
    WScript.Sleep(100);
    WScript.Echo(exec.StdOut.ReadAll());
}


Javascript Home | All Javascript Tutorials | Latest Javascript Tutorials

Sponsored Links

If this tutorial doesn't answer your question, or you have a specific question, just ask an expert here. Post your question to get a direct answer.



Bookmark and Share

Comments(0)


Be the first one to add a comment

Your name (required):


Your email(required, will not be shown to the public):


Your sites URL (optional):


Your comments:



More Tutorials by Eskalin
365 office login (office 365 login)
Configure SSO for a domain with ADFS - Convert a domain to a federated domain in Office 365
What is Office 365 Midsize Business Plan?
What is Office 365 Small Business Premium?
What is Ofice 365 Home Premium
Migration from on-premise exchange 2003 to Office 365
Use WinSCP to transfer log files remotely using Javascript
Mac with Google Apps

More Tutorials in Javascript
Use WinSCP to transfer log files remotely using Javascript
Verifying user input in JavaScript
Pause execution in JavaScript
Javascript to display client date and time on webpage
Getting Browser's height and width using Javascript
Highlighting text on a page using CSS
Using revealTrans to do page transitions in Javascript
Password protect a web page using Javascript
Diabling Right Click option in a browser using Javascript
Scrolling message on the status bar using Javascript
window.frames[i] in Javascript
Form validation using Javascript
Using && in Javascript
The if statement in Javascript
do-while, while, for and for-in loops in Javascript

More Latest News
Most Viewed Articles (in Javascript )
Typecasting in Javascript
Using parseInt() and parseFloat() in JavaScript to convert data types to Numbers
Show how many characters remaining in a html text box using javascript
List of Keywords and reverved words in Javascript
Scrolling message on the status bar using Javascript
Javascript Basics
Getting Browser's height and width using Javascript
History and evolution of Javascript
The if statement in Javascript
do-while, while, for and for-in loops in Javascript
switch in Javascript
call() and apply() methods in Javascript
Pause execution in JavaScript
Bitwise NOT, AND, OR, XOR operators in Javascript
Using ! and !! in Javascript
Most Emailed Articles (in Javascript)
Diabling Right Click option in a browser using Javascript
Click to remove an element from a select html form using javascript
Bitwise NOT, AND, OR, XOR operators in Javascript
this keyword sample in Javascript
Getting Browser's height and width using Javascript
Show how many characters remaining in a html text box using javascript
History and evolution of Javascript
Using toString() in JavaScript to convert data types to String
Using parseInt() and parseFloat() in JavaScript to convert data types to Numbers
Using ! and !! in Javascript
Using && in Javascript
Highlighting text on a page using CSS
Using revealTrans to do page transitions in Javascript
Javascript to display client date and time on webpage
Verifying user input in JavaScript