So my structure looks like this:
main.js
folder1/
jsFile1.js
folder2/
jsFile2.js
bashFile.sh
main.js
requires jsFile1.js
which then requires and runs jsFile2.js
. jsFile2.js
then finally runs bashFile.sh
using ShellJS and passes three arguments.
shell.exec(`bash utils/sshAndCreate.sh ${ip} ${encryptedUser} ${encryptedPw}`);
bashFile.sh:
#!/bin/bash
# Variables
host=$1
user=$2
pw=$3
# Postdata
postdata="someUnfinishedStuff"
# Download the file to the server
ssh root@$host -o StrictHostKeyChecking=no wget http://myOtherServer/api --post-data $postdata -O bash.sh
My problem is that when I manually start bashFile.sh
using bash bashFile.sh parameter1 parameter2 parameter3
it works just fine and as expected but it doesn't work when I run main.js
which eventually also runs the script. I just get
ssh: connect to host myServerIpHere port 22: Connection refused
What is also weird, I moved the shell.exec
part to jsFile1.js
and it worked a few times but when I tried again my connection also gets refused.
Note 1: I never connected to the server before, so it isn't in my known_hosts
.
Note 2: A child_process produces the same error.
I now found a solution. Even though the server should be ready it sometimes isn't. So I solved it by just repeating the process of connecting until the server accepts the connection.
until ssh root@$host command
do
sleep 10
done