Skip to content

Instantly share code, notes, and snippets.

@shizuka-na-kazushi
Created January 17, 2021 03:29
Show Gist options
  • Save shizuka-na-kazushi/e964d09408a1cc90f62f0bfb577de8ea to your computer and use it in GitHub Desktop.
Save shizuka-na-kazushi/e964d09408a1cc90f62f0bfb577de8ea to your computer and use it in GitHub Desktop.
Nodejsのnode-ftpクライアントモジュールを使ったftpクライアントのサンプル
var readline = require('readline');
var Client = require('ftp');
var fs = require('fs');
const { exec } = require('child_process');
var ready = false;
var c = new Client();
c.on('ready', function() {
ready = true;
});
c.on('error', function (err) {
console.log(('error occurs:' + err));
c.end();
});
c.connect(
{
host: 'localhost',
port: 7002,
user: 'xxxx',
password: 'yyyy'
}
);
var rl = readline.createInterface({input: process.stdin, output: process.stdout, prompt: '> '});
function command_cd(args) {
c.cwd(args[1], function(err, currentDir) {
if (err) {
console.log('error: ' + err.message);
} else {
console.log(`current dir: ${currentDir}`);
}
});
};
function command_bin(args) {
c.binary(function(err) {
if (err) {
console.log('error: ' + err.message);
} else {
console.log('binary mode.');
}
});
};
function command_ls(args) {
c.list(function(err, list) {
if (err) {
console.log('error: ' + err.message);
} else {
console.log(list);
list.forEach((i) => {
var outline = `${i.type}${i.rights.user}${i.rights.group}${i.rights.other} `;
outline += `${i.owner} ${i.group}`;
outline += ` ${i.size} `;
outline += i.date.toLocaleDateString() + ' ';
outline += i.name;
console.log(outline);
});
}
});
};
function command_mkdir(args) {
c.mkdir(args[1], function(err) {
if (err) {
console.log('error: ' + err.message);
} else {
console.log('mkdir ok');
}
});
};
function command_pwd(args) {
c.pwd(function(err, cwd) {
if (err) {
console.log('error: ' + err.message);
} else {
console.log(`"${cwd}" is current directory`);
}
});
};
function command_get(args) {
c.get(args[1], function(err, stream) {
if (err) {
console.log('error: ' + err.message);
} else {
stream.pipe(fs.createWriteStream(args[1]));
console.log('done');
}
});
};
function command_exec_local(args) {
exec(args.join(' '), (error, stdout, stderr) => {
if (error) {
console.log(error);
} else {
console.log(stdout ? stdout : stderr);
}
});
}
function command_unknown(args) {
if (args[0].indexOf('!') == 0) {
args[0] = args[0].substr(1);
return command_exec_local(args);
} else {
console.log('<<<<unknown>>>> [' + args + ']');
}
}
function command_lcd(args) {
try {
process.chdir(args[1]);
console.log('Local directory now ' + args[1]);
} catch (e) {
console.log(e.message);
}
}
var commands = [
{cmd: 'cd', func: command_cd},
{cmd: 'ls', func: command_ls},
{cmd: 'bin', func: command_bin},
{cmd: 'mkdir', func: command_mkdir},
{cmd: 'pwd', func: command_pwd},
{cmd: 'lcd', func: command_lcd},
{cmd: 'get', func: command_get},
];
rl.on('line', (line) => {
var args = line.split(/ +/);
var func = command_unknown;
commands.forEach(function(val) {
if (val.cmd == args[0]) {
func = val.func;
return;
}
});
func.call(this, args);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment