PeaceSheep's blog PeaceSheep's blog
首页
  • 分类
  • 标签
  • 归档
相关链接
提建议&咨询&赞赏
GitHub (opens new window)

PeaceSheep

以最简洁、易懂的话解决问题
首页
  • 分类
  • 标签
  • 归档
相关链接
提建议&咨询&赞赏
GitHub (opens new window)
  • 01 说明
  • 环境安装与配置

    • ubuntu换源
    • centos安装配置docker
    • k8s介绍及安装教程
    • github actions自动部署前端项目
    • 服务器创建git仓库,部署自己的博客
    • 使用python批量管理linux设备
    • 关于如何使用机房电脑批量烧录U盘的奇思妙想
      • 需要的软件
      • 操作方法
    • 自建Zerotier节点
    • ubuntu安装g++
    • conda从古老版本升级
    • openkylin国产麒麟操作系统安装zerotier
    • nodejs、yarn安装
  • 常用命令与配置文件

  • 常用代码

  • 常用操作
  • 环境安装与配置
PeaceSheep
2023-10-31
目录

关于如何使用机房电脑批量烧录U盘的奇思妙想

因为举办ICPC南京站的需要,想要烧录大量的ISO到U盘里面方便装系统,但是因为一个30口的USBHUB都要大几百,所以突发奇想能不能用机房电脑来进行这个操作。

# 需要的软件

  1. ventoy

因为找了半天找不到可以命令行烧录的软件,但是找到了ventoy,可以使用命令行把一个U盘做成ventoyU盘,然后直接把iso文件复制进去就可以了。

  1. 极域电子教室

因为要批量给所有电脑发送文件夹,以及批量执行命令。

# 操作方法

写了一个python文件:


import os
import shutil
import subprocess
import webbrowser
from shutil import copyfile

file_path = os.path.abspath(__file__)
current_directory = os.path.dirname(file_path)
ISO_PATH = os.path.join(current_directory, "icpc.iso")
VENTOY_PATH = os.path.join(current_directory, "ventoy/Ventoy2Disk.exe")


def show_result(copy_result):
    with open(os.path.join(current_directory, "ventoy/cli_done.txt"), "r") as f:
        result = f.readline()
    print(result)
    if result.find("0") != -1:
        result = "SUCCESS"
    else:
        result = "FAIL"

    html_file_path = os.path.join(current_directory, "result.html")
    content = f"""
    <h1>burn result:{result}</h1>
    <h1>copy result:{copy_result}</h1>
    """
    with open(html_file_path, "w") as f:
        f.write(content)

    webbrowser.open('file://' + html_file_path, new=1)


def get_usb_drive():
    command = 'wmic logicaldisk where "drivetype=2" get caption'
    output = subprocess.check_output(command, universal_newlines=True, shell=True)

    # 将输出按行拆分
    lines = output.strip().split('\n')
    return lines[-1]


def burn_iso(usb_drive):
    cmd = [
        VENTOY_PATH,
        "VTOYCLI",
        "/I",
        f"/Drive:{usb_drive}",
    ]

    result = subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)

    if result.returncode == 0:
        print(f"Successfully initialized Ventoy on {usb_drive}")
    else:
        print(f"Failed to initialize Ventoy on {usb_drive}. Error: {result.stderr.decode()}")

    # 之后你只需将ISO文件复制到U盘,Ventoy会自动检测和引导ISO文件。


def copy_file():
    src = ISO_PATH
    dst = os.path.join(get_usb_drive())
    try:
        shutil.copy(src, dst)
        return "SUCCESS"
    except Exception as err:
        return str(err)


if __name__ == "__main__":
    try:
        print(current_directory)
        usb_drive = get_usb_drive()
        print(f"get usb drive success:{usb_drive}")
        print("burning")
        burn_iso(usb_drive)
        print("copying file")
        copy_result = copy_file()
        show_result(copy_result)
    except Exception as err:
        print(err)

    input()


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86

这个文件可以以命令行的方式,使用python文件所在路径下的ventoy,制作一个ventoyU盘到系统的第一个USB设备。

另外这个文件需要管理员权限,所以不能直接执行,于是又写了一个VBS文件。

Set objShell = CreateObject("Shell.Application")
objShell.ShellExecute "python.exe", "main.py", "", "runas", 1

1
2
3

最后,只需要把python文件、vbs文件、iso文件、ventoy放到一个文件夹里,发送到每台电脑上,然后运行命令cscript main.vbs就可以了。

花絮

在我写好脚本的一个小时后,问了下竞赛的负责老师,他已经买了USBHUB,所以我的脚本白写了..

编辑 (opens new window)
上次更新: 2025/04/15, 10:52:45
使用python批量管理linux设备
自建Zerotier节点

← 使用python批量管理linux设备 自建Zerotier节点→

最近更新
01
ubuntu安装g++显示已有但是输入g++又找不到命令
04-15
02
使用cloudflare-r2搭建webdav
04-08
03
LLM聚合平台客户端对比
03-29
更多文章>
Theme by Vdoing | Copyright © 2022-2025 PeaceSheep
冀ICP备2022004632号-1
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式