如果这个问题重复,我很抱歉,但我找不到解决方案。
似乎我的问题很简单,但我不明白我做错了什么。我想设置 ssh 客户端来连接它并在 shell 中执行一些命令。所以第一件事 - 我选择了 ssh2 库并尝试使用以下代码。
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
得到了 2 个密钥ssh.pub
;ssh
- 然后重命名为
user.pub
andhost.key
; - 之后我做了
ssh-add host.key
. 反应是Identity added: host.key (host.key)
。似乎一切都很好。
我跑了node index.js
,有以下错误:
throw new Error('Missing passphrase for encrypted private key')
我做错了什么?也许创建密钥对的命令错误?如果您能帮助我,我将不胜感激。