在 Node.js 中执行 bash 脚本时 SSH 连接被拒绝
SSH Connection gets refused when executing bash script within Node.js
所以我的结构是这样的:
main.js
folder1/
jsFile1.js
folder2/
jsFile2.js
bashFile.sh
main.js
需要 jsFile1.js
,然后需要 运行s jsFile2.js
。 jsFile2.js
然后最后 运行s bashFile.sh
使用 ShellJS 并传递三个参数。
shell.exec(`bash utils/sshAndCreate.sh ${ip} ${encryptedUser} ${encryptedPw}`);
bashFile.sh:
#!/bin/bash
# Variables
host=
user=
pw=
# 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
我的问题是,当我使用 bash bashFile.sh parameter1 parameter2 parameter3
手动启动 bashFile.sh
时,它工作得很好并且符合预期,但是当我 运行 main.js
时它不起作用最终还有 运行 脚本。我刚得到
ssh: connect to host myServerIpHere port 22: Connection refused
同样奇怪的是,我将 shell.exec
部分移到了 jsFile1.js
并且它工作了几次但是当我再次尝试时我的连接也被拒绝了。
注意 1:我之前从未连接到服务器,所以它不在我的 known_hosts
.
注意 2:child_process 产生相同的错误。
我现在找到了解决办法。尽管服务器 应该 准备就绪,但有时却没有。所以我通过重复连接过程直到服务器接受连接来解决它。
until ssh root@$host command
do
sleep 10
done
所以我的结构是这样的:
main.js
folder1/
jsFile1.js
folder2/
jsFile2.js
bashFile.sh
main.js
需要 jsFile1.js
,然后需要 运行s jsFile2.js
。 jsFile2.js
然后最后 运行s bashFile.sh
使用 ShellJS 并传递三个参数。
shell.exec(`bash utils/sshAndCreate.sh ${ip} ${encryptedUser} ${encryptedPw}`);
bashFile.sh:
#!/bin/bash
# Variables
host=
user=
pw=
# 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
我的问题是,当我使用 bash bashFile.sh parameter1 parameter2 parameter3
手动启动 bashFile.sh
时,它工作得很好并且符合预期,但是当我 运行 main.js
时它不起作用最终还有 运行 脚本。我刚得到
ssh: connect to host myServerIpHere port 22: Connection refused
同样奇怪的是,我将 shell.exec
部分移到了 jsFile1.js
并且它工作了几次但是当我再次尝试时我的连接也被拒绝了。
注意 1:我之前从未连接到服务器,所以它不在我的 known_hosts
.
注意 2:child_process 产生相同的错误。
我现在找到了解决办法。尽管服务器 应该 准备就绪,但有时却没有。所以我通过重复连接过程直到服务器接受连接来解决它。
until ssh root@$host command
do
sleep 10
done