Use WinSCP to transfer log files remotely using Javascript
By: Eskalin
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:[email protected]";
// 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());
}
Archived Comments
Comment on this tutorial
- Data Science
- Android
- AJAX
- ASP.net
- C
- C++
- C#
- Cocoa
- Cloud Computing
- HTML5
- Java
- Javascript
- JSF
- JSP
- J2ME
- Java Beans
- EJB
- JDBC
- Linux
- Mac OS X
- iPhone
- MySQL
- Office 365
- Perl
- PHP
- Python
- Ruby
- VB.net
- Hibernate
- Struts
- SAP
- Trends
- Tech Reviews
- WebServices
- XML
- Certification
- Interview
categories
Related Tutorials
Use WinSCP to transfer log files remotely using Javascript
Verifying user input 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
Scrolling message on the status bar using Javascript
Diabling Right Click option in a browser using Javascript
Password protect a web page using Javascript
Using revealTrans to do page transitions in Javascript
Form validation using Javascript
window.frames[i] in Javascript
Math object and Math functions in Javascript