diff --git a/download_and_execute.sh b/download_and_execute.sh index cbd0a00..e24b420 100644 --- a/download_and_execute.sh +++ b/download_and_execute.sh @@ -1,5 +1,67 @@ #!/bin/bash +check_software() { + local software_name=$1 + if command -v $software_name &> /dev/null + then + echo "$software_name 已安装" + else + echo "$software_name 未安装" + fi +} + +check_library() { + local library_path=$1 + if [ -f "$library_path" ]; then + echo "库文件 $library_path 存在" + else + echo "库文件 $library_path 不存在" + fi +} + +check_python_library() { + local library_name=$1 + python -c "import $library_name" &> /dev/null + if [ $? -eq 0 ]; then + echo "Python库 $library_name 存在" + else + echo "Python库 $library_name 不存在" + fi +} + +# 检查某个软件 +check_software "git" +check_software "redis-server" + +# 检查某个库文件 +#check_library "/usr/lib/libc.so.6" + +# 检查某个Python库 +#check_python_library "numpy" + + +# 检查是否安装了redis +if ! command -v redis-server &> /dev/null; then + echo "未找到redis-server,尝试安装redis..." + if command -v apt-get &> /dev/null; then + sudo apt-get update + sudo apt-get install -y redis-server + elif command -v yum &> /dev/null; then + sudo yum install -y redis + else + echo "无法自动安装redis,请手动安装redis后重试。" + exit 1 + fi +fi + +# 检查redis-server安装是否成功 +if command -v redis-server &> /dev/null; then + echo "Redis安装成功" +else + echo "Redis安装失败,请手动安装后重试。" + exit 1 +fi + # 设置仓库URL和本地目录 repo_url="https://bdgit.educoder.net/pv2ajsu8k/smp_pc_20240625.git" local_dir="smp_pc" @@ -18,6 +80,12 @@ if ! command -v git &> /dev/null; then fi fi +# 检查是否存在本地目录 +if [ -d "$local_dir" ]; then + echo "本地目录 $local_dir 已存在,请删除后重试。" + exit 1 +fi + # 克隆仓库 if git clone $repo_url $local_dir; then echo "仓库克隆成功" @@ -26,6 +94,11 @@ else exit 1 fi + + + + + # 进入仓库目录 cd $local_dir || { echo "无法进入目录 $local_dir"; exit 1; } @@ -43,42 +116,4 @@ if [ -f "smp_init.sh" ]; then else echo "smp_init.sh 不存在" exit 1 -fi - -check_software() { - local software_name=$1 - if command -v $software_name &> /dev/null - then - echo "$software_name 已安装" - else - echo "$software_name 未安装" - fi -} - -check_library() { - local library_path=$1 - if [ -f "$library_path" ]; then - echo "库文件 $library_path 存在" - else - echo "库文件 $library_path 不存在" - fi -} - -check_python_library() { - local library_name=$1 - python -c "import $library_name" &> /dev/null - if [ $? -eq 0 ]; then - echo "Python库 $library_name 存在" - else - echo "Python库 $library_name 不存在" - fi -} - -# 检查某个软件 -check_software "git" - -# 检查某个库文件 -#check_library "/usr/lib/libc.so.6" - -# 检查某个Python库 -check_python_library "numpy" +fi \ No newline at end of file diff --git a/edu_coder/get_user.sh b/edu_coder/get_user.sh new file mode 100644 index 0000000..a7cc63f --- /dev/null +++ b/edu_coder/get_user.sh @@ -0,0 +1,8 @@ +#!/bin/bash + + +export $(cat /proc/1/environ |tr '\0' '\n' |grep -v ' ' |grep -vE '^(PATH=|HOME=|IFS=|CDPATH=|LS_COLORS=|MANPATH=|LC_ALL=|LANG=)' | xargs) >/dev/null 2>&1 + +env | grep user > user.txt + + diff --git a/edu_coder/mqtt_client.py b/edu_coder/mqtt_client.py new file mode 100644 index 0000000..39f66f8 --- /dev/null +++ b/edu_coder/mqtt_client.py @@ -0,0 +1,50 @@ +import paho.mqtt.client as mqtt +import time + +broker = '121.40.129.71' +port = 48835 + +# 回调函数,当客户端收到连接响应时被调用 +def on_connect(client, userdata, flags, rc, properties=None): + print("连接结果: " + str(rc)) + # 订阅响应主题 + client.subscribe("response/topic") + +# 回调函数,当客户端收到消息时被调用 +def on_message(client, userdata, msg): + print("收到消息: " + msg.topic + " " + str(msg.payload)) + + if msg.payload.decode() == "Set user.txt success!": + global response_received + response_received = True + +# 创建客户端实例 +client = mqtt.Client(mqtt.CallbackAPIVersion.VERSION2,) + +# 设置回调函数 +client.on_connect = on_connect +client.on_message = on_message + +# 连接到MQTT代理 +client.connect(broker, port, 60) + +# 启动一个后台线程来处理网络流量和回调 +client.loop_start() + +with open("user.txt", "r") as f: + user = f.read().strip() + +# 发布消息 +client.publish("request/topic", user, qos=2) + +# 等待响应 +response_received = False +while not response_received: + print("等待响应...") + time.sleep(1) + +# 停止网络循环并断开连接 +client.loop_stop() +client.disconnect() + +print("收到响应,客户端退出。") diff --git a/smp_coder/mqtt_client.py b/smp_coder/mqtt_client.py new file mode 100644 index 0000000..7166b63 --- /dev/null +++ b/smp_coder/mqtt_client.py @@ -0,0 +1,56 @@ +import os + +import paho.mqtt.client as mqtt +import time + +broker = '121.40.129.71' +port = 48835 + +# 回调函数,当客户端收到连接响应时被调用 +def on_connect(client, userdata, flags, rc, properties=None): + print("连接结果: " + str(rc)) + # 订阅响应主题 + client.subscribe("request/topic") + +# 回调函数,当客户端收到消息时被调用 +def on_message(client, userdata, msg): + print("收到消息: " + msg.topic + " " + str(msg.payload)) + + if not os.path.exists("user.txt"): + with open("user.txt", "a") as f: + f.write(msg.payload.decode("utf-8") + "\n") + else: + + # 全局变量,用于标记是否收到响应 + global response_received + response_received = True + +# 创建客户端实例 +client = mqtt.Client(mqtt.CallbackAPIVersion.VERSION2,) + +# 设置回调函数 +client.on_connect = on_connect +client.on_message = on_message + +# 连接到MQTT代理 +client.connect(broker, port, 60) + +# 启动一个后台线程来处理网络流量和回调 +client.loop_start() + +# 发布消息 + + +# 等待响应 +response_received = False +while not response_received: + print("等待响应...") + time.sleep(1) + +client.publish("response/topic", "Set user.txt success!", qos=2) + +# 停止网络循环并断开连接 +client.loop_stop() +client.disconnect() + +print("收到响应,客户端退出。") diff --git a/smp_init.sh b/smp_init.sh index 49a3139..37d4799 100644 --- a/smp_init.sh +++ b/smp_init.sh @@ -29,7 +29,7 @@ else fi # 安装 paho-mqtt 库 -pip install paho-mqtt +pip install -i https://pypi.tuna.tsinghua.edu.cn/simple paho-mqtt==2.1.0 # 验证安装 if python -c "import paho.mqtt.client" &> /dev/null; then @@ -41,3 +41,7 @@ fi # 提示如何退出虚拟环境 echo "要退出虚拟环境,请运行 'deactivate'" +# 运行第一关的mqtt_client.py + +sudo $env_dir/bin/python3 mqtt_client.py +