我想在nodejs中使用ssh2实现一个sshd服务器,它可以由ssh
访问,但它不能由scp
访问,这是我的代码:
_clientConnectCb = (client)->
logger.info 'Client connected!'
_code = null
_user = null
_password = null
_hostPort = null
_clientShell = undefined;
client.on 'authentication', (ctx)->
if ctx.method is 'password'
_tmp = ctx.username.split('_')
_code = _tmp[0]
_user = _tmp[1]
if _tmp.length != 2
ctx.reject()
return
util.GetCodeInfo(_code, true, (err, info)->
if not err and not info.err and info
_hostPort = info.data
_password = ctx.password
logger.info 'user accept', _user, info
ctx.accept()
else
logger.warn err, info
ctx.reject()
)
else
ctx.reject(["password"])
client.on 'ready', ->
logger.info 'Client authenticated!'
rows = undefined;
cols = undefined;
width = undefined;
height = undefined;
term = undefined;
targetShell = undefined;
client.on 'session', (accept, reject)->
session = accept()
session.once("exec", (accept, reject, info)->
// when access by scp, 'exec' event will emit, but what to do next
console.log('Client wants to execute: ', info.command);
stream = accept();
client.on 'end', ->
logger.info 'Client disconnected'
client.on 'error', (err)->
logger.info 'client failed:', err
server = new Server {privateKey: fs.readFileSync(__dirname + "/ssh-rsa/#{config.rsaKey}")}, _clientConnectCb
server.listen config.port, '0.0.0.0', ->logger.info 'ssh proxy on port ' + @address().port
答案 0 :(得分:2)
您已获得所请求命令的stream
。您可以根据自己的需要做出回应。
您可以使用child_process.spawn()
中的命令字符串调用info.command
,然后在该子进程和stream
之间进行管道传输。但是,您应该注意验证/保护info.command
并确保它们在用户登录的用户下执行等。