我使用ssh2库作为nodejs。
# server.coffee
new ssh2.Server
privateKey: fs.readFileSync './id_rsa'
port: 57037
, (client) ->
console.log 'Client connected'
client.on 'authentication', (ctx) ->
ctx.accept()
.on 'ready', ->
console.log 'Client authenticated'
client.on 'session', (accept, reject) ->
console.log 'session'
session = accept()
console.log 'accept'
console.log session.on
session.on 'shell', (accept, reject, info) ->
console.log('Client wants to execute: ' + inspect(info.command));
stream = accept()
stream.stderr.write 'Error!'
stream.write 'Just kidding'
stream.exit 0
stream.end()
session.on 'error', ->
console.log 'error'
# client.coffee
conn = new Client()
conn.on 'ready', ->
console.log 'Client :: ready'
conn.shell (err, stream) ->
console.log 'shell'
if err then throw err
stream.on 'close', ->
console.log 'Stream :: close'
conn.end()
.on 'data', (chunk) ->
console.log 'STDOUT:', data
.stderr.on 'data', (chunk) ->
console.log 'STDERR:', data
stream.end 'ls -al'
.connect()
当我这样做时,我收到以下错误:
Error: Unable to request a pseudo-terminal
at C:\Users\pv\Proj\test\ssh-test\node_modules\ssh2\lib\client.js:1160:25
at SSH2Stream.<anonymous> (C:\Users\pv\Proj\test\ssh-test\node_modules\ssh2\lib\Channel.js:179:24)
at SSH2Stream.emit (events.js:104:17)
at parsePacket (C:\Users\pv\Proj\test\ssh-test\node_modules\ssh2\node_modules\ssh2-streams\lib\ssh.js:3225:10)
at SSH2Stream._transform (C:\Users\pv\Proj\test\ssh-test\node_modules\ssh2\node_modules\ssh2-streams\lib\ssh.js:552:13)
at SSH2Stream.Transform._read [as __read] (_stream_transform.js:179:10)
at SSH2Stream._read (C:\Users\pv\Proj\test\ssh-test\node_modules\ssh2\node_modules\ssh2-streams\lib\ssh.js:213:15)
at SSH2Stream.Transform._write (_stream_transform.js:167:12)
at doWrite (_stream_writable.js:301:12)
at writeOrBuffer (_stream_writable.js:288:5)
我该怎么办?
答案 0 :(得分:0)
您没有处理pty
会话事件。没有事件处理程序时的默认操作是拒绝请求。因此,要允许所有pty
个请求,请添加以下内容:
session.on 'pty', (accept, reject, info) ->
accept()