+++ /dev/null
-#!/usr/bin/env node\r
-\r
-// Require needed modules\r
-var TFTP = require('..'), // The tftp-client module\r
- fs = require('fs'), // Simple wrappers around standard POSIX functions\r
- path = require('path'); // Contains utilities for handling and transforming file paths\r
-\r
-/**********************\\r
-|* Validate arguments *|\r
-\**********************/\r
-\r
-// Too few arguments (node + path_to_this_file + host + cmd + file = 5)\r
-if (process.argv.length < 5) {\r
- // Print usage, and exit\r
- console.log('Needs atleast 3 arguments (you had %d)', process.argv.length-2);\r
- console.log('');\r
- printUsage(true);\r
-}\r
-\r
-// Grab arguments\r
-var host = process.argv[2];\r
-var cmd = process.argv[3];\r
-var file = process.argv[4];\r
-var port = process.argv[5] || 69; // Port defaults to 69 if omitted\r
-\r
-// If cmd is neither read nor write, print usage & exit\r
-if (['read', 'write'].indexOf(cmd)===-1) {\r
- console.log('Command must be read or write (you had %s)', cmd);\r
- printUsage(true);\r
-}\r
-\r
-/*******************\\r
-|* Validation done *|\r
-\*******************/\r
-\r
-// Create the client\r
-var client = new TFTP(port, host);\r
-\r
-var basename = path.basename(file);\r
-\r
-/********\\r
-|* READ *|\r
-\********/\r
-if (cmd == 'read') {\r
- // Read from server\r
- client.read(basename, function(err, data) {\r
- // If error, output some error message\r
- if (err) {\r
- console.error('Oh noes! Error while reading file from tftp server:');\r
- console.error(err);\r
- } else {\r
-\r
- // No error, we got the file, lets write it\r
- \r
- fs.writeFile(file, data, function (err) {\r
- \r
- // If error, output some error message\r
- if (err) {\r
- console.error('Dang it! Error while writing file!');\r
- console.error(err);\r
- } else {\r
-\r
- // No error, the file has been written!\r
-\r
- console.log('File saved (%d bytes)', data.length);\r
- }\r
- });\r
- }\r
- });\r
-}\r
-/*********\\r
-|* WRITE *|\r
-\*********/\r
-else if (cmd == 'write') {\r
- // If file does not exist, print error and usage & exit\r
- if (!fs.existsSync(file)) {\r
- console.log('File (%s) does not exist!', file);\r
- printUsage(true);\r
- }\r
-\r
- // Read file first, then send to server\r
- var data = fs.readFileSync(file);\r
-\r
- client.write(basename, data, function (err, bytes) {\r
- if (err) {\r
- console.error('ERROR:');\r
- console.error(err);\r
- return;\r
- }\r
-\r
- console.log('File sent (%d bytes)', bytes);\r
- });\r
-} else {\r
- console.log('???HOW DID YOU GET HERE???');\r
-}\r
-\r
-/**\r
- * Prints usage - how to use tftp-client\r
- * @param {boolean} exit Wether it sould exit the process\r
- */\r
-function printUsage(exit) {\r
- console.log('Usage:');\r
- console.log(' tftp-client <hostname> (read|write) <filename> [<port>]');\r
- console.log('');\r
- console.log('Example:');\r
- console.log(' tftp-client localhost read 1.txt');\r
-\r
- if (exit===true)\r
- process.exit(0);\r
-}
\ No newline at end of file