3 // Require needed modules
\r
4 var TFTP = require('..'), // The tftp-client module
\r
5 fs = require('fs'), // Simple wrappers around standard POSIX functions
\r
6 path = require('path'); // Contains utilities for handling and transforming file paths
\r
8 /**********************\
\r
9 |* Validate arguments *|
\r
10 \**********************/
\r
12 // Too few arguments (node + path_to_this_file + host + cmd + file = 5)
\r
13 if (process.argv.length < 5) {
\r
14 // Print usage, and exit
\r
15 console.log('Needs atleast 3 arguments (you had %d)', process.argv.length-2);
\r
21 var host = process.argv[2];
\r
22 var cmd = process.argv[3];
\r
23 var file = process.argv[4];
\r
24 var port = process.argv[5] || 69; // Port defaults to 69 if omitted
\r
26 // If cmd is neither read nor write, print usage & exit
\r
27 if (['read', 'write'].indexOf(cmd)===-1) {
\r
28 console.log('Command must be read or write (you had %s)', cmd);
\r
32 /*******************\
\r
33 |* Validation done *|
\r
34 \*******************/
\r
36 // Create the client
\r
37 var client = new TFTP(port, host);
\r
39 var basename = path.basename(file);
\r
44 if (cmd == 'read') {
\r
46 client.read(basename, function(err, data) {
\r
47 // If error, output some error message
\r
49 console.error('Oh noes! Error while reading file from tftp server:');
\r
53 // No error, we got the file, lets write it
\r
55 fs.writeFile(file, data, function (err) {
\r
57 // If error, output some error message
\r
59 console.error('Dang it! Error while writing file!');
\r
63 // No error, the file has been written!
\r
65 console.log('File saved (%d bytes)', data.length);
\r
74 else if (cmd == 'write') {
\r
75 // If file does not exist, print error and usage & exit
\r
76 if (!fs.existsSync(file)) {
\r
77 console.log('File (%s) does not exist!', file);
\r
81 // Read file first, then send to server
\r
82 var data = fs.readFileSync(file);
\r
84 client.write(basename, data, function (err, bytes) {
\r
86 console.error('ERROR:');
\r
91 console.log('File sent (%d bytes)', bytes);
\r
94 console.log('???HOW DID YOU GET HERE???');
\r
98 * Prints usage - how to use tftp-client
\r
99 * @param {boolean} exit Wether it sould exit the process
\r
101 function printUsage(exit) {
\r
102 console.log('Usage:');
\r
103 console.log(' tftp-client <hostname> (read|write) <filename> [<port>]');
\r
105 console.log('Example:');
\r
106 console.log(' tftp-client localhost read 1.txt');
\r