Run any Linux terminal command from TypeScript?
In node.js you could spawn a child-process
const { exec } = require('child_process');
exec('sudo apt-get update', (err, stdout, stderr) => {
// your callback
});
How to run TypeScript files from command line?
How do I do the same with Typescript
You can leave tsc
running in watch mode using tsc -w -p .
and it will generate .js
files for you in a live fashion, so you can run node foo.js
like normal
TS Node
There is ts-node : https://github.com/TypeStrong/ts-node that will compile the code on the fly and run it through node /p>
npx ts-node src/foo.ts
Run Linux command from Angular 4 component
The modern browsers opens the webpage in isolated sandbox so they have have no access to clients' computers.
Imagine the damage that could be done if a black hat could run batch script on computer that opens his webpage.
The only way to run the script is to run the desktop application on client's machine.
The example code you provided is Node.js code, the desktop framework that user have to install on his machine and run the code intentionally. There's (fortunately!) no way to run it remotely via webpage.
Execute and get the output of a shell command in node.js
This is the method I'm using in a project I am currently working on.
var exec = require('child_process').exec;
function execute(command, callback){
exec(command, function(error, stdout, stderr){ callback(stdout); });
};
Example of retrieving a git user:
module.exports.getGitUser = function(callback){
execute("git config --global user.name", function(name){
execute("git config --global user.email", function(email){
callback({ name: name.replace("\n", ""), email: email.replace("\n", "") });
});
});
};
How to use the linux terminal through angular2
In short: no you cannot. Angular is UI framework, that is executed in browser/client side, i.e. it has nothing to do with your server code (Rapsberry). All you can do: make endpoint on backend, that will execute command if http request made. Unless you are using some javascript server on backend (node/meteor), but again it has nothing to do with Angular itself and you have to read documentation of your server.
If your target is executing command line on client side, so answer is still "no you cannot", as browser is isolated from other system and there is no way to execute any system commands from browser.
nodejs execute command on remote linux server
I solved the problem myself. There is one npm package (ssh-exec) available for ssh command execution. Below is the code I used.
var exec = require('ssh-exec')
var v_host = 'XX.XX.XX.XXX'
exec('ls -lh', {
user: 'root',
host: 'XX.XX.XX.XXX',
password: 'password'
}).pipe(process.stdout , function (err, data) {
if ( err ) { console.log(v_host); console.log(err); }
console.log(data)
})
How to run interactive shell command inside node.js?
First and foremost, one of the things preventing node from interfacing with other interactive shells is that the child application must keep its "interactive" behavior, even when stdin
doesn't look like a terminal. python
here knew that its stdin
wasn't a terminal, so it refused to work. This can be overridden by adding the -i
flag to the python command.
Second, as you well mentioned in the update, you forgot to write a new line character to the stream, so the program behaved as if the user didn't press Enter.
Yes, this is the right way to go, but the lack of an interactive mode prevented you from retrieving any results.
Here's something you can do to send multiple inputs to the interactive shell, while still being able to retrieve each result one by one. This code will be resistant to lengthy outputs, accumulating them until a full line is received before performing another instruction. Multiple instructions can be performed at a time as well, which may be preferable if they don't depend on the parent process' state. Feel free to experiment with other asynchronous structures to fulfil your goal.
var cp = require('child_process');
var childProcess = cp.spawn('python', ['-i']);
childProcess.stdout.setEncoding('utf8')
var k = 0;
var data_line = '';
childProcess.stdout.on("data", function(data) {
data_line += data;
if (data_line[data_line.length-1] == '\n') {
// we've got new data (assuming each individual output ends with '\n')
var res = parseFloat(data_line);
data_line = ''; // reset the line of data
console.log('Result #', k, ': ', res);
k++;
// do something else now
if (k < 5) {
// double the previous result
childProcess.stdin.write('2 * + ' + res + '\n');
} else {
// that's enough
childProcess.stdin.end();
}
}
});
childProcess.stdin.write('1 + 0\n');
How to compile typescript using npm command?
You can launch the tsc
command (typescript compiler) with --watch
argument.
Here is an idea :
- Configure typescript using
tsconfig.json
file - Run
tsc --watch
, so every time you change a.ts
file,tsc
will compile it and produce the output (let say you configured typescript to put the output in./dist
folder) - Use
nodemon
to watch if files in./dist
have changed and if needed to relaunch the server.
Here are some scripts (to put in package.json
) that can help you to do it (you will need to install the following modules npm install --save typescript nodemon npm-run-all rimraf
)
"scripts": {
"clean": "rimraf dist",
"start": "npm-run-all clean --parallel watch:build watch:server --print-label",
"watch:build": "tsc --watch",
"watch:server": "nodemon './dist/index.js' --watch './dist'"
}
Then you just need to run npm start
in a terminal
Related Topics
Bridge Serial-Ports Over Network
Cache Coloring on Slab Memory Management in Linux Kernel
Trouble Ssh Tunneling to Remote Server
Initramfs Built into Custom Linux Kernel Is Not Running
How to Store Your Github Https Password on Linux in a Terminal Keychain
Linux Script with Netcat Stops Working After X Hours
What's The Meaning of 'C' in Result of Command "Ls -L /Dev/Tty'
Shell Gnu-Screen -X Stuff Problems
Joining Line Breaks in Fasta File with Condition in Sed/Awk/Perl One-Liner
How to Create a Virtual Device in Linux
Prevent Fork() from Copying Sockets
Would It Be Possible to Read Out Physical Keyboard Strokes in Node.Js
Why Are Both "True" and "False" Tests True
Kubernetes Pods Terminated - Exit Code 137
Can Tmux Save Commands to a File, Like .Bash_History