I have created the following node.js
code with the spawn child process that executes a bash
code inside it:
const spawn = require('child_process').spawn
const shellcode = `
file=$(mktemp)
cat > $file <<- "EOF"
echo "Hello World"
sleep 2
echo "Hello World"
sleep 2
echo "Hello World"
sleep 2
echo "Hello World"
EOF
bash $file
`
const child = spawn('/bin/bash', [ '-c', shellcode ])
child.stdout.on('data', (data) => {
console.log(data.toString())
})
It works perfectly fine, it executes the bash
inside the shellcode
variable and it prints Hello World
while the node.js
code is running.
My problem starts with Python
, I've created the following code:
const spawn = require('child_process').spawn
const shellcode = `
file=$(mktemp)
cat > $file <<- "EOF"
import os
print('Hello World')
os.system("sleep 2")
print('Hello World')
os.system("sleep 2")
print('Hello World')
os.system("sleep 2")
print('Hello World')
EOF
python3 $file
`
const child = spawn('/bin/bash', [ '-c', shellcode ])
child.stdout.on('data', (data) => {
console.log(data.toString())
})
However, this code doesn't execute as a spawn
code. It waits for the process to finish to print the results to stdout. I've also tested the same logic with a node.js code inside the shellcode
variable and it works fine just like bash. What am I getting wrong? I'm not sure if it's a Python
issue or if it's a node.js
issue. Why is the Python
code not behaving as a spawn (it's waiting for the process to finish to print its output)?