很抱歉,如果这个问题重复,但是找不到解决方案。
看来我的问题很简单,但我不明白自己在做什么错。我想设置ssh客户端连接到它并在shell中进行一些命令。因此,第一件事-我选择了ssh2库并尝试使用following code。
var fs = require("fs");
var crypto = require("crypto");
var inspect = require("util").inspect;
var buffersEqual = require("buffer-equal-constant-time");
var ssh2 = require("ssh2");
var utils = ssh2.utils;
var pubKey = utils.genPublicKey(
utils.parseKey(fs.readFileSync("user.pub")),
);
new ssh2.Server(
{
hostKeys: [fs.readFileSync("host.key")],
},
function(client) {
console.log("Client connected!");
client
.on("authentication", function(ctx) {
if (
ctx.method === "password" &&
// Note: Don't do this in production code, see
// https://www.brendanlong.com/timing-attacks-and-usernames.html
// In node v6.0.0+, you can use `crypto.timingSafeEqual()` to safely
// compare two values.
ctx.username === "foo" &&
ctx.password === "bar"
)
ctx.accept();
else if (
ctx.method === "publickey" &&
ctx.key.algo === pubKey.fulltype &&
buffersEqual(ctx.key.data, pubKey.public)
) {
if (ctx.signature) {
var verifier = crypto.createVerify(ctx.sigAlgo);
verifier.update(ctx.blob);
if (verifier.verify(pubKey.publicOrig, ctx.signature))
ctx.accept();
else ctx.reject();
} else {
// if no signature present, that means the client is just checking
// the validity of the given public key
ctx.accept();
}
} else ctx.reject();
})
.on("ready", function() {
console.log("Client authenticated!");
client.on("session", function(accept, reject) {
var session = accept();
session.once("exec", function(accept, reject, info) {
console.log(
"Client wants to execute: " + inspect(info.command),
);
var stream = accept();
stream.stderr.write("Oh no, the dreaded errors!\n");
stream.write("Just kidding about the errors!\n");
stream.exit(0);
stream.end();
});
});
})
.on("end", function() {
console.log("Client disconnected");
});
},
).listen(0, "127.0.0.1", function() {
console.log("Listening on port " + this.address().port);
});
我将其放入index.js
文件中。
在使用此代码之前:
ssh-keygen -t rsa -b 4096
设置了密钥对,得到了两个密钥ssh.pub
和ssh
; user.pub
和host.key
; ssh-add host.key
。回应为Identity added:
host.key (host.key)
。似乎一切都很好。我跑了node index.js
并遇到以下错误:
throw new Error('Missing passphrase for encrypted private key')
我做错了什么?也许创建密钥对的命令错误?如果您能帮助我,我将不胜感激。
答案 0 :(得分:0)
因此问题出在您(如您在评论中所承认的)使用受密码短语保护的主机密钥这一事实所致。 ssh2
自然也需要密码。
文档说
hostKeys -数组-包含主机私钥或格式为
{ key: <Buffer/string>, passphrase: <string> }
(用于加密私钥)的对象的缓冲区/字符串的数组。 (必需)
所以尝试
{
hostKeys: [
{
key: fs.readFileSync('...'),
passphrase: 'the-passphrase-you-used',
},
],
}
我无法重现此问题(使用更简单的代码):
$ yarn add ssh2
$ cat > index.js
var fs = require("fs");
var crypto = require("crypto");
var inspect = require("util").inspect;
var ssh2 = require("ssh2");
var utils = ssh2.utils;
new ssh2.Server(
{
hostKeys: [fs.readFileSync("host.key")],
},
function(client) {
console.log("Client connected!");
client
.on("authentication", function(ctx) {
ctx.accept();
})
.on("ready", function() {
console.log("Client authenticated!");
client.on("session", function(accept, reject) {
var session = reject();
console.log("Client session rejected.");
});
})
.on("end", function() {
console.log("Client disconnected");
});
},
).listen(0, "127.0.0.1", function() {
console.log("Listening on port " + this.address().port);
});
$ ssh-keygen -t rsa -b 1024
Generating public/private rsa key pair.
Enter file in which to save the key: ./host
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in ./host.
Your public key has been saved in ./host.pub.
$ mv host host.key
$ node index.js
Listening on port 52182
Client connected!
Client authenticated!
Client session rejected.
Client disconnected
(我的ssh连接尝试是:)
$ ssh foo@127.0.0.1 -p 52182
channel 0: open failed: administratively prohibited:
Connection to 127.0.0.1 closed.
~ $