node.js - 在 Node.js 中执行 bash 脚本时,SSH 连接被拒绝
问题描述
所以我的结构是这样的:
main.js
folder1/
jsFile1.js
folder2/
jsFile2.js
bashFile.sh
main.js
requires jsFile1.js
,然后 requires 并运行jsFile2.js
. jsFile2.js
然后最终bashFile.sh
使用 ShellJS 运行并传递三个参数。
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
我的问题是,当我手动开始bashFile.sh
使用它时,它工作得很好并且符合预期,但是当我运行它最终也运行脚本bash bashFile.sh parameter1 parameter2 parameter3
时它不起作用。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
推荐阅读
- mongodb - MongoDB - 重命名与正则表达式匹配的列名
- laravel - 符号链接 Laravel 后存储链接未加载图像
- jquery - 我可以通过 Ajax 将两个数组传递给控制器吗?(文件数组和整数数组)
- r - 在 ggplot/ggimage 中保持形状和图像的纵横比
- javascript - Wix 代码/为什么全选按钮不全选?
- azure-data-factory - Dynamic name of Spark notebook in Notebook activity
- java - What is the minimum number of test cases for edge and condition coverage in this example?
- python - 在 Python Selenium (Mac OS) 中使用向下箭头键
- macos - 如何更改在 pgrep 中显示的应用程序名称?
- python - 带有读取和随机模块的 Python 密码生成器