如何在 exec 函数中使用 `cd` 命令指定文件夹位置?
How to specify folder location using `cd` command inside exec function?
我们如何在exec()
中使用cd
命令而不是指定在远程服务器中执行的文件的根目录我在nodejs.What中完成是这样的。
var command_part1 = `ssh -p 22 root@ip`;
var command_part2 = `python3 test.py ${input}`;
var folderPath = `/root/folder/`;
var child = exec(`${command_part1} && cd ${folderPath} && ${command_part2}` , function (error, stdout, stderr) {
if (error !== null) {
callback(error)
} else {
callback();
}
});
但是此代码不起作用,当我在本地执行此命令时,仅发生 ssh 登录,其他命令无效 working.How 我可以使它起作用吗?我不想像 python3 /root/folder/test.py ${input};
那样指定文件夹路径
提前致谢
试试这个代码,
`var command = `sshpass -p givePassword ssh -o "StrictHostKeyChecking no" root@ip "cd ${folderPath} && ${command_part2}"`
sshpass
是一个简单轻量级的命令行工具,它使我们能够向命令提示符本身提供密码(非交互式密码验证),以便可以执行自动化的 shell 脚本
我们如何在exec()
中使用cd
命令而不是指定在远程服务器中执行的文件的根目录我在nodejs.What中完成是这样的。
var command_part1 = `ssh -p 22 root@ip`;
var command_part2 = `python3 test.py ${input}`;
var folderPath = `/root/folder/`;
var child = exec(`${command_part1} && cd ${folderPath} && ${command_part2}` , function (error, stdout, stderr) {
if (error !== null) {
callback(error)
} else {
callback();
}
});
但是此代码不起作用,当我在本地执行此命令时,仅发生 ssh 登录,其他命令无效 working.How 我可以使它起作用吗?我不想像 python3 /root/folder/test.py ${input};
提前致谢
试试这个代码,
`var command = `sshpass -p givePassword ssh -o "StrictHostKeyChecking no" root@ip "cd ${folderPath} && ${command_part2}"`
sshpass
是一个简单轻量级的命令行工具,它使我们能够向命令提示符本身提供密码(非交互式密码验证),以便可以执行自动化的 shell 脚本