我想先连接到 aws 并通过 Api 从 Nodejs 中点击 python 命令。不知道该怎么做?
I want to first connect to aws and hit a python command from Nodejs via Api. Not sure how to do?
我想从 nodejs.The 执行 'python3 sample.py --path.data=/root/tej4' 命令 下面是我的代码 -
const runningPython = async(req,res,next) => {
ssh.connect({
host: '******',
port: 22,
username: 'ubuntu',
privateKey: '*******'
}).then(function () {
exec('python3 sample.py --path.data=/root/tej4', (err, stdout, stderr) => {
if (err) {
console.log(err)
}
// the *entire* stdout and stderr (buffered)
console.log(`stdout: ${stdout}`);
console.log(`stderr: ${stderr}`);
});
})
}
但我得到的回应是它试图 运行 在本地而不是在 aws cli 上。不确定如何实现?
Error: Command failed: python3 sample.py --path.data=/root/tej4
Python was not found; run without arguments to install from the Microsoft Store, or disable this shortcut from Settings > Manage App Execution Aliases.
at ChildProcess.exithandler (node:child_process:397:12)
at ChildProcess.emit (node:events:390:28)
at maybeClose (node:internal/child_process:1064:16)
at Process.ChildProcess._handle.onexit (node:internal/child_process:301:5) {
killed: false,
code: 9009,
signal: null,
cmd: 'python3 sample.py --path.data=/root/tej4'
}
stdout:
stderr: Python was not found; run without arguments to install from the Microsoft Store, or disable this shortcut from Settings > Manage App Execution Aliases.
您必须创建类型 node_ssh
的实例并使用该实例调用 connect
和 executeCommand
方法
const node_ssh = require('node-ssh');
const ssh = new node_ssh();
ssh
.connect({
host: '******',
port: 22,
username: 'ubuntu',
privateKey: '*******'
})
.then(() => {
ssh.execCommand('python3 sample.py --path.data=/root/tej4', { cwd:'/your-python-script-path-here' })
.then((result) => {
console.log('STDOUT: ' + result.stdout)
console.log('STDERR: ' + result.stderr)
});
您可以使用 ssh 库,但对于 sudo su 和 运行 多个命令切换到 ssh2shell,其代码如下所述:
const runningPython = async (req, res, next) => {
var host = {
server: {
host: ***** ,
username: ***** ,
privateKey: fs.readFileSync('ppk file path'),
},
commands: ["sudo su -", "python3 sample.py --path.data=root/tej4"],
onCommandComplete: function (command, response, sshObj) {
if (sshObj.debug) {
this.emit("msg", this.sshObj.server.host + ": host.onCommandComplete event, command: " + command);
}
if (command === "sudo su -" && response.indexOf("/root") != -1) {
sshObj.commands.unshift("msg:The command and response check worked. Added another cd command.");
sshObj.commands.unshift("cd .ssh");
} else if (command === "python3 sample.py --path.data=root/tej4") {
this.emit("msg", response);
}
},
onEnd: function (sessionText, sshObj) {
if (sshObj.debug) {
this.emit("msg", this.sshObj.server.host + ": host.onEnd event");
}
}
};
let SSH = new SSH2Shell(host),
//Use a callback function to process the full session text
callback = function (sessionText) {
console.log(sessionText)
}
//Start the process
SSH.connect(callback);
}
我想从 nodejs.The 执行 'python3 sample.py --path.data=/root/tej4' 命令 下面是我的代码 -
const runningPython = async(req,res,next) => {
ssh.connect({
host: '******',
port: 22,
username: 'ubuntu',
privateKey: '*******'
}).then(function () {
exec('python3 sample.py --path.data=/root/tej4', (err, stdout, stderr) => {
if (err) {
console.log(err)
}
// the *entire* stdout and stderr (buffered)
console.log(`stdout: ${stdout}`);
console.log(`stderr: ${stderr}`);
});
})
}
但我得到的回应是它试图 运行 在本地而不是在 aws cli 上。不确定如何实现?
Error: Command failed: python3 sample.py --path.data=/root/tej4
Python was not found; run without arguments to install from the Microsoft Store, or disable this shortcut from Settings > Manage App Execution Aliases.
at ChildProcess.exithandler (node:child_process:397:12)
at ChildProcess.emit (node:events:390:28)
at maybeClose (node:internal/child_process:1064:16)
at Process.ChildProcess._handle.onexit (node:internal/child_process:301:5) {
killed: false,
code: 9009,
signal: null,
cmd: 'python3 sample.py --path.data=/root/tej4'
}
stdout:
stderr: Python was not found; run without arguments to install from the Microsoft Store, or disable this shortcut from Settings > Manage App Execution Aliases.
您必须创建类型 node_ssh
的实例并使用该实例调用 connect
和 executeCommand
方法
const node_ssh = require('node-ssh');
const ssh = new node_ssh();
ssh
.connect({
host: '******',
port: 22,
username: 'ubuntu',
privateKey: '*******'
})
.then(() => {
ssh.execCommand('python3 sample.py --path.data=/root/tej4', { cwd:'/your-python-script-path-here' })
.then((result) => {
console.log('STDOUT: ' + result.stdout)
console.log('STDERR: ' + result.stderr)
});
您可以使用 ssh 库,但对于 sudo su 和 运行 多个命令切换到 ssh2shell,其代码如下所述:
const runningPython = async (req, res, next) => {
var host = {
server: {
host: ***** ,
username: ***** ,
privateKey: fs.readFileSync('ppk file path'),
},
commands: ["sudo su -", "python3 sample.py --path.data=root/tej4"],
onCommandComplete: function (command, response, sshObj) {
if (sshObj.debug) {
this.emit("msg", this.sshObj.server.host + ": host.onCommandComplete event, command: " + command);
}
if (command === "sudo su -" && response.indexOf("/root") != -1) {
sshObj.commands.unshift("msg:The command and response check worked. Added another cd command.");
sshObj.commands.unshift("cd .ssh");
} else if (command === "python3 sample.py --path.data=root/tej4") {
this.emit("msg", response);
}
},
onEnd: function (sessionText, sshObj) {
if (sshObj.debug) {
this.emit("msg", this.sshObj.server.host + ": host.onEnd event");
}
}
};
let SSH = new SSH2Shell(host),
//Use a callback function to process the full session text
callback = function (sessionText) {
console.log(sessionText)
}
//Start the process
SSH.connect(callback);
}