如何从Atom electron应用程序调用Shell脚本或python脚本

2024-05-23 23:09:15 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试使用Atom electron为Mac和Windows编写一个桌面应用程序。

我需要的是:

一个按钮。

当用户单击按钮时,它将运行以下shell(或python脚本):

ping x.x.x.x

结果将显示在文本区域中。

我试着用[shelljs]和[yargs],但似乎用原子电子是行不通的。

我只想使用JAVASCRIPT来编写桌面应用程序(当然有GUI),它调用一些脚本(shell&;python)来完成一些自动化工作。

如有任何建议,将不胜感激,谢谢:)


Tags: 用户文本脚本应用程序区域windowsmacshell
3条回答

您可以使用以下代码使用child_进程来存档您正在尝试执行的操作

var exec = require('child_process').exec
function Callback(err, stdout, stderr) {
    if (err) {
        console.log(`exec error: ${err}`);
        return;
    }else{
        console.log(`${stdout}`);
    }
}

res = exec('ping xxx.xxx.xxx', Callback);

它可以直接使用Node完成,您可以使用child_process模块。请注意这是异步的。

const exec = require('child_process').exec;

function execute(command, callback) {
    exec(command, (error, stdout, stderr) => { 
        callback(stdout); 
    });
};

// call the function
execute('ping -c 4 0.0.0.0', (output) => {
    console.log(output);
});

我鼓励您也看看npm,有很多模块可以帮助您做您想要的事情,而无需调用python脚本。

尝试使用节点powershell。您可以直接执行shell脚本命令并显示结果。

var shell = require('node-powershell')
var ps = new shell()
ps.addCommand('ping -c 4 0.0.0.0')
ps.invoke()
.then(function (output) {
    console.log(output)
})
.catch(function (err) {
    console.log(err)
    ps.dispose()
})

请参阅:https://www.npmjs.com/package/node-powershell

相关问题 更多 >