通常docker内部没有网络,所以我们先离线安装需要的依赖包,然后再使用sh脚本容器内部访问宿主机同网络端其他服务器ip,实现监测远程ip
telnet包依赖于netbase包,但是netbase包没有安装。你需要先安装netbase包,然后再尝试安装telnet。
apt-get download netbase
dpkg -i netbase*.deb
安装telnet
apt-get download telnet
dpkg -i telnet_*.deb
telnet 10.1.7.139 1883
安装ping
apt-get download iputils-ping#在有网络的宿主机下载安装包
dpkg -i iputils-ping_*.deb#在容器内部执行安装指令安装离线包
ping 10.1.7.139 1883#测试
通信测试脚本
bash
#!/bin/bash
service mosquitto start
echo "mosquitto..."
sysctl vm.overcommit_memory=1
service redis-server start &
echo "redis..."
mongod -f /data/config/mongod.conf --fork
echo "mongo..."
service nginx start
echo "nginx..."
# 要ping的服务器地址
SERVER_IP="10.1.77.74"
# 要检查的端口
SERVER_PORT=1883
# 等待时间(秒)
WAIT_TIME=60
# 尝试连接端口的函数
check_port() {
local ip="$1"
local port="$2"
# 尝试使用telnet连接到指定端口,并立即发送quit命令退出
(echo quit; sleep 1) | telnet "$ip" "$port" 2>&1 | grep -q "Connected"
# 根据grep的退出状态来返回结果
return $?
}
# 无限循环直到服务器可达和端口开放
while true; do
# 尝试ping服务器
if ping -c 1 "$SERVER_IP" &> /dev/null; then
echo "$(date): Server $SERVER_IP is reachable."
# 尝试检查端口是否开放
if check_port "$SERVER_IP" "$SERVER_PORT"; then
echo "$(date): Port $SERVER_PORT is open on $SERVER_IP."
# 在这里执行后续命令
echo "$(date): Executing commands..."
# 例如:
# service some_service start
# /path/to/some/script.sh
cd /bwss/agent/iot/server/edgeagent/ && ./start.sh
echo "edgeagent..."
sleep 10;
cd /bwss/agent/iot/server/collector/ai-collector/ && ./start.sh
echo "ai-collector..."
break
# 如果需要的话,可以在这里添加退出循环的条件
# 例如:检查某个服务是否已成功启动
# if [ some_condition ]; then
# break
# fi
else
echo "$(date): Port $SERVER_PORT is closed on $SERVER_IP. Retrying..."
fi
else
echo "$(date): Server $SERVER_IP is unreachable. Retrying after $WAIT_TIME seconds..."
fi
# 等待一段时间后再次尝试
sleep $WAIT_TIME
done