通常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#测试
通信测试脚本
#!/bin/bashservice mosquitto startecho "mosquitto..."sysctl vm.overcommit_memory=1service redis-server start &echo "redis..."mongod -f /data/config/mongod.conf --forkecho "mongo..."service nginx startecho "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服务器ifping -c 1 "$SERVER_IP" &> /dev/null; thenecho "$(date): Server $SERVER_IP is reachable."# 尝试检查端口是否开放if check_port "$SERVER_IP" "$SERVER_PORT"; thenecho "$(date): Port $SERVER_PORT is open on $SERVER_IP."# 在这里执行后续命令echo "$(date): Executing commands..." # 例如: # service some_service start # /path/to/some/script.shcd /bwss/agent/iot/server/edgeagent/ && ./start.shecho "edgeagent..."sleep 10;cd /bwss/agent/iot/server/collector/ai-collector/ && ./start.shecho "ai-collector..."break# 如果需要的话,可以在这里添加退出循环的条件# 例如:检查某个服务是否已成功启动# if [ some_condition ]; then# break# fielseecho "$(date): Port $SERVER_PORT is closed on $SERVER_IP. Retrying..."fielseecho "$(date): Server $SERVER_IP is unreachable. Retrying after $WAIT_TIME seconds..."fi# 等待一段时间后再次尝试sleep $WAIT_TIMEdone