Use WinSCP to transfer log files remotely using Javascript
By: Eskalin in Javascript Tutorials on 2012-10-24
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()); }
Add Comment
This policy contains information about your privacy. By posting, you are declaring that you understand this policy:
- Your name, rating, website address, town, country, state and comment will be publicly displayed if entered.
- Aside from the data entered into these form fields, other stored data about your comment will include:
- Your IP address (not displayed)
- The time/date of your submission (displayed)
- Your email address will not be shared. It is collected for only two reasons:
- Administrative purposes, should a need to contact you arise.
- To inform you of new comments, should you subscribe to receive notifications.
- A cookie may be set on your computer. This is used to remember your inputs. It will expire by itself.
This policy is subject to change at any time and without notice.
These terms and conditions contain rules about posting comments. By submitting a comment, you are declaring that you agree with these rules:
- Although the administrator will attempt to moderate comments, it is impossible for every comment to have been moderated at any given time.
- You acknowledge that all comments express the views and opinions of the original author and not those of the administrator.
- You agree not to post any material which is knowingly false, obscene, hateful, threatening, harassing or invasive of a person's privacy.
- The administrator has the right to edit, move or remove any comment for any reason and without notice.
Failure to comply with these rules may result in being banned from submitting further comments.
These terms and conditions are subject to change at any time and without notice.
Comments